chan_pjsip.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, 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 PSJIP SIP Channel Driver
  23. *
  24. * \ingroup channel_drivers
  25. */
  26. /*** MODULEINFO
  27. <depend>pjproject</depend>
  28. <depend>res_pjsip</depend>
  29. <depend>res_pjsip_session</depend>
  30. <support_level>core</support_level>
  31. ***/
  32. #include "asterisk.h"
  33. #include <pjsip.h>
  34. #include <pjsip_ua.h>
  35. #include <pjlib.h>
  36. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  37. #include "asterisk/lock.h"
  38. #include "asterisk/channel.h"
  39. #include "asterisk/module.h"
  40. #include "asterisk/pbx.h"
  41. #include "asterisk/rtp_engine.h"
  42. #include "asterisk/acl.h"
  43. #include "asterisk/callerid.h"
  44. #include "asterisk/file.h"
  45. #include "asterisk/cli.h"
  46. #include "asterisk/app.h"
  47. #include "asterisk/musiconhold.h"
  48. #include "asterisk/causes.h"
  49. #include "asterisk/taskprocessor.h"
  50. #include "asterisk/dsp.h"
  51. #include "asterisk/stasis_endpoints.h"
  52. #include "asterisk/stasis_channels.h"
  53. #include "asterisk/indications.h"
  54. #include "asterisk/format_cache.h"
  55. #include "asterisk/translate.h"
  56. #include "asterisk/threadstorage.h"
  57. #include "asterisk/features_config.h"
  58. #include "asterisk/pickup.h"
  59. #include "asterisk/test.h"
  60. #include "asterisk/message.h"
  61. #include "asterisk/res_pjsip.h"
  62. #include "asterisk/res_pjsip_session.h"
  63. #include "pjsip/include/chan_pjsip.h"
  64. #include "pjsip/include/dialplan_functions.h"
  65. #include "pjsip/include/cli_functions.h"
  66. AST_THREADSTORAGE(uniqueid_threadbuf);
  67. #define UNIQUEID_BUFSIZE 256
  68. static const char channel_type[] = "PJSIP";
  69. static unsigned int chan_idx;
  70. static void chan_pjsip_pvt_dtor(void *obj)
  71. {
  72. struct chan_pjsip_pvt *pvt = obj;
  73. int i;
  74. for (i = 0; i < SIP_MEDIA_SIZE; ++i) {
  75. ao2_cleanup(pvt->media[i]);
  76. pvt->media[i] = NULL;
  77. }
  78. }
  79. /* \brief Asterisk core interaction functions */
  80. static struct ast_channel *chan_pjsip_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);
  81. static int chan_pjsip_sendtext_data(struct ast_channel *ast, struct ast_msg_data *msg);
  82. static int chan_pjsip_sendtext(struct ast_channel *ast, const char *text);
  83. static int chan_pjsip_digit_begin(struct ast_channel *ast, char digit);
  84. static int chan_pjsip_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
  85. static int chan_pjsip_call(struct ast_channel *ast, const char *dest, int timeout);
  86. static int chan_pjsip_hangup(struct ast_channel *ast);
  87. static int chan_pjsip_answer(struct ast_channel *ast);
  88. static struct ast_frame *chan_pjsip_read(struct ast_channel *ast);
  89. static int chan_pjsip_write(struct ast_channel *ast, struct ast_frame *f);
  90. static int chan_pjsip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
  91. static int chan_pjsip_transfer(struct ast_channel *ast, const char *target);
  92. static int chan_pjsip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
  93. static int chan_pjsip_devicestate(const char *data);
  94. static int chan_pjsip_queryoption(struct ast_channel *ast, int option, void *data, int *datalen);
  95. static const char *chan_pjsip_get_uniqueid(struct ast_channel *ast);
  96. /*! \brief PBX interface structure for channel registration */
  97. struct ast_channel_tech chan_pjsip_tech = {
  98. .type = channel_type,
  99. .description = "PJSIP Channel Driver",
  100. .requester = chan_pjsip_request,
  101. .send_text = chan_pjsip_sendtext,
  102. .send_text_data = chan_pjsip_sendtext_data,
  103. .send_digit_begin = chan_pjsip_digit_begin,
  104. .send_digit_end = chan_pjsip_digit_end,
  105. .call = chan_pjsip_call,
  106. .hangup = chan_pjsip_hangup,
  107. .answer = chan_pjsip_answer,
  108. .read = chan_pjsip_read,
  109. .write = chan_pjsip_write,
  110. .write_video = chan_pjsip_write,
  111. .exception = chan_pjsip_read,
  112. .indicate = chan_pjsip_indicate,
  113. .transfer = chan_pjsip_transfer,
  114. .fixup = chan_pjsip_fixup,
  115. .devicestate = chan_pjsip_devicestate,
  116. .queryoption = chan_pjsip_queryoption,
  117. .func_channel_read = pjsip_acf_channel_read,
  118. .get_pvt_uniqueid = chan_pjsip_get_uniqueid,
  119. .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER | AST_CHAN_TP_SEND_TEXT_DATA
  120. };
  121. /*! \brief SIP session interaction functions */
  122. static void chan_pjsip_session_begin(struct ast_sip_session *session);
  123. static void chan_pjsip_session_end(struct ast_sip_session *session);
  124. static int chan_pjsip_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
  125. static void chan_pjsip_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
  126. /*! \brief SIP session supplement structure */
  127. static struct ast_sip_session_supplement chan_pjsip_supplement = {
  128. .method = "INVITE",
  129. .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL,
  130. .session_begin = chan_pjsip_session_begin,
  131. .session_end = chan_pjsip_session_end,
  132. .incoming_request = chan_pjsip_incoming_request,
  133. /* It is important that this supplement runs after media has been negotiated */
  134. .response_priority = AST_SIP_SESSION_AFTER_MEDIA,
  135. };
  136. /*! \brief SIP session supplement structure just for responses */
  137. static struct ast_sip_session_supplement chan_pjsip_supplement_response = {
  138. .method = "INVITE",
  139. .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL,
  140. .incoming_response = chan_pjsip_incoming_response,
  141. .response_priority = AST_SIP_SESSION_BEFORE_MEDIA | AST_SIP_SESSION_AFTER_MEDIA,
  142. };
  143. static int chan_pjsip_incoming_ack(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
  144. static struct ast_sip_session_supplement chan_pjsip_ack_supplement = {
  145. .method = "ACK",
  146. .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL,
  147. .incoming_request = chan_pjsip_incoming_ack,
  148. };
  149. /*! \brief Function called by RTP engine to get local audio RTP peer */
  150. static enum ast_rtp_glue_result chan_pjsip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
  151. {
  152. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
  153. struct chan_pjsip_pvt *pvt;
  154. struct ast_sip_endpoint *endpoint;
  155. struct ast_datastore *datastore;
  156. if (!channel || !channel->session || !(pvt = channel->pvt) || !pvt->media[SIP_MEDIA_AUDIO]->rtp) {
  157. return AST_RTP_GLUE_RESULT_FORBID;
  158. }
  159. datastore = ast_sip_session_get_datastore(channel->session, "t38");
  160. if (datastore) {
  161. ao2_ref(datastore, -1);
  162. return AST_RTP_GLUE_RESULT_FORBID;
  163. }
  164. endpoint = channel->session->endpoint;
  165. *instance = pvt->media[SIP_MEDIA_AUDIO]->rtp;
  166. ao2_ref(*instance, +1);
  167. ast_assert(endpoint != NULL);
  168. if (endpoint->media.rtp.encryption != AST_SIP_MEDIA_ENCRYPT_NONE) {
  169. return AST_RTP_GLUE_RESULT_FORBID;
  170. }
  171. if (endpoint->media.direct_media.enabled) {
  172. return AST_RTP_GLUE_RESULT_REMOTE;
  173. }
  174. return AST_RTP_GLUE_RESULT_LOCAL;
  175. }
  176. /*! \brief Function called by RTP engine to get local video RTP peer */
  177. static enum ast_rtp_glue_result chan_pjsip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
  178. {
  179. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
  180. struct chan_pjsip_pvt *pvt = channel->pvt;
  181. struct ast_sip_endpoint *endpoint;
  182. if (!pvt || !channel->session || !pvt->media[SIP_MEDIA_VIDEO]->rtp) {
  183. return AST_RTP_GLUE_RESULT_FORBID;
  184. }
  185. endpoint = channel->session->endpoint;
  186. *instance = pvt->media[SIP_MEDIA_VIDEO]->rtp;
  187. ao2_ref(*instance, +1);
  188. ast_assert(endpoint != NULL);
  189. if (endpoint->media.rtp.encryption != AST_SIP_MEDIA_ENCRYPT_NONE) {
  190. return AST_RTP_GLUE_RESULT_FORBID;
  191. }
  192. return AST_RTP_GLUE_RESULT_LOCAL;
  193. }
  194. /*! \brief Function called by RTP engine to get peer capabilities */
  195. static void chan_pjsip_get_codec(struct ast_channel *chan, struct ast_format_cap *result)
  196. {
  197. ast_format_cap_append_from_cap(result, ast_channel_nativeformats(chan), AST_MEDIA_TYPE_UNKNOWN);
  198. }
  199. /*! \brief Destructor function for \ref transport_info_data */
  200. static void transport_info_destroy(void *obj)
  201. {
  202. struct transport_info_data *data = obj;
  203. ast_free(data);
  204. }
  205. /*! \brief Datastore used to store local/remote addresses for the
  206. * INVITE request that created the PJSIP channel */
  207. static struct ast_datastore_info transport_info = {
  208. .type = "chan_pjsip_transport_info",
  209. .destroy = transport_info_destroy,
  210. };
  211. static struct ast_datastore_info direct_media_mitigation_info = { };
  212. static int direct_media_mitigate_glare(struct ast_sip_session *session)
  213. {
  214. RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
  215. if (session->endpoint->media.direct_media.glare_mitigation ==
  216. AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE) {
  217. return 0;
  218. }
  219. datastore = ast_sip_session_get_datastore(session, "direct_media_glare_mitigation");
  220. if (!datastore) {
  221. return 0;
  222. }
  223. /* Removing the datastore ensures we won't try to mitigate glare on subsequent reinvites */
  224. ast_sip_session_remove_datastore(session, "direct_media_glare_mitigation");
  225. if ((session->endpoint->media.direct_media.glare_mitigation ==
  226. AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_OUTGOING &&
  227. session->inv_session->role == PJSIP_ROLE_UAC) ||
  228. (session->endpoint->media.direct_media.glare_mitigation ==
  229. AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_INCOMING &&
  230. session->inv_session->role == PJSIP_ROLE_UAS)) {
  231. return 1;
  232. }
  233. return 0;
  234. }
  235. /*!
  236. * \pre chan is locked
  237. */
  238. static int check_for_rtp_changes(struct ast_channel *chan, struct ast_rtp_instance *rtp,
  239. struct ast_sip_session_media *media, int rtcp_fd)
  240. {
  241. int changed = 0;
  242. if (rtp) {
  243. changed = ast_rtp_instance_get_and_cmp_remote_address(rtp, &media->direct_media_addr);
  244. if (media->rtp) {
  245. ast_channel_set_fd(chan, rtcp_fd, -1);
  246. ast_rtp_instance_set_prop(media->rtp, AST_RTP_PROPERTY_RTCP, 0);
  247. }
  248. } else if (!ast_sockaddr_isnull(&media->direct_media_addr)){
  249. ast_sockaddr_setnull(&media->direct_media_addr);
  250. changed = 1;
  251. if (media->rtp) {
  252. ast_rtp_instance_set_prop(media->rtp, AST_RTP_PROPERTY_RTCP, 1);
  253. ast_channel_set_fd(chan, rtcp_fd, ast_rtp_instance_fd(media->rtp, 1));
  254. }
  255. }
  256. return changed;
  257. }
  258. struct rtp_direct_media_data {
  259. struct ast_channel *chan;
  260. struct ast_rtp_instance *rtp;
  261. struct ast_rtp_instance *vrtp;
  262. struct ast_format_cap *cap;
  263. struct ast_sip_session *session;
  264. };
  265. static void rtp_direct_media_data_destroy(void *data)
  266. {
  267. struct rtp_direct_media_data *cdata = data;
  268. ao2_cleanup(cdata->session);
  269. ao2_cleanup(cdata->cap);
  270. ao2_cleanup(cdata->vrtp);
  271. ao2_cleanup(cdata->rtp);
  272. ao2_cleanup(cdata->chan);
  273. }
  274. static struct rtp_direct_media_data *rtp_direct_media_data_create(
  275. struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp,
  276. const struct ast_format_cap *cap, struct ast_sip_session *session)
  277. {
  278. struct rtp_direct_media_data *cdata = ao2_alloc(sizeof(*cdata), rtp_direct_media_data_destroy);
  279. if (!cdata) {
  280. return NULL;
  281. }
  282. cdata->chan = ao2_bump(chan);
  283. cdata->rtp = ao2_bump(rtp);
  284. cdata->vrtp = ao2_bump(vrtp);
  285. cdata->cap = ao2_bump((struct ast_format_cap *)cap);
  286. cdata->session = ao2_bump(session);
  287. return cdata;
  288. }
  289. static int send_direct_media_request(void *data)
  290. {
  291. struct rtp_direct_media_data *cdata = data;
  292. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(cdata->chan);
  293. struct chan_pjsip_pvt *pvt = channel->pvt;
  294. int changed = 0;
  295. int res = 0;
  296. /* The channel needs to be locked when checking for RTP changes.
  297. * Otherwise, we could end up destroying an underlying RTCP structure
  298. * at the same time that the channel thread is attempting to read RTCP
  299. */
  300. ast_channel_lock(cdata->chan);
  301. if (pvt->media[SIP_MEDIA_AUDIO]) {
  302. changed |= check_for_rtp_changes(
  303. cdata->chan, cdata->rtp, pvt->media[SIP_MEDIA_AUDIO], 1);
  304. }
  305. if (pvt->media[SIP_MEDIA_VIDEO]) {
  306. changed |= check_for_rtp_changes(
  307. cdata->chan, cdata->vrtp, pvt->media[SIP_MEDIA_VIDEO], 3);
  308. }
  309. ast_channel_unlock(cdata->chan);
  310. if (direct_media_mitigate_glare(cdata->session)) {
  311. ast_debug(4, "Disregarding setting RTP on %s: mitigating re-INVITE glare\n", ast_channel_name(cdata->chan));
  312. ao2_ref(cdata, -1);
  313. return 0;
  314. }
  315. if (cdata->cap && ast_format_cap_count(cdata->cap) &&
  316. !ast_format_cap_identical(cdata->session->direct_media_cap, cdata->cap)) {
  317. ast_format_cap_remove_by_type(cdata->session->direct_media_cap, AST_MEDIA_TYPE_UNKNOWN);
  318. ast_format_cap_append_from_cap(cdata->session->direct_media_cap, cdata->cap, AST_MEDIA_TYPE_UNKNOWN);
  319. changed = 1;
  320. }
  321. if (changed) {
  322. ast_debug(4, "RTP changed on %s; initiating direct media update\n", ast_channel_name(cdata->chan));
  323. res = ast_sip_session_refresh(cdata->session, NULL, NULL, NULL,
  324. cdata->session->endpoint->media.direct_media.method, 1);
  325. }
  326. ao2_ref(cdata, -1);
  327. return res;
  328. }
  329. /*! \brief Function called by RTP engine to change where the remote party should send media */
  330. static int chan_pjsip_set_rtp_peer(struct ast_channel *chan,
  331. struct ast_rtp_instance *rtp,
  332. struct ast_rtp_instance *vrtp,
  333. struct ast_rtp_instance *tpeer,
  334. const struct ast_format_cap *cap,
  335. int nat_active)
  336. {
  337. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
  338. struct ast_sip_session *session = channel->session;
  339. struct rtp_direct_media_data *cdata;
  340. /* Don't try to do any direct media shenanigans on early bridges */
  341. if ((rtp || vrtp || tpeer) && !ast_channel_is_bridged(chan)) {
  342. ast_debug(4, "Disregarding setting RTP on %s: channel is not bridged\n", ast_channel_name(chan));
  343. return 0;
  344. }
  345. if (nat_active && session->endpoint->media.direct_media.disable_on_nat) {
  346. ast_debug(4, "Disregarding setting RTP on %s: NAT is active\n", ast_channel_name(chan));
  347. return 0;
  348. }
  349. cdata = rtp_direct_media_data_create(chan, rtp, vrtp, cap, session);
  350. if (!cdata) {
  351. return 0;
  352. }
  353. if (ast_sip_push_task(session->serializer, send_direct_media_request, cdata)) {
  354. ast_log(LOG_ERROR, "Unable to send direct media request for channel %s\n", ast_channel_name(chan));
  355. ao2_ref(cdata, -1);
  356. }
  357. return 0;
  358. }
  359. /*! \brief Local glue for interacting with the RTP engine core */
  360. static struct ast_rtp_glue chan_pjsip_rtp_glue = {
  361. .type = "PJSIP",
  362. .get_rtp_info = chan_pjsip_get_rtp_peer,
  363. .get_vrtp_info = chan_pjsip_get_vrtp_peer,
  364. .get_codec = chan_pjsip_get_codec,
  365. .update_peer = chan_pjsip_set_rtp_peer,
  366. };
  367. static void set_channel_on_rtp_instance(struct chan_pjsip_pvt *pvt, const char *channel_id)
  368. {
  369. if (pvt->media[SIP_MEDIA_AUDIO] && pvt->media[SIP_MEDIA_AUDIO]->rtp) {
  370. ast_rtp_instance_set_channel_id(pvt->media[SIP_MEDIA_AUDIO]->rtp, channel_id);
  371. }
  372. if (pvt->media[SIP_MEDIA_VIDEO] && pvt->media[SIP_MEDIA_VIDEO]->rtp) {
  373. ast_rtp_instance_set_channel_id(pvt->media[SIP_MEDIA_VIDEO]->rtp, channel_id);
  374. }
  375. }
  376. /*! \brief Function called to create a new PJSIP Asterisk channel */
  377. static struct ast_channel *chan_pjsip_new(struct ast_sip_session *session, int state, const char *exten, const char *title, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *cid_name)
  378. {
  379. struct ast_channel *chan;
  380. struct ast_format_cap *caps;
  381. RAII_VAR(struct chan_pjsip_pvt *, pvt, NULL, ao2_cleanup);
  382. struct ast_sip_channel_pvt *channel;
  383. struct ast_variable *var;
  384. if (!(pvt = ao2_alloc(sizeof(*pvt), chan_pjsip_pvt_dtor))) {
  385. return NULL;
  386. }
  387. caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  388. if (!caps) {
  389. return NULL;
  390. }
  391. chan = ast_channel_alloc_with_endpoint(1, state,
  392. S_COR(session->id.number.valid, session->id.number.str, ""),
  393. S_COR(session->id.name.valid, session->id.name.str, ""),
  394. session->endpoint->accountcode,
  395. exten, session->endpoint->context,
  396. assignedids, requestor, 0,
  397. session->endpoint->persistent, "PJSIP/%s-%08x",
  398. ast_sorcery_object_get_id(session->endpoint),
  399. (unsigned) ast_atomic_fetchadd_int((int *) &chan_idx, +1));
  400. if (!chan) {
  401. ao2_ref(caps, -1);
  402. return NULL;
  403. }
  404. ast_channel_tech_set(chan, &chan_pjsip_tech);
  405. if (!(channel = ast_sip_channel_pvt_alloc(pvt, session))) {
  406. ao2_ref(caps, -1);
  407. ast_channel_unlock(chan);
  408. ast_hangup(chan);
  409. return NULL;
  410. }
  411. ast_channel_stage_snapshot(chan);
  412. ast_channel_tech_pvt_set(chan, channel);
  413. if (!ast_format_cap_count(session->req_caps) ||
  414. !ast_format_cap_iscompatible(session->req_caps, session->endpoint->media.codecs)) {
  415. ast_format_cap_append_from_cap(caps, session->endpoint->media.codecs, AST_MEDIA_TYPE_UNKNOWN);
  416. } else {
  417. ast_format_cap_append_from_cap(caps, session->req_caps, AST_MEDIA_TYPE_UNKNOWN);
  418. }
  419. ast_channel_nativeformats_set(chan, caps);
  420. if (!ast_format_cap_empty(caps)) {
  421. struct ast_format *fmt;
  422. fmt = ast_format_cap_get_best_by_type(caps, AST_MEDIA_TYPE_AUDIO);
  423. if (!fmt) {
  424. /* Since our capabilities aren't empty, this will succeed */
  425. fmt = ast_format_cap_get_format(caps, 0);
  426. }
  427. ast_channel_set_writeformat(chan, fmt);
  428. ast_channel_set_rawwriteformat(chan, fmt);
  429. ast_channel_set_readformat(chan, fmt);
  430. ast_channel_set_rawreadformat(chan, fmt);
  431. ao2_ref(fmt, -1);
  432. }
  433. ao2_ref(caps, -1);
  434. if (state == AST_STATE_RING) {
  435. ast_channel_rings_set(chan, 1);
  436. }
  437. ast_channel_adsicpe_set(chan, AST_ADSI_UNAVAILABLE);
  438. ast_party_id_copy(&ast_channel_caller(chan)->id, &session->id);
  439. ast_party_id_copy(&ast_channel_caller(chan)->ani, &session->id);
  440. if (!ast_strlen_zero(exten)) {
  441. /* Set provided DNID on the new channel. */
  442. ast_channel_dialed(chan)->number.str = ast_strdup(exten);
  443. }
  444. ast_channel_priority_set(chan, 1);
  445. ast_channel_callgroup_set(chan, session->endpoint->pickup.callgroup);
  446. ast_channel_pickupgroup_set(chan, session->endpoint->pickup.pickupgroup);
  447. ast_channel_named_callgroups_set(chan, session->endpoint->pickup.named_callgroups);
  448. ast_channel_named_pickupgroups_set(chan, session->endpoint->pickup.named_pickupgroups);
  449. if (!ast_strlen_zero(session->endpoint->language)) {
  450. ast_channel_language_set(chan, session->endpoint->language);
  451. }
  452. if (!ast_strlen_zero(session->endpoint->zone)) {
  453. struct ast_tone_zone *zone = ast_get_indication_zone(session->endpoint->zone);
  454. if (!zone) {
  455. ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", session->endpoint->zone);
  456. }
  457. ast_channel_zone_set(chan, zone);
  458. }
  459. for (var = session->endpoint->channel_vars; var; var = var->next) {
  460. char buf[512];
  461. pbx_builtin_setvar_helper(chan, var->name, ast_get_encoded_str(
  462. var->value, buf, sizeof(buf)));
  463. }
  464. ast_channel_stage_snapshot_done(chan);
  465. ast_channel_unlock(chan);
  466. /* If res_pjsip_session is ever updated to create/destroy ast_sip_session_media
  467. * during a call such as if multiple same-type stream support is introduced,
  468. * these will need to be recaptured as well */
  469. pvt->media[SIP_MEDIA_AUDIO] = ao2_find(session->media, "audio", OBJ_KEY);
  470. pvt->media[SIP_MEDIA_VIDEO] = ao2_find(session->media, "video", OBJ_KEY);
  471. set_channel_on_rtp_instance(pvt, ast_channel_uniqueid(chan));
  472. return chan;
  473. }
  474. static int answer(void *data)
  475. {
  476. pj_status_t status = PJ_SUCCESS;
  477. pjsip_tx_data *packet = NULL;
  478. struct ast_sip_session *session = data;
  479. if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
  480. ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
  481. session->inv_session->cause,
  482. pjsip_get_status_text(session->inv_session->cause)->ptr);
  483. #ifdef HAVE_PJSIP_INV_SESSION_REF
  484. pjsip_inv_dec_ref(session->inv_session);
  485. #endif
  486. return 0;
  487. }
  488. pjsip_dlg_inc_lock(session->inv_session->dlg);
  489. if (session->inv_session->invite_tsx) {
  490. status = pjsip_inv_answer(session->inv_session, 200, NULL, NULL, &packet);
  491. } else {
  492. ast_log(LOG_ERROR,"Cannot answer '%s' because there is no associated SIP transaction\n",
  493. ast_channel_name(session->channel));
  494. }
  495. pjsip_dlg_dec_lock(session->inv_session->dlg);
  496. if (status == PJ_SUCCESS && packet) {
  497. ast_sip_session_send_response(session, packet);
  498. }
  499. #ifdef HAVE_PJSIP_INV_SESSION_REF
  500. pjsip_inv_dec_ref(session->inv_session);
  501. #endif
  502. if (status != PJ_SUCCESS) {
  503. char err[PJ_ERR_MSG_SIZE];
  504. pj_strerror(status, err, sizeof(err));
  505. ast_log(LOG_WARNING,"Cannot answer '%s': %s\n",
  506. ast_channel_name(session->channel), err);
  507. /*
  508. * Return this value so we can distinguish between this
  509. * failure and the threadpool synchronous push failing.
  510. */
  511. return -2;
  512. }
  513. return 0;
  514. }
  515. /*! \brief Function called by core when we should answer a PJSIP session */
  516. static int chan_pjsip_answer(struct ast_channel *ast)
  517. {
  518. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
  519. struct ast_sip_session *session;
  520. int res;
  521. if (ast_channel_state(ast) == AST_STATE_UP) {
  522. return 0;
  523. }
  524. ast_setstate(ast, AST_STATE_UP);
  525. session = ao2_bump(channel->session);
  526. #ifdef HAVE_PJSIP_INV_SESSION_REF
  527. if (pjsip_inv_add_ref(session->inv_session) != PJ_SUCCESS) {
  528. ast_log(LOG_ERROR, "Can't increase the session reference counter\n");
  529. ao2_ref(session, -1);
  530. return -1;
  531. }
  532. #endif
  533. /* the answer task needs to be pushed synchronously otherwise a race condition
  534. can occur between this thread and bridging (specifically when native bridging
  535. attempts to do direct media) */
  536. ast_channel_unlock(ast);
  537. res = ast_sip_push_task_wait_serializer(session->serializer, answer, session);
  538. if (res) {
  539. if (res == -1) {
  540. ast_log(LOG_ERROR,"Cannot answer '%s': Unable to push answer task to the threadpool.\n",
  541. ast_channel_name(session->channel));
  542. #ifdef HAVE_PJSIP_INV_SESSION_REF
  543. pjsip_inv_dec_ref(session->inv_session);
  544. #endif
  545. }
  546. ao2_ref(session, -1);
  547. ast_channel_lock(ast);
  548. return -1;
  549. }
  550. ao2_ref(session, -1);
  551. ast_channel_lock(ast);
  552. return 0;
  553. }
  554. /*! \brief Internal helper function called when CNG tone is detected */
  555. static struct ast_frame *chan_pjsip_cng_tone_detected(struct ast_sip_session *session, struct ast_frame *f)
  556. {
  557. const char *target_context;
  558. int exists;
  559. int dsp_features;
  560. dsp_features = ast_dsp_get_features(session->dsp);
  561. dsp_features &= ~DSP_FEATURE_FAX_DETECT;
  562. if (dsp_features) {
  563. ast_dsp_set_features(session->dsp, dsp_features);
  564. } else {
  565. ast_dsp_free(session->dsp);
  566. session->dsp = NULL;
  567. }
  568. /* If already executing in the fax extension don't do anything */
  569. if (!strcmp(ast_channel_exten(session->channel), "fax")) {
  570. return f;
  571. }
  572. target_context = S_OR(ast_channel_macrocontext(session->channel), ast_channel_context(session->channel));
  573. /*
  574. * We need to unlock the channel here because ast_exists_extension has the
  575. * potential to start and stop an autoservice on the channel. Such action
  576. * is prone to deadlock if the channel is locked.
  577. *
  578. * ast_async_goto() has its own restriction on not holding the channel lock.
  579. */
  580. ast_channel_unlock(session->channel);
  581. ast_frfree(f);
  582. f = &ast_null_frame;
  583. exists = ast_exists_extension(session->channel, target_context, "fax", 1,
  584. S_COR(ast_channel_caller(session->channel)->id.number.valid,
  585. ast_channel_caller(session->channel)->id.number.str, NULL));
  586. if (exists) {
  587. ast_verb(2, "Redirecting '%s' to fax extension due to CNG detection\n",
  588. ast_channel_name(session->channel));
  589. pbx_builtin_setvar_helper(session->channel, "FAXEXTEN", ast_channel_exten(session->channel));
  590. if (ast_async_goto(session->channel, target_context, "fax", 1)) {
  591. ast_log(LOG_ERROR, "Failed to async goto '%s' into fax extension in '%s'\n",
  592. ast_channel_name(session->channel), target_context);
  593. }
  594. } else {
  595. ast_log(LOG_NOTICE, "FAX CNG detected on '%s' but no fax extension in '%s'\n",
  596. ast_channel_name(session->channel), target_context);
  597. }
  598. ast_channel_lock(session->channel);
  599. return f;
  600. }
  601. /*!
  602. * \brief Function called by core to read any waiting frames
  603. *
  604. * \note The channel is already locked.
  605. */
  606. static struct ast_frame *chan_pjsip_read(struct ast_channel *ast)
  607. {
  608. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
  609. struct ast_sip_session *session;
  610. struct chan_pjsip_pvt *pvt = channel->pvt;
  611. struct ast_frame *f;
  612. struct ast_sip_session_media *media = NULL;
  613. int rtcp = 0;
  614. int fdno = ast_channel_fdno(ast);
  615. switch (fdno) {
  616. case 0:
  617. media = pvt->media[SIP_MEDIA_AUDIO];
  618. break;
  619. case 1:
  620. media = pvt->media[SIP_MEDIA_AUDIO];
  621. rtcp = 1;
  622. break;
  623. case 2:
  624. media = pvt->media[SIP_MEDIA_VIDEO];
  625. break;
  626. case 3:
  627. media = pvt->media[SIP_MEDIA_VIDEO];
  628. rtcp = 1;
  629. break;
  630. }
  631. if (!media || !media->rtp) {
  632. return &ast_null_frame;
  633. }
  634. if (!(f = ast_rtp_instance_read(media->rtp, rtcp))) {
  635. return f;
  636. }
  637. ast_rtp_instance_set_last_rx(media->rtp, time(NULL));
  638. if (f->frametype != AST_FRAME_VOICE) {
  639. return f;
  640. }
  641. session = channel->session;
  642. /*
  643. * Asymmetric RTP only has one native format set at a time.
  644. * Therefore we need to update the native format to the current
  645. * raw read format BEFORE the native format check
  646. */
  647. if (!session->endpoint->asymmetric_rtp_codec &&
  648. ast_format_cmp(ast_channel_rawwriteformat(ast), f->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
  649. struct ast_format_cap *caps;
  650. /* For maximum compatibility we ensure that the formats match that of the received media */
  651. ast_debug(1, "Oooh, got a frame with format of %s on channel '%s' when we're sending '%s', switching to match\n",
  652. ast_format_get_name(f->subclass.format), ast_channel_name(ast),
  653. ast_format_get_name(ast_channel_rawwriteformat(ast)));
  654. caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  655. if (caps) {
  656. ast_format_cap_append_from_cap(caps, ast_channel_nativeformats(ast), AST_MEDIA_TYPE_UNKNOWN);
  657. ast_format_cap_remove_by_type(caps, AST_MEDIA_TYPE_AUDIO);
  658. ast_format_cap_append(caps, f->subclass.format, 0);
  659. ast_channel_nativeformats_set(ast, caps);
  660. ao2_ref(caps, -1);
  661. }
  662. ast_set_write_format_path(ast, ast_channel_writeformat(ast), f->subclass.format);
  663. ast_set_read_format_path(ast, ast_channel_readformat(ast), f->subclass.format);
  664. if (ast_channel_is_bridged(ast)) {
  665. ast_channel_set_unbridged_nolock(ast, 1);
  666. }
  667. }
  668. if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), f->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
  669. ast_debug(1, "Oooh, got a frame with format of %s on channel '%s' when it has not been negotiated\n",
  670. ast_format_get_name(f->subclass.format), ast_channel_name(ast));
  671. ast_frfree(f);
  672. return &ast_null_frame;
  673. }
  674. if (session->dsp) {
  675. int dsp_features;
  676. dsp_features = ast_dsp_get_features(session->dsp);
  677. if ((dsp_features & DSP_FEATURE_FAX_DETECT)
  678. && session->endpoint->faxdetect_timeout
  679. && session->endpoint->faxdetect_timeout <= ast_channel_get_up_time(ast)) {
  680. dsp_features &= ~DSP_FEATURE_FAX_DETECT;
  681. if (dsp_features) {
  682. ast_dsp_set_features(session->dsp, dsp_features);
  683. } else {
  684. ast_dsp_free(session->dsp);
  685. session->dsp = NULL;
  686. }
  687. ast_debug(3, "Channel driver fax CNG detection timeout on %s\n",
  688. ast_channel_name(ast));
  689. }
  690. }
  691. if (session->dsp) {
  692. f = ast_dsp_process(ast, session->dsp, f);
  693. if (f && (f->frametype == AST_FRAME_DTMF)) {
  694. if (f->subclass.integer == 'f') {
  695. ast_debug(3, "Channel driver fax CNG detected on %s\n",
  696. ast_channel_name(ast));
  697. f = chan_pjsip_cng_tone_detected(session, f);
  698. } else {
  699. ast_debug(3, "* Detected inband DTMF '%c' on '%s'\n", f->subclass.integer,
  700. ast_channel_name(ast));
  701. }
  702. }
  703. }
  704. return f;
  705. }
  706. /*! \brief Function called by core to write frames */
  707. static int chan_pjsip_write(struct ast_channel *ast, struct ast_frame *frame)
  708. {
  709. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
  710. struct chan_pjsip_pvt *pvt = channel->pvt;
  711. struct ast_sip_session_media *media;
  712. int res = 0;
  713. switch (frame->frametype) {
  714. case AST_FRAME_VOICE:
  715. media = pvt->media[SIP_MEDIA_AUDIO];
  716. if (!media) {
  717. return 0;
  718. }
  719. if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), frame->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
  720. struct ast_str *cap_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  721. struct ast_str *write_transpath = ast_str_alloca(256);
  722. struct ast_str *read_transpath = ast_str_alloca(256);
  723. ast_log(LOG_WARNING,
  724. "Channel %s asked to send %s frame when native formats are %s (rd:%s->%s;%s wr:%s->%s;%s)\n",
  725. ast_channel_name(ast),
  726. ast_format_get_name(frame->subclass.format),
  727. ast_format_cap_get_names(ast_channel_nativeformats(ast), &cap_buf),
  728. ast_format_get_name(ast_channel_rawreadformat(ast)),
  729. ast_format_get_name(ast_channel_readformat(ast)),
  730. ast_translate_path_to_str(ast_channel_readtrans(ast), &read_transpath),
  731. ast_format_get_name(ast_channel_writeformat(ast)),
  732. ast_format_get_name(ast_channel_rawwriteformat(ast)),
  733. ast_translate_path_to_str(ast_channel_writetrans(ast), &write_transpath));
  734. return 0;
  735. }
  736. if (media->rtp) {
  737. res = ast_rtp_instance_write(media->rtp, frame);
  738. }
  739. break;
  740. case AST_FRAME_VIDEO:
  741. if ((media = pvt->media[SIP_MEDIA_VIDEO]) && media->rtp) {
  742. res = ast_rtp_instance_write(media->rtp, frame);
  743. }
  744. break;
  745. case AST_FRAME_MODEM:
  746. break;
  747. case AST_FRAME_CNG:
  748. break;
  749. default:
  750. ast_log(LOG_WARNING, "Can't send %u type frames with PJSIP\n", frame->frametype);
  751. break;
  752. }
  753. return res;
  754. }
  755. /*! \brief Function called by core to change the underlying owner channel */
  756. static int chan_pjsip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  757. {
  758. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(newchan);
  759. struct chan_pjsip_pvt *pvt = channel->pvt;
  760. if (channel->session->channel != oldchan) {
  761. return -1;
  762. }
  763. /*
  764. * The masquerade has suspended the channel's session
  765. * serializer so we can safely change it outside of
  766. * the serializer thread.
  767. */
  768. channel->session->channel = newchan;
  769. set_channel_on_rtp_instance(pvt, ast_channel_uniqueid(newchan));
  770. return 0;
  771. }
  772. /*! AO2 hash function for on hold UIDs */
  773. static int uid_hold_hash_fn(const void *obj, const int flags)
  774. {
  775. const char *key = obj;
  776. switch (flags & OBJ_SEARCH_MASK) {
  777. case OBJ_SEARCH_KEY:
  778. break;
  779. case OBJ_SEARCH_OBJECT:
  780. break;
  781. default:
  782. /* Hash can only work on something with a full key. */
  783. ast_assert(0);
  784. return 0;
  785. }
  786. return ast_str_hash(key);
  787. }
  788. /*! AO2 sort function for on hold UIDs */
  789. static int uid_hold_sort_fn(const void *obj_left, const void *obj_right, const int flags)
  790. {
  791. const char *left = obj_left;
  792. const char *right = obj_right;
  793. int cmp;
  794. switch (flags & OBJ_SEARCH_MASK) {
  795. case OBJ_SEARCH_OBJECT:
  796. case OBJ_SEARCH_KEY:
  797. cmp = strcmp(left, right);
  798. break;
  799. case OBJ_SEARCH_PARTIAL_KEY:
  800. cmp = strncmp(left, right, strlen(right));
  801. break;
  802. default:
  803. /* Sort can only work on something with a full or partial key. */
  804. ast_assert(0);
  805. cmp = 0;
  806. break;
  807. }
  808. return cmp;
  809. }
  810. static struct ao2_container *pjsip_uids_onhold;
  811. /*!
  812. * \brief Add a channel ID to the list of PJSIP channels on hold
  813. *
  814. * \param chan_uid - Unique ID of the channel being put into the hold list
  815. *
  816. * \retval 0 Channel has been added to or was already in the hold list
  817. * \retval -1 Failed to add channel to the hold list
  818. */
  819. static int chan_pjsip_add_hold(const char *chan_uid)
  820. {
  821. RAII_VAR(char *, hold_uid, NULL, ao2_cleanup);
  822. hold_uid = ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY);
  823. if (hold_uid) {
  824. /* Device is already on hold. Nothing to do. */
  825. return 0;
  826. }
  827. /* Device wasn't in hold list already. Create a new one. */
  828. hold_uid = ao2_alloc_options(strlen(chan_uid) + 1, NULL,
  829. AO2_ALLOC_OPT_LOCK_NOLOCK);
  830. if (!hold_uid) {
  831. return -1;
  832. }
  833. ast_copy_string(hold_uid, chan_uid, strlen(chan_uid) + 1);
  834. if (ao2_link(pjsip_uids_onhold, hold_uid) == 0) {
  835. return -1;
  836. }
  837. return 0;
  838. }
  839. /*!
  840. * \brief Remove a channel ID from the list of PJSIP channels on hold
  841. *
  842. * \param chan_uid - Unique ID of the channel being taken out of the hold list
  843. */
  844. static void chan_pjsip_remove_hold(const char *chan_uid)
  845. {
  846. ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
  847. }
  848. /*!
  849. * \brief Determine whether a channel ID is in the list of PJSIP channels on hold
  850. *
  851. * \param chan_uid - Channel being checked
  852. *
  853. * \retval 0 The channel is not in the hold list
  854. * \retval 1 The channel is in the hold list
  855. */
  856. static int chan_pjsip_get_hold(const char *chan_uid)
  857. {
  858. RAII_VAR(char *, hold_uid, NULL, ao2_cleanup);
  859. hold_uid = ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY);
  860. if (!hold_uid) {
  861. return 0;
  862. }
  863. return 1;
  864. }
  865. /*! \brief Function called to get the device state of an endpoint */
  866. static int chan_pjsip_devicestate(const char *data)
  867. {
  868. RAII_VAR(struct ast_sip_endpoint *, endpoint, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", data), ao2_cleanup);
  869. enum ast_device_state state = AST_DEVICE_UNKNOWN;
  870. RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot, NULL, ao2_cleanup);
  871. RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
  872. struct ast_devstate_aggregate aggregate;
  873. int num, inuse = 0;
  874. if (!endpoint) {
  875. return AST_DEVICE_INVALID;
  876. }
  877. endpoint_snapshot = ast_endpoint_latest_snapshot(ast_endpoint_get_tech(endpoint->persistent),
  878. ast_endpoint_get_resource(endpoint->persistent));
  879. if (!endpoint_snapshot) {
  880. return AST_DEVICE_INVALID;
  881. }
  882. if (endpoint_snapshot->state == AST_ENDPOINT_OFFLINE) {
  883. state = AST_DEVICE_UNAVAILABLE;
  884. } else if (endpoint_snapshot->state == AST_ENDPOINT_ONLINE) {
  885. state = AST_DEVICE_NOT_INUSE;
  886. }
  887. if (!endpoint_snapshot->num_channels || !(cache = ast_channel_cache())) {
  888. return state;
  889. }
  890. ast_devstate_aggregate_init(&aggregate);
  891. ao2_ref(cache, +1);
  892. for (num = 0; num < endpoint_snapshot->num_channels; num++) {
  893. RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
  894. struct ast_channel_snapshot *snapshot;
  895. msg = stasis_cache_get(cache, ast_channel_snapshot_type(),
  896. endpoint_snapshot->channel_ids[num]);
  897. if (!msg) {
  898. continue;
  899. }
  900. snapshot = stasis_message_data(msg);
  901. if (chan_pjsip_get_hold(snapshot->uniqueid)) {
  902. ast_devstate_aggregate_add(&aggregate, AST_DEVICE_ONHOLD);
  903. } else {
  904. ast_devstate_aggregate_add(&aggregate, ast_state_chan2dev(snapshot->state));
  905. }
  906. if ((snapshot->state == AST_STATE_UP) || (snapshot->state == AST_STATE_RING) ||
  907. (snapshot->state == AST_STATE_BUSY)) {
  908. inuse++;
  909. }
  910. }
  911. if (endpoint->devicestate_busy_at && (inuse == endpoint->devicestate_busy_at)) {
  912. state = AST_DEVICE_BUSY;
  913. } else if (ast_devstate_aggregate_result(&aggregate) != AST_DEVICE_INVALID) {
  914. state = ast_devstate_aggregate_result(&aggregate);
  915. }
  916. return state;
  917. }
  918. /*! \brief Function called to query options on a channel */
  919. static int chan_pjsip_queryoption(struct ast_channel *ast, int option, void *data, int *datalen)
  920. {
  921. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
  922. struct ast_sip_session *session = channel->session;
  923. int res = -1;
  924. enum ast_t38_state state = T38_STATE_UNAVAILABLE;
  925. switch (option) {
  926. case AST_OPTION_T38_STATE:
  927. if (session->endpoint->media.t38.enabled) {
  928. switch (session->t38state) {
  929. case T38_LOCAL_REINVITE:
  930. case T38_PEER_REINVITE:
  931. state = T38_STATE_NEGOTIATING;
  932. break;
  933. case T38_ENABLED:
  934. state = T38_STATE_NEGOTIATED;
  935. break;
  936. case T38_REJECTED:
  937. state = T38_STATE_REJECTED;
  938. break;
  939. default:
  940. state = T38_STATE_UNKNOWN;
  941. break;
  942. }
  943. }
  944. *((enum ast_t38_state *) data) = state;
  945. res = 0;
  946. break;
  947. default:
  948. break;
  949. }
  950. return res;
  951. }
  952. static const char *chan_pjsip_get_uniqueid(struct ast_channel *ast)
  953. {
  954. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
  955. char *uniqueid = ast_threadstorage_get(&uniqueid_threadbuf, UNIQUEID_BUFSIZE);
  956. if (!uniqueid) {
  957. return "";
  958. }
  959. ast_copy_pj_str(uniqueid, &channel->session->inv_session->dlg->call_id->id, UNIQUEID_BUFSIZE);
  960. return uniqueid;
  961. }
  962. struct indicate_data {
  963. struct ast_sip_session *session;
  964. int condition;
  965. int response_code;
  966. void *frame_data;
  967. size_t datalen;
  968. };
  969. static void indicate_data_destroy(void *obj)
  970. {
  971. struct indicate_data *ind_data = obj;
  972. ast_free(ind_data->frame_data);
  973. ao2_ref(ind_data->session, -1);
  974. }
  975. static struct indicate_data *indicate_data_alloc(struct ast_sip_session *session,
  976. int condition, int response_code, const void *frame_data, size_t datalen)
  977. {
  978. struct indicate_data *ind_data = ao2_alloc(sizeof(*ind_data), indicate_data_destroy);
  979. if (!ind_data) {
  980. return NULL;
  981. }
  982. ind_data->frame_data = ast_malloc(datalen);
  983. if (!ind_data->frame_data) {
  984. ao2_ref(ind_data, -1);
  985. return NULL;
  986. }
  987. memcpy(ind_data->frame_data, frame_data, datalen);
  988. ind_data->datalen = datalen;
  989. ind_data->condition = condition;
  990. ind_data->response_code = response_code;
  991. ao2_ref(session, +1);
  992. ind_data->session = session;
  993. return ind_data;
  994. }
  995. static int indicate(void *data)
  996. {
  997. pjsip_tx_data *packet = NULL;
  998. struct indicate_data *ind_data = data;
  999. struct ast_sip_session *session = ind_data->session;
  1000. int response_code = ind_data->response_code;
  1001. if ((session->inv_session->state != PJSIP_INV_STATE_DISCONNECTED) &&
  1002. (pjsip_inv_answer(session->inv_session, response_code, NULL, NULL, &packet) == PJ_SUCCESS)) {
  1003. ast_sip_session_send_response(session, packet);
  1004. }
  1005. #ifdef HAVE_PJSIP_INV_SESSION_REF
  1006. pjsip_inv_dec_ref(session->inv_session);
  1007. #endif
  1008. ao2_ref(ind_data, -1);
  1009. return 0;
  1010. }
  1011. /*! \brief Send SIP INFO with video update request */
  1012. static int transmit_info_with_vidupdate(void *data)
  1013. {
  1014. const char * xml =
  1015. "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n"
  1016. " <media_control>\r\n"
  1017. " <vc_primitive>\r\n"
  1018. " <to_encoder>\r\n"
  1019. " <picture_fast_update/>\r\n"
  1020. " </to_encoder>\r\n"
  1021. " </vc_primitive>\r\n"
  1022. " </media_control>\r\n";
  1023. const struct ast_sip_body body = {
  1024. .type = "application",
  1025. .subtype = "media_control+xml",
  1026. .body_text = xml
  1027. };
  1028. RAII_VAR(struct ast_sip_session *, session, data, ao2_cleanup);
  1029. struct pjsip_tx_data *tdata;
  1030. if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
  1031. ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
  1032. session->inv_session->cause,
  1033. pjsip_get_status_text(session->inv_session->cause)->ptr);
  1034. goto failure;
  1035. }
  1036. if (ast_sip_create_request("INFO", session->inv_session->dlg, session->endpoint, NULL, NULL, &tdata)) {
  1037. ast_log(LOG_ERROR, "Could not create text video update INFO request\n");
  1038. goto failure;
  1039. }
  1040. if (ast_sip_add_body(tdata, &body)) {
  1041. ast_log(LOG_ERROR, "Could not add body to text video update INFO request\n");
  1042. goto failure;
  1043. }
  1044. ast_sip_session_send_request(session, tdata);
  1045. #ifdef HAVE_PJSIP_INV_SESSION_REF
  1046. pjsip_inv_dec_ref(session->inv_session);
  1047. #endif
  1048. return 0;
  1049. failure:
  1050. #ifdef HAVE_PJSIP_INV_SESSION_REF
  1051. pjsip_inv_dec_ref(session->inv_session);
  1052. #endif
  1053. return -1;
  1054. }
  1055. /*!
  1056. * \internal
  1057. * \brief TRUE if a COLP update can be sent to the peer.
  1058. * \since 13.3.0
  1059. *
  1060. * \param session The session to see if the COLP update is allowed.
  1061. *
  1062. * \retval 0 Update is not allowed.
  1063. * \retval 1 Update is allowed.
  1064. */
  1065. static int is_colp_update_allowed(struct ast_sip_session *session)
  1066. {
  1067. struct ast_party_id connected_id;
  1068. int update_allowed = 0;
  1069. if (!session->endpoint->send_connected_line
  1070. || (!session->endpoint->id.send_pai && !session->endpoint->id.send_rpid)) {
  1071. return 0;
  1072. }
  1073. /*
  1074. * Check if privacy allows the update. Check while the channel
  1075. * is locked so we can work with the shallow connected_id copy.
  1076. */
  1077. ast_channel_lock(session->channel);
  1078. connected_id = ast_channel_connected_effective_id(session->channel);
  1079. if (connected_id.number.valid
  1080. && (session->endpoint->id.trust_outbound
  1081. || (ast_party_id_presentation(&connected_id) & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED)) {
  1082. update_allowed = 1;
  1083. }
  1084. ast_channel_unlock(session->channel);
  1085. return update_allowed;
  1086. }
  1087. /*! \brief Update connected line information */
  1088. static int update_connected_line_information(void *data)
  1089. {
  1090. struct ast_sip_session *session = data;
  1091. if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
  1092. ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
  1093. session->inv_session->cause,
  1094. pjsip_get_status_text(session->inv_session->cause)->ptr);
  1095. #ifdef HAVE_PJSIP_INV_SESSION_REF
  1096. pjsip_inv_dec_ref(session->inv_session);
  1097. #endif
  1098. ao2_ref(session, -1);
  1099. return -1;
  1100. }
  1101. if (ast_channel_state(session->channel) == AST_STATE_UP
  1102. || session->inv_session->role == PJSIP_ROLE_UAC) {
  1103. if (is_colp_update_allowed(session)) {
  1104. enum ast_sip_session_refresh_method method;
  1105. int generate_new_sdp;
  1106. method = session->endpoint->id.refresh_method;
  1107. if (session->inv_session->options & PJSIP_INV_SUPPORT_UPDATE) {
  1108. method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
  1109. }
  1110. /* Only the INVITE method actually needs SDP, UPDATE can do without */
  1111. generate_new_sdp = (method == AST_SIP_SESSION_REFRESH_METHOD_INVITE);
  1112. ast_sip_session_refresh(session, NULL, NULL, NULL, method, generate_new_sdp);
  1113. }
  1114. } else if (session->endpoint->rpid_immediate
  1115. && session->inv_session->state != PJSIP_INV_STATE_DISCONNECTED
  1116. && is_colp_update_allowed(session)) {
  1117. int response_code = 0;
  1118. if (ast_channel_state(session->channel) == AST_STATE_RING) {
  1119. response_code = !session->endpoint->inband_progress ? 180 : 183;
  1120. } else if (ast_channel_state(session->channel) == AST_STATE_RINGING) {
  1121. response_code = 183;
  1122. }
  1123. if (response_code) {
  1124. struct pjsip_tx_data *packet = NULL;
  1125. if (pjsip_inv_answer(session->inv_session, response_code, NULL, NULL, &packet) == PJ_SUCCESS) {
  1126. ast_sip_session_send_response(session, packet);
  1127. }
  1128. }
  1129. }
  1130. #ifdef HAVE_PJSIP_INV_SESSION_REF
  1131. pjsip_inv_dec_ref(session->inv_session);
  1132. #endif
  1133. ao2_ref(session, -1);
  1134. return 0;
  1135. }
  1136. /*! \brief Function called by core to ask the channel to indicate some sort of condition */
  1137. static int chan_pjsip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
  1138. {
  1139. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
  1140. struct chan_pjsip_pvt *pvt = channel->pvt;
  1141. struct ast_sip_session_media *media;
  1142. int response_code = 0;
  1143. int res = 0;
  1144. char *device_buf;
  1145. size_t device_buf_size;
  1146. switch (condition) {
  1147. case AST_CONTROL_RINGING:
  1148. if (ast_channel_state(ast) == AST_STATE_RING) {
  1149. if (channel->session->endpoint->inband_progress) {
  1150. response_code = 183;
  1151. res = -1;
  1152. } else {
  1153. response_code = 180;
  1154. }
  1155. } else {
  1156. res = -1;
  1157. }
  1158. ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "PJSIP/%s", ast_sorcery_object_get_id(channel->session->endpoint));
  1159. break;
  1160. case AST_CONTROL_BUSY:
  1161. if (ast_channel_state(ast) != AST_STATE_UP) {
  1162. response_code = 486;
  1163. } else {
  1164. res = -1;
  1165. }
  1166. break;
  1167. case AST_CONTROL_CONGESTION:
  1168. if (ast_channel_state(ast) != AST_STATE_UP) {
  1169. response_code = 503;
  1170. } else {
  1171. res = -1;
  1172. }
  1173. break;
  1174. case AST_CONTROL_INCOMPLETE:
  1175. if (ast_channel_state(ast) != AST_STATE_UP) {
  1176. response_code = 484;
  1177. } else {
  1178. res = -1;
  1179. }
  1180. break;
  1181. case AST_CONTROL_PROCEEDING:
  1182. if (ast_channel_state(ast) != AST_STATE_UP) {
  1183. response_code = 100;
  1184. } else {
  1185. res = -1;
  1186. }
  1187. break;
  1188. case AST_CONTROL_PROGRESS:
  1189. if (ast_channel_state(ast) != AST_STATE_UP) {
  1190. response_code = 183;
  1191. } else {
  1192. res = -1;
  1193. }
  1194. ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "PJSIP/%s", ast_sorcery_object_get_id(channel->session->endpoint));
  1195. break;
  1196. case AST_CONTROL_VIDUPDATE:
  1197. media = pvt->media[SIP_MEDIA_VIDEO];
  1198. if (media && media->rtp) {
  1199. /* FIXME: Only use this for VP8. Additional work would have to be done to
  1200. * fully support other video codecs */
  1201. if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), ast_format_vp8) != AST_FORMAT_CMP_NOT_EQUAL ||
  1202. ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), ast_format_vp9) != AST_FORMAT_CMP_NOT_EQUAL) {
  1203. /* FIXME Fake RTP write, this will be sent as an RTCP packet. Ideally the
  1204. * RTP engine would provide a way to externally write/schedule RTCP
  1205. * packets */
  1206. struct ast_frame fr;
  1207. fr.frametype = AST_FRAME_CONTROL;
  1208. fr.subclass.integer = AST_CONTROL_VIDUPDATE;
  1209. res = ast_rtp_instance_write(media->rtp, &fr);
  1210. } else {
  1211. ao2_ref(channel->session, +1);
  1212. #ifdef HAVE_PJSIP_INV_SESSION_REF
  1213. if (pjsip_inv_add_ref(channel->session->inv_session) != PJ_SUCCESS) {
  1214. ast_log(LOG_ERROR, "Can't increase the session reference counter\n");
  1215. ao2_cleanup(channel->session);
  1216. } else {
  1217. #endif
  1218. if (ast_sip_push_task(channel->session->serializer, transmit_info_with_vidupdate, channel->session)) {
  1219. ao2_cleanup(channel->session);
  1220. }
  1221. #ifdef HAVE_PJSIP_INV_SESSION_REF
  1222. }
  1223. #endif
  1224. }
  1225. ast_test_suite_event_notify("AST_CONTROL_VIDUPDATE", "Result: Success");
  1226. } else {
  1227. ast_test_suite_event_notify("AST_CONTROL_VIDUPDATE", "Result: Failure");
  1228. res = -1;
  1229. }
  1230. break;
  1231. case AST_CONTROL_CONNECTED_LINE:
  1232. ao2_ref(channel->session, +1);
  1233. #ifdef HAVE_PJSIP_INV_SESSION_REF
  1234. if (pjsip_inv_add_ref(channel->session->inv_session) != PJ_SUCCESS) {
  1235. ast_log(LOG_ERROR, "Can't increase the session reference counter\n");
  1236. ao2_cleanup(channel->session);
  1237. return -1;
  1238. }
  1239. #endif
  1240. if (ast_sip_push_task(channel->session->serializer, update_connected_line_information, channel->session)) {
  1241. #ifdef HAVE_PJSIP_INV_SESSION_REF
  1242. pjsip_inv_dec_ref(channel->session->inv_session);
  1243. #endif
  1244. ao2_cleanup(channel->session);
  1245. }
  1246. break;
  1247. case AST_CONTROL_UPDATE_RTP_PEER:
  1248. break;
  1249. case AST_CONTROL_PVT_CAUSE_CODE:
  1250. res = -1;
  1251. break;
  1252. case AST_CONTROL_MASQUERADE_NOTIFY:
  1253. ast_assert(datalen == sizeof(int));
  1254. if (*(int *) data) {
  1255. /*
  1256. * Masquerade is beginning:
  1257. * Wait for session serializer to get suspended.
  1258. */
  1259. ast_channel_unlock(ast);
  1260. ast_sip_session_suspend(channel->session);
  1261. ast_channel_lock(ast);
  1262. } else {
  1263. /*
  1264. * Masquerade is complete:
  1265. * Unsuspend the session serializer.
  1266. */
  1267. ast_sip_session_unsuspend(channel->session);
  1268. }
  1269. break;
  1270. case AST_CONTROL_HOLD:
  1271. chan_pjsip_add_hold(ast_channel_uniqueid(ast));
  1272. device_buf_size = strlen(ast_channel_name(ast)) + 1;
  1273. device_buf = alloca(device_buf_size);
  1274. ast_channel_get_device_name(ast, device_buf, device_buf_size);
  1275. ast_devstate_changed_literal(AST_DEVICE_ONHOLD, 1, device_buf);
  1276. ast_moh_start(ast, data, NULL);
  1277. break;
  1278. case AST_CONTROL_UNHOLD:
  1279. chan_pjsip_remove_hold(ast_channel_uniqueid(ast));
  1280. device_buf_size = strlen(ast_channel_name(ast)) + 1;
  1281. device_buf = alloca(device_buf_size);
  1282. ast_channel_get_device_name(ast, device_buf, device_buf_size);
  1283. ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, 1, device_buf);
  1284. ast_moh_stop(ast);
  1285. break;
  1286. case AST_CONTROL_SRCUPDATE:
  1287. break;
  1288. case AST_CONTROL_SRCCHANGE:
  1289. break;
  1290. case AST_CONTROL_REDIRECTING:
  1291. if (ast_channel_state(ast) != AST_STATE_UP) {
  1292. response_code = 181;
  1293. } else {
  1294. res = -1;
  1295. }
  1296. break;
  1297. case AST_CONTROL_T38_PARAMETERS:
  1298. res = 0;
  1299. if (channel->session->t38state == T38_PEER_REINVITE) {
  1300. const struct ast_control_t38_parameters *parameters = data;
  1301. if (parameters->request_response == AST_T38_REQUEST_PARMS) {
  1302. res = AST_T38_REQUEST_PARMS;
  1303. }
  1304. }
  1305. break;
  1306. case -1:
  1307. res = -1;
  1308. break;
  1309. default:
  1310. ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
  1311. res = -1;
  1312. break;
  1313. }
  1314. if (response_code) {
  1315. struct indicate_data *ind_data = indicate_data_alloc(channel->session, condition, response_code, data, datalen);
  1316. if (!ind_data) {
  1317. return -1;
  1318. }
  1319. #ifdef HAVE_PJSIP_INV_SESSION_REF
  1320. if (pjsip_inv_add_ref(ind_data->session->inv_session) != PJ_SUCCESS) {
  1321. ast_log(LOG_ERROR, "Can't increase the session reference counter\n");
  1322. ao2_cleanup(ind_data);
  1323. return -1;
  1324. }
  1325. #endif
  1326. if (ast_sip_push_task(channel->session->serializer, indicate, ind_data)) {
  1327. ast_log(LOG_NOTICE, "Cannot send response code %d to endpoint %s. Could not queue task properly\n",
  1328. response_code, ast_sorcery_object_get_id(channel->session->endpoint));
  1329. #ifdef HAVE_PJSIP_INV_SESSION_REF
  1330. pjsip_inv_dec_ref(ind_data->session->inv_session);
  1331. #endif
  1332. ao2_cleanup(ind_data);
  1333. res = -1;
  1334. }
  1335. }
  1336. return res;
  1337. }
  1338. struct transfer_data {
  1339. struct ast_sip_session *session;
  1340. char *target;
  1341. };
  1342. static void transfer_data_destroy(void *obj)
  1343. {
  1344. struct transfer_data *trnf_data = obj;
  1345. ast_free(trnf_data->target);
  1346. ao2_cleanup(trnf_data->session);
  1347. }
  1348. static struct transfer_data *transfer_data_alloc(struct ast_sip_session *session, const char *target)
  1349. {
  1350. struct transfer_data *trnf_data = ao2_alloc(sizeof(*trnf_data), transfer_data_destroy);
  1351. if (!trnf_data) {
  1352. return NULL;
  1353. }
  1354. if (!(trnf_data->target = ast_strdup(target))) {
  1355. ao2_ref(trnf_data, -1);
  1356. return NULL;
  1357. }
  1358. ao2_ref(session, +1);
  1359. trnf_data->session = session;
  1360. return trnf_data;
  1361. }
  1362. static void transfer_redirect(struct ast_sip_session *session, const char *target)
  1363. {
  1364. pjsip_tx_data *packet;
  1365. enum ast_control_transfer message = AST_TRANSFER_SUCCESS;
  1366. pjsip_contact_hdr *contact;
  1367. pj_str_t tmp;
  1368. if (pjsip_inv_end_session(session->inv_session, 302, NULL, &packet) != PJ_SUCCESS
  1369. || !packet) {
  1370. ast_log(LOG_WARNING, "Failed to redirect PJSIP session for channel %s\n",
  1371. ast_channel_name(session->channel));
  1372. message = AST_TRANSFER_FAILED;
  1373. ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
  1374. return;
  1375. }
  1376. if (!(contact = pjsip_msg_find_hdr(packet->msg, PJSIP_H_CONTACT, NULL))) {
  1377. contact = pjsip_contact_hdr_create(packet->pool);
  1378. }
  1379. pj_strdup2_with_null(packet->pool, &tmp, target);
  1380. if (!(contact->uri = pjsip_parse_uri(packet->pool, tmp.ptr, tmp.slen, PJSIP_PARSE_URI_AS_NAMEADDR))) {
  1381. ast_log(LOG_WARNING, "Failed to parse destination URI '%s' for channel %s\n",
  1382. target, ast_channel_name(session->channel));
  1383. message = AST_TRANSFER_FAILED;
  1384. ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
  1385. pjsip_tx_data_dec_ref(packet);
  1386. return;
  1387. }
  1388. pjsip_msg_add_hdr(packet->msg, (pjsip_hdr *) contact);
  1389. ast_sip_session_send_response(session, packet);
  1390. ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
  1391. }
  1392. static void transfer_refer(struct ast_sip_session *session, const char *target)
  1393. {
  1394. pjsip_evsub *sub;
  1395. enum ast_control_transfer message = AST_TRANSFER_SUCCESS;
  1396. pj_str_t tmp;
  1397. pjsip_tx_data *packet;
  1398. const char *ref_by_val;
  1399. char local_info[pj_strlen(&session->inv_session->dlg->local.info_str) + 1];
  1400. if (pjsip_xfer_create_uac(session->inv_session->dlg, NULL, &sub) != PJ_SUCCESS) {
  1401. message = AST_TRANSFER_FAILED;
  1402. ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
  1403. return;
  1404. }
  1405. if (pjsip_xfer_initiate(sub, pj_cstr(&tmp, target), &packet) != PJ_SUCCESS) {
  1406. message = AST_TRANSFER_FAILED;
  1407. ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
  1408. pjsip_evsub_terminate(sub, PJ_FALSE);
  1409. return;
  1410. }
  1411. ref_by_val = pbx_builtin_getvar_helper(session->channel, "SIPREFERREDBYHDR");
  1412. if (!ast_strlen_zero(ref_by_val)) {
  1413. ast_sip_add_header(packet, "Referred-By", ref_by_val);
  1414. } else {
  1415. ast_copy_pj_str(local_info, &session->inv_session->dlg->local.info_str, sizeof(local_info));
  1416. ast_sip_add_header(packet, "Referred-By", local_info);
  1417. }
  1418. pjsip_xfer_send_request(sub, packet);
  1419. ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
  1420. }
  1421. static int transfer(void *data)
  1422. {
  1423. struct transfer_data *trnf_data = data;
  1424. struct ast_sip_endpoint *endpoint = NULL;
  1425. struct ast_sip_contact *contact = NULL;
  1426. const char *target = trnf_data->target;
  1427. if (trnf_data->session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
  1428. ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
  1429. trnf_data->session->inv_session->cause,
  1430. pjsip_get_status_text(trnf_data->session->inv_session->cause)->ptr);
  1431. } else {
  1432. /* See if we have an endpoint; if so, use its contact */
  1433. endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", target);
  1434. if (endpoint) {
  1435. contact = ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors);
  1436. if (contact && !ast_strlen_zero(contact->uri)) {
  1437. target = contact->uri;
  1438. }
  1439. }
  1440. if (ast_channel_state(trnf_data->session->channel) == AST_STATE_RING) {
  1441. transfer_redirect(trnf_data->session, target);
  1442. } else {
  1443. transfer_refer(trnf_data->session, target);
  1444. }
  1445. }
  1446. #ifdef HAVE_PJSIP_INV_SESSION_REF
  1447. pjsip_inv_dec_ref(trnf_data->session->inv_session);
  1448. #endif
  1449. ao2_ref(trnf_data, -1);
  1450. ao2_cleanup(endpoint);
  1451. ao2_cleanup(contact);
  1452. return 0;
  1453. }
  1454. /*! \brief Function called by core for Asterisk initiated transfer */
  1455. static int chan_pjsip_transfer(struct ast_channel *chan, const char *target)
  1456. {
  1457. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
  1458. struct transfer_data *trnf_data = transfer_data_alloc(channel->session, target);
  1459. if (!trnf_data) {
  1460. return -1;
  1461. }
  1462. #ifdef HAVE_PJSIP_INV_SESSION_REF
  1463. if (pjsip_inv_add_ref(trnf_data->session->inv_session) != PJ_SUCCESS) {
  1464. ast_log(LOG_ERROR, "Can't increase the session reference counter\n");
  1465. ao2_cleanup(trnf_data);
  1466. return -1;
  1467. }
  1468. #endif
  1469. if (ast_sip_push_task(channel->session->serializer, transfer, trnf_data)) {
  1470. ast_log(LOG_WARNING, "Error requesting transfer\n");
  1471. #ifdef HAVE_PJSIP_INV_SESSION_REF
  1472. pjsip_inv_dec_ref(trnf_data->session->inv_session);
  1473. #endif
  1474. ao2_cleanup(trnf_data);
  1475. return -1;
  1476. }
  1477. return 0;
  1478. }
  1479. /*! \brief Function called by core to start a DTMF digit */
  1480. static int chan_pjsip_digit_begin(struct ast_channel *chan, char digit)
  1481. {
  1482. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
  1483. struct chan_pjsip_pvt *pvt = channel->pvt;
  1484. struct ast_sip_session_media *media = pvt->media[SIP_MEDIA_AUDIO];
  1485. int res = 0;
  1486. switch (channel->session->dtmf) {
  1487. case AST_SIP_DTMF_RFC_4733:
  1488. if (!media || !media->rtp) {
  1489. return -1;
  1490. }
  1491. ast_rtp_instance_dtmf_begin(media->rtp, digit);
  1492. break;
  1493. case AST_SIP_DTMF_AUTO:
  1494. if (!media || !media->rtp || (ast_rtp_instance_dtmf_mode_get(media->rtp) == AST_RTP_DTMF_MODE_INBAND)) {
  1495. return -1;
  1496. }
  1497. ast_rtp_instance_dtmf_begin(media->rtp, digit);
  1498. break;
  1499. case AST_SIP_DTMF_AUTO_INFO:
  1500. if (!media || !media->rtp || (ast_rtp_instance_dtmf_mode_get(media->rtp) == AST_RTP_DTMF_MODE_NONE)) {
  1501. return -1;
  1502. }
  1503. ast_rtp_instance_dtmf_begin(media->rtp, digit);
  1504. break;
  1505. case AST_SIP_DTMF_NONE:
  1506. break;
  1507. case AST_SIP_DTMF_INBAND:
  1508. res = -1;
  1509. break;
  1510. default:
  1511. break;
  1512. }
  1513. return res;
  1514. }
  1515. struct info_dtmf_data {
  1516. struct ast_sip_session *session;
  1517. char digit;
  1518. unsigned int duration;
  1519. };
  1520. static void info_dtmf_data_destroy(void *obj)
  1521. {
  1522. struct info_dtmf_data *dtmf_data = obj;
  1523. ao2_ref(dtmf_data->session, -1);
  1524. }
  1525. static struct info_dtmf_data *info_dtmf_data_alloc(struct ast_sip_session *session, char digit, unsigned int duration)
  1526. {
  1527. struct info_dtmf_data *dtmf_data = ao2_alloc(sizeof(*dtmf_data), info_dtmf_data_destroy);
  1528. if (!dtmf_data) {
  1529. return NULL;
  1530. }
  1531. ao2_ref(session, +1);
  1532. dtmf_data->session = session;
  1533. dtmf_data->digit = digit;
  1534. dtmf_data->duration = duration;
  1535. return dtmf_data;
  1536. }
  1537. static int transmit_info_dtmf(void *data)
  1538. {
  1539. RAII_VAR(struct info_dtmf_data *, dtmf_data, data, ao2_cleanup);
  1540. struct ast_sip_session *session = dtmf_data->session;
  1541. struct pjsip_tx_data *tdata;
  1542. RAII_VAR(struct ast_str *, body_text, NULL, ast_free_ptr);
  1543. struct ast_sip_body body = {
  1544. .type = "application",
  1545. .subtype = "dtmf-relay",
  1546. };
  1547. if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
  1548. ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
  1549. session->inv_session->cause,
  1550. pjsip_get_status_text(session->inv_session->cause)->ptr);
  1551. goto failure;
  1552. }
  1553. if (!(body_text = ast_str_create(32))) {
  1554. ast_log(LOG_ERROR, "Could not allocate buffer for INFO DTMF.\n");
  1555. goto failure;
  1556. }
  1557. ast_str_set(&body_text, 0, "Signal=%c\r\nDuration=%u\r\n", dtmf_data->digit, dtmf_data->duration);
  1558. body.body_text = ast_str_buffer(body_text);
  1559. if (ast_sip_create_request("INFO", session->inv_session->dlg, session->endpoint, NULL, NULL, &tdata)) {
  1560. ast_log(LOG_ERROR, "Could not create DTMF INFO request\n");
  1561. goto failure;
  1562. }
  1563. if (ast_sip_add_body(tdata, &body)) {
  1564. ast_log(LOG_ERROR, "Could not add body to DTMF INFO request\n");
  1565. pjsip_tx_data_dec_ref(tdata);
  1566. goto failure;
  1567. }
  1568. ast_sip_session_send_request(session, tdata);
  1569. #ifdef HAVE_PJSIP_INV_SESSION_REF
  1570. pjsip_inv_dec_ref(session->inv_session);
  1571. #endif
  1572. return 0;
  1573. failure:
  1574. #ifdef HAVE_PJSIP_INV_SESSION_REF
  1575. pjsip_inv_dec_ref(session->inv_session);
  1576. #endif
  1577. return -1;
  1578. }
  1579. /*! \brief Function called by core to stop a DTMF digit */
  1580. static int chan_pjsip_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
  1581. {
  1582. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
  1583. struct chan_pjsip_pvt *pvt = channel->pvt;
  1584. struct ast_sip_session_media *media = pvt->media[SIP_MEDIA_AUDIO];
  1585. int res = 0;
  1586. switch (channel->session->dtmf) {
  1587. case AST_SIP_DTMF_AUTO_INFO:
  1588. {
  1589. if (!media || !media->rtp) {
  1590. return -1;
  1591. }
  1592. if (ast_rtp_instance_dtmf_mode_get(media->rtp) != AST_RTP_DTMF_MODE_NONE) {
  1593. ast_debug(3, "Told to send end of digit on Auto-Info channel %s RFC4733 negotiated so using it.\n", ast_channel_name(ast));
  1594. ast_rtp_instance_dtmf_end_with_duration(media->rtp, digit, duration);
  1595. break;
  1596. }
  1597. /* If RFC_4733 was not negotiated, fail through to the DTMF_INFO processing */
  1598. ast_debug(3, "Told to send end of digit on Auto-Info channel %s RFC4733 NOT negotiated using INFO instead.\n", ast_channel_name(ast));
  1599. }
  1600. case AST_SIP_DTMF_INFO:
  1601. {
  1602. struct info_dtmf_data *dtmf_data = info_dtmf_data_alloc(channel->session, digit, duration);
  1603. if (!dtmf_data) {
  1604. return -1;
  1605. }
  1606. #ifdef HAVE_PJSIP_INV_SESSION_REF
  1607. if (pjsip_inv_add_ref(dtmf_data->session->inv_session) != PJ_SUCCESS) {
  1608. ast_log(LOG_ERROR, "Can't increase the session reference counter\n");
  1609. ao2_cleanup(dtmf_data);
  1610. return -1;
  1611. }
  1612. #endif
  1613. if (ast_sip_push_task(channel->session->serializer, transmit_info_dtmf, dtmf_data)) {
  1614. ast_log(LOG_WARNING, "Error sending DTMF via INFO.\n");
  1615. #ifdef HAVE_PJSIP_INV_SESSION_REF
  1616. pjsip_inv_dec_ref(dtmf_data->session->inv_session);
  1617. #endif
  1618. ao2_cleanup(dtmf_data);
  1619. return -1;
  1620. }
  1621. break;
  1622. }
  1623. case AST_SIP_DTMF_RFC_4733:
  1624. if (!media || !media->rtp) {
  1625. return -1;
  1626. }
  1627. ast_rtp_instance_dtmf_end_with_duration(media->rtp, digit, duration);
  1628. break;
  1629. case AST_SIP_DTMF_AUTO:
  1630. if (!media || !media->rtp || (ast_rtp_instance_dtmf_mode_get(media->rtp) == AST_RTP_DTMF_MODE_INBAND)) {
  1631. return -1;
  1632. }
  1633. ast_rtp_instance_dtmf_end_with_duration(media->rtp, digit, duration);
  1634. break;
  1635. case AST_SIP_DTMF_NONE:
  1636. break;
  1637. case AST_SIP_DTMF_INBAND:
  1638. res = -1;
  1639. break;
  1640. }
  1641. return res;
  1642. }
  1643. static void update_initial_connected_line(struct ast_sip_session *session)
  1644. {
  1645. struct ast_party_connected_line connected;
  1646. /*
  1647. * Use the channel CALLERID() as the initial connected line data.
  1648. * The core or a predial handler may have supplied missing values
  1649. * from the session->endpoint->id.self about who we are calling.
  1650. */
  1651. ast_channel_lock(session->channel);
  1652. ast_party_id_copy(&session->id, &ast_channel_caller(session->channel)->id);
  1653. ast_channel_unlock(session->channel);
  1654. /* Supply initial connected line information if available. */
  1655. if (!session->id.number.valid && !session->id.name.valid) {
  1656. return;
  1657. }
  1658. ast_party_connected_line_init(&connected);
  1659. connected.id = session->id;
  1660. connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
  1661. ast_channel_queue_connected_line_update(session->channel, &connected, NULL);
  1662. }
  1663. static int call(void *data)
  1664. {
  1665. struct ast_sip_channel_pvt *channel = data;
  1666. struct ast_sip_session *session = channel->session;
  1667. struct chan_pjsip_pvt *pvt = channel->pvt;
  1668. pjsip_tx_data *tdata;
  1669. int res = ast_sip_session_create_invite(session, &tdata);
  1670. if (res) {
  1671. ast_set_hangupsource(session->channel, ast_channel_name(session->channel), 0);
  1672. ast_queue_hangup(session->channel);
  1673. } else {
  1674. set_channel_on_rtp_instance(pvt, ast_channel_uniqueid(session->channel));
  1675. update_initial_connected_line(session);
  1676. ast_sip_session_send_request(session, tdata);
  1677. }
  1678. ao2_ref(channel, -1);
  1679. return res;
  1680. }
  1681. /*! \brief Function called by core to actually start calling a remote party */
  1682. static int chan_pjsip_call(struct ast_channel *ast, const char *dest, int timeout)
  1683. {
  1684. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
  1685. ao2_ref(channel, +1);
  1686. if (ast_sip_push_task(channel->session->serializer, call, channel)) {
  1687. ast_log(LOG_WARNING, "Error attempting to place outbound call to '%s'\n", dest);
  1688. ao2_cleanup(channel);
  1689. return -1;
  1690. }
  1691. return 0;
  1692. }
  1693. /*! \brief Internal function which translates from Asterisk cause codes to SIP response codes */
  1694. static int hangup_cause2sip(int cause)
  1695. {
  1696. switch (cause) {
  1697. case AST_CAUSE_UNALLOCATED: /* 1 */
  1698. case AST_CAUSE_NO_ROUTE_DESTINATION: /* 3 IAX2: Can't find extension in context */
  1699. case AST_CAUSE_NO_ROUTE_TRANSIT_NET: /* 2 */
  1700. return 404;
  1701. case AST_CAUSE_CONGESTION: /* 34 */
  1702. case AST_CAUSE_SWITCH_CONGESTION: /* 42 */
  1703. return 503;
  1704. case AST_CAUSE_NO_USER_RESPONSE: /* 18 */
  1705. return 408;
  1706. case AST_CAUSE_NO_ANSWER: /* 19 */
  1707. case AST_CAUSE_UNREGISTERED: /* 20 */
  1708. return 480;
  1709. case AST_CAUSE_CALL_REJECTED: /* 21 */
  1710. return 403;
  1711. case AST_CAUSE_NUMBER_CHANGED: /* 22 */
  1712. return 410;
  1713. case AST_CAUSE_NORMAL_UNSPECIFIED: /* 31 */
  1714. return 480;
  1715. case AST_CAUSE_INVALID_NUMBER_FORMAT:
  1716. return 484;
  1717. case AST_CAUSE_USER_BUSY:
  1718. return 486;
  1719. case AST_CAUSE_FAILURE:
  1720. return 500;
  1721. case AST_CAUSE_FACILITY_REJECTED: /* 29 */
  1722. return 501;
  1723. case AST_CAUSE_CHAN_NOT_IMPLEMENTED:
  1724. return 503;
  1725. case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
  1726. return 502;
  1727. case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL: /* Can't find codec to connect to host */
  1728. return 488;
  1729. case AST_CAUSE_INTERWORKING: /* Unspecified Interworking issues */
  1730. return 500;
  1731. case AST_CAUSE_NOTDEFINED:
  1732. default:
  1733. ast_debug(1, "AST hangup cause %d (no match found in PJSIP)\n", cause);
  1734. return 0;
  1735. }
  1736. /* Never reached */
  1737. return 0;
  1738. }
  1739. struct hangup_data {
  1740. int cause;
  1741. struct ast_channel *chan;
  1742. };
  1743. static void hangup_data_destroy(void *obj)
  1744. {
  1745. struct hangup_data *h_data = obj;
  1746. h_data->chan = ast_channel_unref(h_data->chan);
  1747. }
  1748. static struct hangup_data *hangup_data_alloc(int cause, struct ast_channel *chan)
  1749. {
  1750. struct hangup_data *h_data = ao2_alloc(sizeof(*h_data), hangup_data_destroy);
  1751. if (!h_data) {
  1752. return NULL;
  1753. }
  1754. h_data->cause = cause;
  1755. h_data->chan = ast_channel_ref(chan);
  1756. return h_data;
  1757. }
  1758. /*! \brief Clear a channel from a session along with its PVT */
  1759. static void clear_session_and_channel(struct ast_sip_session *session, struct ast_channel *ast, struct chan_pjsip_pvt *pvt)
  1760. {
  1761. session->channel = NULL;
  1762. set_channel_on_rtp_instance(pvt, "");
  1763. ast_channel_tech_pvt_set(ast, NULL);
  1764. }
  1765. static int hangup(void *data)
  1766. {
  1767. struct hangup_data *h_data = data;
  1768. struct ast_channel *ast = h_data->chan;
  1769. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
  1770. struct chan_pjsip_pvt *pvt = channel->pvt;
  1771. struct ast_sip_session *session = channel->session;
  1772. int cause = h_data->cause;
  1773. /*
  1774. * It's possible that session_terminate might cause the session to be destroyed
  1775. * immediately so we need to keep a reference to it so we can NULL session->channel
  1776. * afterwards.
  1777. */
  1778. ast_sip_session_terminate(ao2_bump(session), cause);
  1779. clear_session_and_channel(session, ast, pvt);
  1780. ao2_cleanup(session);
  1781. ao2_cleanup(channel);
  1782. ao2_cleanup(h_data);
  1783. return 0;
  1784. }
  1785. /*! \brief Function called by core to hang up a PJSIP session */
  1786. static int chan_pjsip_hangup(struct ast_channel *ast)
  1787. {
  1788. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
  1789. struct chan_pjsip_pvt *pvt;
  1790. int cause;
  1791. struct hangup_data *h_data;
  1792. if (!channel || !channel->session) {
  1793. return -1;
  1794. }
  1795. pvt = channel->pvt;
  1796. cause = hangup_cause2sip(ast_channel_hangupcause(channel->session->channel));
  1797. h_data = hangup_data_alloc(cause, ast);
  1798. if (!h_data) {
  1799. goto failure;
  1800. }
  1801. if (ast_sip_push_task(channel->session->serializer, hangup, h_data)) {
  1802. ast_log(LOG_WARNING, "Unable to push hangup task to the threadpool. Expect bad things\n");
  1803. goto failure;
  1804. }
  1805. return 0;
  1806. failure:
  1807. /* Go ahead and do our cleanup of the session and channel even if we're not going
  1808. * to be able to send our SIP request/response
  1809. */
  1810. clear_session_and_channel(channel->session, ast, pvt);
  1811. ao2_cleanup(channel);
  1812. ao2_cleanup(h_data);
  1813. return -1;
  1814. }
  1815. struct request_data {
  1816. struct ast_sip_session *session;
  1817. struct ast_format_cap *caps;
  1818. const char *dest;
  1819. int cause;
  1820. };
  1821. static int request(void *obj)
  1822. {
  1823. struct request_data *req_data = obj;
  1824. struct ast_sip_session *session = NULL;
  1825. char *tmp = ast_strdupa(req_data->dest), *endpoint_name = NULL, *request_user = NULL;
  1826. struct ast_sip_endpoint *endpoint;
  1827. AST_DECLARE_APP_ARGS(args,
  1828. AST_APP_ARG(endpoint);
  1829. AST_APP_ARG(aor);
  1830. );
  1831. if (ast_strlen_zero(tmp)) {
  1832. ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty destination\n");
  1833. req_data->cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
  1834. return -1;
  1835. }
  1836. AST_NONSTANDARD_APP_ARGS(args, tmp, '/');
  1837. if (ast_sip_get_disable_multi_domain()) {
  1838. /* If a request user has been specified extract it from the endpoint name portion */
  1839. if ((endpoint_name = strchr(args.endpoint, '@'))) {
  1840. request_user = args.endpoint;
  1841. *endpoint_name++ = '\0';
  1842. } else {
  1843. endpoint_name = args.endpoint;
  1844. }
  1845. if (ast_strlen_zero(endpoint_name)) {
  1846. if (request_user) {
  1847. ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name: %s@<endpoint-name>\n",
  1848. request_user);
  1849. } else {
  1850. ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name\n");
  1851. }
  1852. req_data->cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
  1853. return -1;
  1854. }
  1855. endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
  1856. endpoint_name);
  1857. if (!endpoint) {
  1858. ast_log(LOG_ERROR, "Unable to create PJSIP channel - endpoint '%s' was not found\n", endpoint_name);
  1859. req_data->cause = AST_CAUSE_NO_ROUTE_DESTINATION;
  1860. return -1;
  1861. }
  1862. } else {
  1863. /* First try to find an exact endpoint match, for single (user) or multi-domain (user@domain) */
  1864. endpoint_name = args.endpoint;
  1865. if (ast_strlen_zero(endpoint_name)) {
  1866. ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name\n");
  1867. req_data->cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
  1868. return -1;
  1869. }
  1870. endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
  1871. endpoint_name);
  1872. if (!endpoint) {
  1873. /* It seems it's not a multi-domain endpoint or single endpoint exact match,
  1874. * it's possible that it's a SIP trunk with a specified user (user@trunkname),
  1875. * so extract the user before @ sign.
  1876. */
  1877. endpoint_name = strchr(args.endpoint, '@');
  1878. if (!endpoint_name) {
  1879. /*
  1880. * Couldn't find an '@' so it had to be an endpoint
  1881. * name that doesn't exist.
  1882. */
  1883. ast_log(LOG_ERROR, "Unable to create PJSIP channel - endpoint '%s' was not found\n",
  1884. args.endpoint);
  1885. req_data->cause = AST_CAUSE_NO_ROUTE_DESTINATION;
  1886. return -1;
  1887. }
  1888. request_user = args.endpoint;
  1889. *endpoint_name++ = '\0';
  1890. if (ast_strlen_zero(endpoint_name)) {
  1891. ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name: %s@<endpoint-name>\n",
  1892. request_user);
  1893. req_data->cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
  1894. return -1;
  1895. }
  1896. endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
  1897. endpoint_name);
  1898. if (!endpoint) {
  1899. ast_log(LOG_ERROR, "Unable to create PJSIP channel - endpoint '%s' was not found\n", endpoint_name);
  1900. req_data->cause = AST_CAUSE_NO_ROUTE_DESTINATION;
  1901. return -1;
  1902. }
  1903. }
  1904. }
  1905. session = ast_sip_session_create_outgoing(endpoint, NULL, args.aor, request_user,
  1906. req_data->caps);
  1907. ao2_ref(endpoint, -1);
  1908. if (!session) {
  1909. ast_log(LOG_ERROR, "Failed to create outgoing session to endpoint '%s'\n", endpoint_name);
  1910. req_data->cause = AST_CAUSE_NO_ROUTE_DESTINATION;
  1911. return -1;
  1912. }
  1913. req_data->session = session;
  1914. return 0;
  1915. }
  1916. /*! \brief Function called by core to create a new outgoing PJSIP session */
  1917. static struct ast_channel *chan_pjsip_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)
  1918. {
  1919. struct request_data req_data;
  1920. RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
  1921. req_data.caps = cap;
  1922. req_data.dest = data;
  1923. /* Default failure value in case ast_sip_push_task_wait_servant() itself fails. */
  1924. req_data.cause = AST_CAUSE_FAILURE;
  1925. if (ast_sip_push_task_wait_servant(NULL, request, &req_data)) {
  1926. *cause = req_data.cause;
  1927. return NULL;
  1928. }
  1929. session = req_data.session;
  1930. if (!(session->channel = chan_pjsip_new(session, AST_STATE_DOWN, NULL, NULL, assignedids, requestor, NULL))) {
  1931. /* Session needs to be terminated prematurely */
  1932. return NULL;
  1933. }
  1934. return session->channel;
  1935. }
  1936. struct sendtext_data {
  1937. struct ast_sip_session *session;
  1938. struct ast_msg_data *msg;
  1939. };
  1940. static void sendtext_data_destroy(void *obj)
  1941. {
  1942. struct sendtext_data *data = obj;
  1943. ao2_cleanup(data->session);
  1944. ast_free(data->msg);
  1945. }
  1946. static struct sendtext_data* sendtext_data_create(struct ast_channel *chan,
  1947. struct ast_msg_data *msg)
  1948. {
  1949. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
  1950. struct sendtext_data *data = ao2_alloc(sizeof(*data), sendtext_data_destroy);
  1951. if (!data) {
  1952. return NULL;
  1953. }
  1954. data->msg = ast_msg_data_dup(msg);
  1955. if (!data->msg) {
  1956. ao2_cleanup(data);
  1957. return NULL;
  1958. }
  1959. data->session = channel->session;
  1960. ao2_ref(data->session, +1);
  1961. return data;
  1962. }
  1963. static int sendtext(void *obj)
  1964. {
  1965. struct sendtext_data *data = obj;
  1966. pjsip_tx_data *tdata;
  1967. const char *body_text = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_BODY);
  1968. const char *content_type = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_CONTENT_TYPE);
  1969. char *sep;
  1970. struct ast_sip_body body = {
  1971. .type = "text",
  1972. .subtype = "plain",
  1973. .body_text = body_text,
  1974. };
  1975. if (!ast_strlen_zero(content_type)) {
  1976. sep = strchr(content_type, '/');
  1977. if (sep) {
  1978. *sep = '\0';
  1979. body.type = content_type;
  1980. body.subtype = ++sep;
  1981. }
  1982. }
  1983. if (data->session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
  1984. ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
  1985. data->session->inv_session->cause,
  1986. pjsip_get_status_text(data->session->inv_session->cause)->ptr);
  1987. } else {
  1988. pjsip_from_hdr *hdr;
  1989. pjsip_name_addr *name_addr;
  1990. const char *from = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_FROM);
  1991. const char *to = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_TO);
  1992. int invalidate_tdata = 0;
  1993. ast_sip_create_request("MESSAGE", data->session->inv_session->dlg, data->session->endpoint, NULL, NULL, &tdata);
  1994. ast_sip_add_body(tdata, &body);
  1995. /*
  1996. * If we have a 'from' in the msg, set the display name in the From
  1997. * header to it.
  1998. */
  1999. if (!ast_strlen_zero(from)) {
  2000. hdr = PJSIP_MSG_FROM_HDR(tdata->msg);
  2001. name_addr = (pjsip_name_addr *) hdr->uri;
  2002. pj_strdup2(tdata->pool, &name_addr->display, from);
  2003. invalidate_tdata = 1;
  2004. }
  2005. /*
  2006. * If we have a 'to' in the msg, set the display name in the To
  2007. * header to it.
  2008. */
  2009. if (!ast_strlen_zero(to)) {
  2010. hdr = PJSIP_MSG_TO_HDR(tdata->msg);
  2011. name_addr = (pjsip_name_addr *) hdr->uri;
  2012. pj_strdup2(tdata->pool, &name_addr->display, to);
  2013. invalidate_tdata = 1;
  2014. }
  2015. if (invalidate_tdata) {
  2016. pjsip_tx_data_invalidate_msg(tdata);
  2017. }
  2018. ast_sip_send_request(tdata, data->session->inv_session->dlg, data->session->endpoint, NULL, NULL);
  2019. }
  2020. #ifdef HAVE_PJSIP_INV_SESSION_REF
  2021. pjsip_inv_dec_ref(data->session->inv_session);
  2022. #endif
  2023. ao2_cleanup(data);
  2024. return 0;
  2025. }
  2026. /*! \brief Function called by core to send text on PJSIP session */
  2027. static int chan_pjsip_sendtext_data(struct ast_channel *ast, struct ast_msg_data *msg)
  2028. {
  2029. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
  2030. struct sendtext_data *data = sendtext_data_create(ast, msg);
  2031. ast_debug(1, "Sending MESSAGE from '%s' to '%s:%s': %s\n",
  2032. ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_FROM),
  2033. ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_TO),
  2034. ast_channel_name(ast),
  2035. ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_BODY));
  2036. if (!data) {
  2037. return -1;
  2038. }
  2039. #ifdef HAVE_PJSIP_INV_SESSION_REF
  2040. if (pjsip_inv_add_ref(data->session->inv_session) != PJ_SUCCESS) {
  2041. ast_log(LOG_ERROR, "Can't increase the session reference counter\n");
  2042. ao2_ref(data, -1);
  2043. return -1;
  2044. }
  2045. #endif
  2046. if (ast_sip_push_task(channel->session->serializer, sendtext, data)) {
  2047. #ifdef HAVE_PJSIP_INV_SESSION_REF
  2048. pjsip_inv_dec_ref(data->session->inv_session);
  2049. #endif
  2050. ao2_ref(data, -1);
  2051. return -1;
  2052. }
  2053. return 0;
  2054. }
  2055. static int chan_pjsip_sendtext(struct ast_channel *ast, const char *text)
  2056. {
  2057. struct ast_msg_data *msg;
  2058. int rc;
  2059. struct ast_msg_data_attribute attrs[] =
  2060. {
  2061. {
  2062. .type = AST_MSG_DATA_ATTR_BODY,
  2063. .value = (char *)text,
  2064. }
  2065. };
  2066. msg = ast_msg_data_alloc(AST_MSG_DATA_SOURCE_TYPE_UNKNOWN, attrs, ARRAY_LEN(attrs));
  2067. if (!msg) {
  2068. return -1;
  2069. }
  2070. rc = chan_pjsip_sendtext_data(ast, msg);
  2071. ast_free(msg);
  2072. return rc;
  2073. }
  2074. /*! \brief Convert SIP hangup causes to Asterisk hangup causes */
  2075. static int hangup_sip2cause(int cause)
  2076. {
  2077. /* Possible values taken from causes.h */
  2078. switch(cause) {
  2079. case 401: /* Unauthorized */
  2080. return AST_CAUSE_CALL_REJECTED;
  2081. case 403: /* Not found */
  2082. return AST_CAUSE_CALL_REJECTED;
  2083. case 404: /* Not found */
  2084. return AST_CAUSE_UNALLOCATED;
  2085. case 405: /* Method not allowed */
  2086. return AST_CAUSE_INTERWORKING;
  2087. case 407: /* Proxy authentication required */
  2088. return AST_CAUSE_CALL_REJECTED;
  2089. case 408: /* No reaction */
  2090. return AST_CAUSE_NO_USER_RESPONSE;
  2091. case 409: /* Conflict */
  2092. return AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
  2093. case 410: /* Gone */
  2094. return AST_CAUSE_NUMBER_CHANGED;
  2095. case 411: /* Length required */
  2096. return AST_CAUSE_INTERWORKING;
  2097. case 413: /* Request entity too large */
  2098. return AST_CAUSE_INTERWORKING;
  2099. case 414: /* Request URI too large */
  2100. return AST_CAUSE_INTERWORKING;
  2101. case 415: /* Unsupported media type */
  2102. return AST_CAUSE_INTERWORKING;
  2103. case 420: /* Bad extension */
  2104. return AST_CAUSE_NO_ROUTE_DESTINATION;
  2105. case 480: /* No answer */
  2106. return AST_CAUSE_NO_ANSWER;
  2107. case 481: /* No answer */
  2108. return AST_CAUSE_INTERWORKING;
  2109. case 482: /* Loop detected */
  2110. return AST_CAUSE_INTERWORKING;
  2111. case 483: /* Too many hops */
  2112. return AST_CAUSE_NO_ANSWER;
  2113. case 484: /* Address incomplete */
  2114. return AST_CAUSE_INVALID_NUMBER_FORMAT;
  2115. case 485: /* Ambiguous */
  2116. return AST_CAUSE_UNALLOCATED;
  2117. case 486: /* Busy everywhere */
  2118. return AST_CAUSE_BUSY;
  2119. case 487: /* Request terminated */
  2120. return AST_CAUSE_INTERWORKING;
  2121. case 488: /* No codecs approved */
  2122. return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
  2123. case 491: /* Request pending */
  2124. return AST_CAUSE_INTERWORKING;
  2125. case 493: /* Undecipherable */
  2126. return AST_CAUSE_INTERWORKING;
  2127. case 500: /* Server internal failure */
  2128. return AST_CAUSE_FAILURE;
  2129. case 501: /* Call rejected */
  2130. return AST_CAUSE_FACILITY_REJECTED;
  2131. case 502:
  2132. return AST_CAUSE_DESTINATION_OUT_OF_ORDER;
  2133. case 503: /* Service unavailable */
  2134. return AST_CAUSE_CONGESTION;
  2135. case 504: /* Gateway timeout */
  2136. return AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
  2137. case 505: /* SIP version not supported */
  2138. return AST_CAUSE_INTERWORKING;
  2139. case 600: /* Busy everywhere */
  2140. return AST_CAUSE_USER_BUSY;
  2141. case 603: /* Decline */
  2142. return AST_CAUSE_CALL_REJECTED;
  2143. case 604: /* Does not exist anywhere */
  2144. return AST_CAUSE_UNALLOCATED;
  2145. case 606: /* Not acceptable */
  2146. return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
  2147. default:
  2148. if (cause < 500 && cause >= 400) {
  2149. /* 4xx class error that is unknown - someting wrong with our request */
  2150. return AST_CAUSE_INTERWORKING;
  2151. } else if (cause < 600 && cause >= 500) {
  2152. /* 5xx class error - problem in the remote end */
  2153. return AST_CAUSE_CONGESTION;
  2154. } else if (cause < 700 && cause >= 600) {
  2155. /* 6xx - global errors in the 4xx class */
  2156. return AST_CAUSE_INTERWORKING;
  2157. }
  2158. return AST_CAUSE_NORMAL;
  2159. }
  2160. /* Never reached */
  2161. return 0;
  2162. }
  2163. static void chan_pjsip_session_begin(struct ast_sip_session *session)
  2164. {
  2165. RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
  2166. if (session->endpoint->media.direct_media.glare_mitigation ==
  2167. AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE) {
  2168. return;
  2169. }
  2170. datastore = ast_sip_session_alloc_datastore(&direct_media_mitigation_info,
  2171. "direct_media_glare_mitigation");
  2172. if (!datastore) {
  2173. return;
  2174. }
  2175. ast_sip_session_add_datastore(session, datastore);
  2176. }
  2177. /*! \brief Function called when the session ends */
  2178. static void chan_pjsip_session_end(struct ast_sip_session *session)
  2179. {
  2180. if (!session->channel) {
  2181. return;
  2182. }
  2183. chan_pjsip_remove_hold(ast_channel_uniqueid(session->channel));
  2184. ast_set_hangupsource(session->channel, ast_channel_name(session->channel), 0);
  2185. if (!ast_channel_hangupcause(session->channel) && session->inv_session) {
  2186. int cause = hangup_sip2cause(session->inv_session->cause);
  2187. ast_queue_hangup_with_cause(session->channel, cause);
  2188. } else {
  2189. ast_queue_hangup(session->channel);
  2190. }
  2191. }
  2192. /*! \brief Function called when a request is received on the session */
  2193. static int chan_pjsip_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
  2194. {
  2195. RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
  2196. struct transport_info_data *transport_data;
  2197. pjsip_tx_data *packet = NULL;
  2198. if (session->channel) {
  2199. return 0;
  2200. }
  2201. /* Check for a to-tag to determine if this is a reinvite */
  2202. if (rdata->msg_info.to->tag.slen) {
  2203. /* Weird case. We've received a reinvite but we don't have a channel. The most
  2204. * typical case for this happening is that a blind transfer fails, and so the
  2205. * transferer attempts to reinvite himself back into the call. We already got
  2206. * rid of that channel, and the other side of the call is unrecoverable.
  2207. *
  2208. * We treat this as a failure, so our best bet is to just hang this call
  2209. * up and not create a new channel. Clearing defer_terminate here ensures that
  2210. * calling ast_sip_session_terminate() can result in a BYE being sent ASAP.
  2211. */
  2212. session->defer_terminate = 0;
  2213. ast_sip_session_terminate(session, 400);
  2214. return -1;
  2215. }
  2216. datastore = ast_sip_session_alloc_datastore(&transport_info, "transport_info");
  2217. if (!datastore) {
  2218. return -1;
  2219. }
  2220. transport_data = ast_calloc(1, sizeof(*transport_data));
  2221. if (!transport_data) {
  2222. return -1;
  2223. }
  2224. pj_sockaddr_cp(&transport_data->local_addr, &rdata->tp_info.transport->local_addr);
  2225. pj_sockaddr_cp(&transport_data->remote_addr, &rdata->pkt_info.src_addr);
  2226. datastore->data = transport_data;
  2227. ast_sip_session_add_datastore(session, datastore);
  2228. if (!(session->channel = chan_pjsip_new(session, AST_STATE_RING, session->exten, NULL, NULL, NULL, NULL))) {
  2229. if (pjsip_inv_end_session(session->inv_session, 503, NULL, &packet) == PJ_SUCCESS
  2230. && packet) {
  2231. ast_sip_session_send_response(session, packet);
  2232. }
  2233. ast_log(LOG_ERROR, "Failed to allocate new PJSIP channel on incoming SIP INVITE\n");
  2234. return -1;
  2235. }
  2236. /* channel gets created on incoming request, but we wait to call start
  2237. so other supplements have a chance to run */
  2238. return 0;
  2239. }
  2240. static int call_pickup_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
  2241. {
  2242. struct ast_features_pickup_config *pickup_cfg;
  2243. struct ast_channel *chan;
  2244. /* Check for a to-tag to determine if this is a reinvite */
  2245. if (rdata->msg_info.to->tag.slen) {
  2246. /* We don't care about reinvites */
  2247. return 0;
  2248. }
  2249. pickup_cfg = ast_get_chan_features_pickup_config(session->channel);
  2250. if (!pickup_cfg) {
  2251. ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension.\n");
  2252. return 0;
  2253. }
  2254. if (strcmp(session->exten, pickup_cfg->pickupexten)) {
  2255. ao2_ref(pickup_cfg, -1);
  2256. return 0;
  2257. }
  2258. ao2_ref(pickup_cfg, -1);
  2259. /* We can't directly use session->channel because the pickup operation will cause a masquerade to occur,
  2260. * changing the channel pointer in session to a different channel. To ensure we work on the right channel
  2261. * we store a pointer locally before we begin and keep a reference so it remains valid no matter what.
  2262. */
  2263. chan = ast_channel_ref(session->channel);
  2264. if (ast_pickup_call(chan)) {
  2265. ast_channel_hangupcause_set(chan, AST_CAUSE_CALL_REJECTED);
  2266. } else {
  2267. ast_channel_hangupcause_set(chan, AST_CAUSE_NORMAL_CLEARING);
  2268. }
  2269. /* A hangup always occurs because the pickup operation will have either failed resulting in the call
  2270. * needing to be hung up OR the pickup operation was a success and the channel we now have is actually
  2271. * the channel that was replaced, which should be hung up since it is literally in limbo not connected
  2272. * to anything at all.
  2273. */
  2274. ast_hangup(chan);
  2275. ast_channel_unref(chan);
  2276. return 1;
  2277. }
  2278. static struct ast_sip_session_supplement call_pickup_supplement = {
  2279. .method = "INVITE",
  2280. .priority = AST_SIP_SUPPLEMENT_PRIORITY_LAST - 1,
  2281. .incoming_request = call_pickup_incoming_request,
  2282. };
  2283. static int pbx_start_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
  2284. {
  2285. int res;
  2286. /* Check for a to-tag to determine if this is a reinvite */
  2287. if (rdata->msg_info.to->tag.slen) {
  2288. /* We don't care about reinvites */
  2289. return 0;
  2290. }
  2291. res = ast_pbx_start(session->channel);
  2292. switch (res) {
  2293. case AST_PBX_FAILED:
  2294. ast_log(LOG_WARNING, "Failed to start PBX ;(\n");
  2295. ast_channel_hangupcause_set(session->channel, AST_CAUSE_SWITCH_CONGESTION);
  2296. ast_hangup(session->channel);
  2297. break;
  2298. case AST_PBX_CALL_LIMIT:
  2299. ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
  2300. ast_channel_hangupcause_set(session->channel, AST_CAUSE_SWITCH_CONGESTION);
  2301. ast_hangup(session->channel);
  2302. break;
  2303. case AST_PBX_SUCCESS:
  2304. default:
  2305. break;
  2306. }
  2307. ast_debug(3, "Started PBX on new PJSIP channel %s\n", ast_channel_name(session->channel));
  2308. return (res == AST_PBX_SUCCESS) ? 0 : -1;
  2309. }
  2310. static struct ast_sip_session_supplement pbx_start_supplement = {
  2311. .method = "INVITE",
  2312. .priority = AST_SIP_SUPPLEMENT_PRIORITY_LAST,
  2313. .incoming_request = pbx_start_incoming_request,
  2314. };
  2315. /*! \brief Function called when a response is received on the session */
  2316. static void chan_pjsip_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
  2317. {
  2318. struct pjsip_status_line status = rdata->msg_info.msg->line.status;
  2319. struct ast_control_pvt_cause_code *cause_code;
  2320. int data_size = sizeof(*cause_code);
  2321. if (!session->channel) {
  2322. return;
  2323. }
  2324. /* Build and send the tech-specific cause information */
  2325. /* size of the string making up the cause code is "SIP " number + " " + reason length */
  2326. data_size += 4 + 4 + pj_strlen(&status.reason);
  2327. cause_code = ast_alloca(data_size);
  2328. memset(cause_code, 0, data_size);
  2329. ast_copy_string(cause_code->chan_name, ast_channel_name(session->channel), AST_CHANNEL_NAME);
  2330. snprintf(cause_code->code, data_size - sizeof(*cause_code) + 1, "SIP %d %.*s", status.code,
  2331. (int) pj_strlen(&status.reason), pj_strbuf(&status.reason));
  2332. cause_code->ast_cause = hangup_sip2cause(status.code);
  2333. ast_queue_control_data(session->channel, AST_CONTROL_PVT_CAUSE_CODE, cause_code, data_size);
  2334. ast_channel_hangupcause_hash_set(session->channel, cause_code, data_size);
  2335. switch (status.code) {
  2336. case 180:
  2337. ast_queue_control(session->channel, AST_CONTROL_RINGING);
  2338. ast_channel_lock(session->channel);
  2339. if (ast_channel_state(session->channel) != AST_STATE_UP) {
  2340. ast_setstate(session->channel, AST_STATE_RINGING);
  2341. }
  2342. ast_channel_unlock(session->channel);
  2343. break;
  2344. case 183:
  2345. ast_queue_control(session->channel, AST_CONTROL_PROGRESS);
  2346. break;
  2347. case 200:
  2348. ast_queue_control(session->channel, AST_CONTROL_ANSWER);
  2349. break;
  2350. default:
  2351. break;
  2352. }
  2353. }
  2354. static int chan_pjsip_incoming_ack(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
  2355. {
  2356. if (rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD) {
  2357. if (session->endpoint->media.direct_media.enabled && session->channel) {
  2358. ast_queue_control(session->channel, AST_CONTROL_SRCCHANGE);
  2359. }
  2360. }
  2361. return 0;
  2362. }
  2363. static int update_devstate(void *obj, void *arg, int flags)
  2364. {
  2365. ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE,
  2366. "PJSIP/%s", ast_sorcery_object_get_id(obj));
  2367. return 0;
  2368. }
  2369. static struct ast_custom_function chan_pjsip_dial_contacts_function = {
  2370. .name = "PJSIP_DIAL_CONTACTS",
  2371. .read = pjsip_acf_dial_contacts_read,
  2372. };
  2373. static struct ast_custom_function chan_pjsip_parse_uri_function = {
  2374. .name = "PJSIP_PARSE_URI",
  2375. .read = pjsip_acf_parse_uri_read,
  2376. };
  2377. static struct ast_custom_function media_offer_function = {
  2378. .name = "PJSIP_MEDIA_OFFER",
  2379. .read = pjsip_acf_media_offer_read,
  2380. .write = pjsip_acf_media_offer_write
  2381. };
  2382. static struct ast_custom_function dtmf_mode_function = {
  2383. .name = "PJSIP_DTMF_MODE",
  2384. .read = pjsip_acf_dtmf_mode_read,
  2385. .write = pjsip_acf_dtmf_mode_write
  2386. };
  2387. static struct ast_custom_function session_refresh_function = {
  2388. .name = "PJSIP_SEND_SESSION_REFRESH",
  2389. .write = pjsip_acf_session_refresh_write,
  2390. };
  2391. /*!
  2392. * \brief Load the module
  2393. *
  2394. * Module loading including tests for configuration or dependencies.
  2395. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  2396. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  2397. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  2398. * configuration file or other non-critical problem return
  2399. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  2400. */
  2401. static int load_module(void)
  2402. {
  2403. struct ao2_container *endpoints;
  2404. CHECK_PJSIP_SESSION_MODULE_LOADED();
  2405. if (!(chan_pjsip_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
  2406. return AST_MODULE_LOAD_DECLINE;
  2407. }
  2408. ast_format_cap_append_by_type(chan_pjsip_tech.capabilities, AST_MEDIA_TYPE_AUDIO);
  2409. ast_rtp_glue_register(&chan_pjsip_rtp_glue);
  2410. if (ast_channel_register(&chan_pjsip_tech)) {
  2411. ast_log(LOG_ERROR, "Unable to register channel class %s\n", channel_type);
  2412. goto end;
  2413. }
  2414. if (ast_custom_function_register(&chan_pjsip_dial_contacts_function)) {
  2415. ast_log(LOG_ERROR, "Unable to register PJSIP_DIAL_CONTACTS dialplan function\n");
  2416. goto end;
  2417. }
  2418. if (ast_custom_function_register(&chan_pjsip_parse_uri_function)) {
  2419. ast_log(LOG_ERROR, "Unable to register PJSIP_PARSE_URI dialplan function\n");
  2420. goto end;
  2421. }
  2422. if (ast_custom_function_register(&media_offer_function)) {
  2423. ast_log(LOG_WARNING, "Unable to register PJSIP_MEDIA_OFFER dialplan function\n");
  2424. goto end;
  2425. }
  2426. if (ast_custom_function_register(&dtmf_mode_function)) {
  2427. ast_log(LOG_WARNING, "Unable to register PJSIP_DTMF_MODE dialplan function\n");
  2428. goto end;
  2429. }
  2430. if (ast_custom_function_register(&session_refresh_function)) {
  2431. ast_log(LOG_WARNING, "Unable to register PJSIP_SEND_SESSION_REFRESH dialplan function\n");
  2432. goto end;
  2433. }
  2434. if (ast_sip_session_register_supplement(&chan_pjsip_supplement)) {
  2435. ast_log(LOG_ERROR, "Unable to register PJSIP supplement\n");
  2436. goto end;
  2437. }
  2438. if (ast_sip_session_register_supplement(&chan_pjsip_supplement_response)) {
  2439. ast_log(LOG_ERROR, "Unable to register PJSIP supplement\n");
  2440. goto end;
  2441. }
  2442. if (!(pjsip_uids_onhold = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK,
  2443. AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT, 37, uid_hold_hash_fn,
  2444. uid_hold_sort_fn, NULL))) {
  2445. ast_log(LOG_ERROR, "Unable to create held channels container\n");
  2446. goto end;
  2447. }
  2448. if (ast_sip_session_register_supplement(&call_pickup_supplement)) {
  2449. ast_log(LOG_ERROR, "Unable to register PJSIP call pickup supplement\n");
  2450. goto end;
  2451. }
  2452. if (ast_sip_session_register_supplement(&pbx_start_supplement)) {
  2453. ast_log(LOG_ERROR, "Unable to register PJSIP pbx start supplement\n");
  2454. goto end;
  2455. }
  2456. if (ast_sip_session_register_supplement(&chan_pjsip_ack_supplement)) {
  2457. ast_log(LOG_ERROR, "Unable to register PJSIP ACK supplement\n");
  2458. goto end;
  2459. }
  2460. if (pjsip_channel_cli_register()) {
  2461. ast_log(LOG_ERROR, "Unable to register PJSIP Channel CLI\n");
  2462. goto end;
  2463. }
  2464. /* since endpoints are loaded before the channel driver their device
  2465. states get set to 'invalid', so they need to be updated */
  2466. if ((endpoints = ast_sip_get_endpoints())) {
  2467. ao2_callback(endpoints, OBJ_NODATA, update_devstate, NULL);
  2468. ao2_ref(endpoints, -1);
  2469. }
  2470. return 0;
  2471. end:
  2472. ao2_cleanup(pjsip_uids_onhold);
  2473. pjsip_uids_onhold = NULL;
  2474. ast_sip_session_unregister_supplement(&chan_pjsip_ack_supplement);
  2475. ast_sip_session_unregister_supplement(&pbx_start_supplement);
  2476. ast_sip_session_unregister_supplement(&chan_pjsip_supplement_response);
  2477. ast_sip_session_unregister_supplement(&chan_pjsip_supplement);
  2478. ast_sip_session_unregister_supplement(&call_pickup_supplement);
  2479. ast_custom_function_unregister(&dtmf_mode_function);
  2480. ast_custom_function_unregister(&media_offer_function);
  2481. ast_custom_function_unregister(&chan_pjsip_dial_contacts_function);
  2482. ast_custom_function_unregister(&chan_pjsip_parse_uri_function);
  2483. ast_custom_function_unregister(&session_refresh_function);
  2484. ast_channel_unregister(&chan_pjsip_tech);
  2485. ast_rtp_glue_unregister(&chan_pjsip_rtp_glue);
  2486. return AST_MODULE_LOAD_DECLINE;
  2487. }
  2488. /*! \brief Unload the PJSIP channel from Asterisk */
  2489. static int unload_module(void)
  2490. {
  2491. ao2_cleanup(pjsip_uids_onhold);
  2492. pjsip_uids_onhold = NULL;
  2493. pjsip_channel_cli_unregister();
  2494. ast_sip_session_unregister_supplement(&chan_pjsip_supplement_response);
  2495. ast_sip_session_unregister_supplement(&chan_pjsip_supplement);
  2496. ast_sip_session_unregister_supplement(&pbx_start_supplement);
  2497. ast_sip_session_unregister_supplement(&chan_pjsip_ack_supplement);
  2498. ast_sip_session_unregister_supplement(&call_pickup_supplement);
  2499. ast_custom_function_unregister(&dtmf_mode_function);
  2500. ast_custom_function_unregister(&media_offer_function);
  2501. ast_custom_function_unregister(&chan_pjsip_dial_contacts_function);
  2502. ast_custom_function_unregister(&chan_pjsip_parse_uri_function);
  2503. ast_custom_function_unregister(&session_refresh_function);
  2504. ast_channel_unregister(&chan_pjsip_tech);
  2505. ao2_ref(chan_pjsip_tech.capabilities, -1);
  2506. ast_rtp_glue_unregister(&chan_pjsip_rtp_glue);
  2507. return 0;
  2508. }
  2509. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Channel Driver",
  2510. .support_level = AST_MODULE_SUPPORT_CORE,
  2511. .load = load_module,
  2512. .unload = unload_module,
  2513. .load_pri = AST_MODPRI_CHANNEL_DRIVER,
  2514. );