chan_pjsip.c 72 KB

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