res_pjsip_session.c 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Mark Michelson <mmichelson@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. /*** MODULEINFO
  19. <depend>pjproject</depend>
  20. <depend>res_pjsip</depend>
  21. <support_level>core</support_level>
  22. ***/
  23. #include "asterisk.h"
  24. #include <pjsip.h>
  25. #include <pjsip_ua.h>
  26. #include <pjlib.h>
  27. #include "asterisk/res_pjsip.h"
  28. #include "asterisk/res_pjsip_session.h"
  29. #include "asterisk/callerid.h"
  30. #include "asterisk/datastore.h"
  31. #include "asterisk/module.h"
  32. #include "asterisk/logger.h"
  33. #include "asterisk/res_pjsip.h"
  34. #include "asterisk/astobj2.h"
  35. #include "asterisk/lock.h"
  36. #include "asterisk/uuid.h"
  37. #include "asterisk/pbx.h"
  38. #include "asterisk/taskprocessor.h"
  39. #include "asterisk/causes.h"
  40. #include "asterisk/sdp_srtp.h"
  41. #include "asterisk/dsp.h"
  42. #include "asterisk/acl.h"
  43. #include "asterisk/features_config.h"
  44. #include "asterisk/pickup.h"
  45. #include "asterisk/test.h"
  46. #define SDP_HANDLER_BUCKETS 11
  47. #define MOD_DATA_ON_RESPONSE "on_response"
  48. #define MOD_DATA_NAT_HOOK "nat_hook"
  49. /* Some forward declarations */
  50. static void handle_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata);
  51. static void handle_incoming_response(struct ast_sip_session *session, pjsip_rx_data *rdata,
  52. enum ast_sip_session_response_priority response_priority);
  53. static int handle_incoming(struct ast_sip_session *session, pjsip_rx_data *rdata,
  54. enum ast_sip_session_response_priority response_priority);
  55. static void handle_outgoing_request(struct ast_sip_session *session, pjsip_tx_data *tdata);
  56. static void handle_outgoing_response(struct ast_sip_session *session, pjsip_tx_data *tdata);
  57. /*! \brief NAT hook for modifying outgoing messages with SDP */
  58. static struct ast_sip_nat_hook *nat_hook;
  59. /*!
  60. * \brief Registered SDP stream handlers
  61. *
  62. * This container is keyed on stream types. Each
  63. * object in the container is a linked list of
  64. * handlers for the stream type.
  65. */
  66. static struct ao2_container *sdp_handlers;
  67. /*!
  68. * These are the objects in the sdp_handlers container
  69. */
  70. struct sdp_handler_list {
  71. /* The list of handlers to visit */
  72. AST_LIST_HEAD_NOLOCK(, ast_sip_session_sdp_handler) list;
  73. /* The handlers in this list handle streams of this type */
  74. char stream_type[1];
  75. };
  76. static struct pjmedia_sdp_session *create_local_sdp(pjsip_inv_session *inv, struct ast_sip_session *session, const pjmedia_sdp_session *offer);
  77. static int sdp_handler_list_hash(const void *obj, int flags)
  78. {
  79. const struct sdp_handler_list *handler_list = obj;
  80. const char *stream_type = flags & OBJ_KEY ? obj : handler_list->stream_type;
  81. return ast_str_hash(stream_type);
  82. }
  83. static int sdp_handler_list_cmp(void *obj, void *arg, int flags)
  84. {
  85. struct sdp_handler_list *handler_list1 = obj;
  86. struct sdp_handler_list *handler_list2 = arg;
  87. const char *stream_type2 = flags & OBJ_KEY ? arg : handler_list2->stream_type;
  88. return strcmp(handler_list1->stream_type, stream_type2) ? 0 : CMP_MATCH | CMP_STOP;
  89. }
  90. static int session_media_hash(const void *obj, int flags)
  91. {
  92. const struct ast_sip_session_media *session_media = obj;
  93. const char *stream_type = flags & OBJ_KEY ? obj : session_media->stream_type;
  94. return ast_str_hash(stream_type);
  95. }
  96. static int session_media_cmp(void *obj, void *arg, int flags)
  97. {
  98. struct ast_sip_session_media *session_media1 = obj;
  99. struct ast_sip_session_media *session_media2 = arg;
  100. const char *stream_type2 = flags & OBJ_KEY ? arg : session_media2->stream_type;
  101. return strcmp(session_media1->stream_type, stream_type2) ? 0 : CMP_MATCH | CMP_STOP;
  102. }
  103. int ast_sip_session_register_sdp_handler(struct ast_sip_session_sdp_handler *handler, const char *stream_type)
  104. {
  105. RAII_VAR(struct sdp_handler_list *, handler_list,
  106. ao2_find(sdp_handlers, stream_type, OBJ_KEY), ao2_cleanup);
  107. SCOPED_AO2LOCK(lock, sdp_handlers);
  108. if (handler_list) {
  109. struct ast_sip_session_sdp_handler *iter;
  110. /* Check if this handler is already registered for this stream type */
  111. AST_LIST_TRAVERSE(&handler_list->list, iter, next) {
  112. if (!strcmp(iter->id, handler->id)) {
  113. ast_log(LOG_WARNING, "Handler '%s' already registered for stream type '%s'.\n", handler->id, stream_type);
  114. return -1;
  115. }
  116. }
  117. AST_LIST_INSERT_TAIL(&handler_list->list, handler, next);
  118. ast_debug(1, "Registered SDP stream handler '%s' for stream type '%s'\n", handler->id, stream_type);
  119. ast_module_ref(ast_module_info->self);
  120. return 0;
  121. }
  122. /* No stream of this type has been registered yet, so we need to create a new list */
  123. handler_list = ao2_alloc(sizeof(*handler_list) + strlen(stream_type), NULL);
  124. if (!handler_list) {
  125. return -1;
  126. }
  127. /* Safe use of strcpy */
  128. strcpy(handler_list->stream_type, stream_type);
  129. AST_LIST_HEAD_INIT_NOLOCK(&handler_list->list);
  130. AST_LIST_INSERT_TAIL(&handler_list->list, handler, next);
  131. if (!ao2_link(sdp_handlers, handler_list)) {
  132. return -1;
  133. }
  134. ast_debug(1, "Registered SDP stream handler '%s' for stream type '%s'\n", handler->id, stream_type);
  135. ast_module_ref(ast_module_info->self);
  136. return 0;
  137. }
  138. static int remove_handler(void *obj, void *arg, void *data, int flags)
  139. {
  140. struct sdp_handler_list *handler_list = obj;
  141. struct ast_sip_session_sdp_handler *handler = data;
  142. struct ast_sip_session_sdp_handler *iter;
  143. const char *stream_type = arg;
  144. AST_LIST_TRAVERSE_SAFE_BEGIN(&handler_list->list, iter, next) {
  145. if (!strcmp(iter->id, handler->id)) {
  146. AST_LIST_REMOVE_CURRENT(next);
  147. ast_debug(1, "Unregistered SDP stream handler '%s' for stream type '%s'\n", handler->id, stream_type);
  148. ast_module_unref(ast_module_info->self);
  149. }
  150. }
  151. AST_LIST_TRAVERSE_SAFE_END;
  152. if (AST_LIST_EMPTY(&handler_list->list)) {
  153. ast_debug(3, "No more handlers exist for stream type '%s'\n", stream_type);
  154. return CMP_MATCH;
  155. } else {
  156. return CMP_STOP;
  157. }
  158. }
  159. void ast_sip_session_unregister_sdp_handler(struct ast_sip_session_sdp_handler *handler, const char *stream_type)
  160. {
  161. ao2_callback_data(sdp_handlers, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA, remove_handler, (void *)stream_type, handler);
  162. }
  163. /*!
  164. * \brief Set an SDP stream handler for a corresponding session media.
  165. *
  166. * \note Always use this function to set the SDP handler for a session media.
  167. *
  168. * This function will properly free resources on the SDP handler currently being
  169. * used by the session media, then set the session media to use the new SDP
  170. * handler.
  171. */
  172. static void session_media_set_handler(struct ast_sip_session_media *session_media,
  173. struct ast_sip_session_sdp_handler *handler)
  174. {
  175. ast_assert(session_media->handler != handler);
  176. if (session_media->handler) {
  177. session_media->handler->stream_destroy(session_media);
  178. }
  179. session_media->handler = handler;
  180. }
  181. static int handle_incoming_sdp(struct ast_sip_session *session, const pjmedia_sdp_session *sdp)
  182. {
  183. int i;
  184. int handled = 0;
  185. if (session->inv_session && session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
  186. ast_log(LOG_ERROR, "Failed to handle incoming SDP. Session has been already disconnected\n");
  187. return -1;
  188. }
  189. for (i = 0; i < sdp->media_count; ++i) {
  190. /* See if there are registered handlers for this media stream type */
  191. char media[20];
  192. struct ast_sip_session_sdp_handler *handler;
  193. RAII_VAR(struct sdp_handler_list *, handler_list, NULL, ao2_cleanup);
  194. RAII_VAR(struct ast_sip_session_media *, session_media, NULL, ao2_cleanup);
  195. int res;
  196. /* We need a null-terminated version of the media string */
  197. ast_copy_pj_str(media, &sdp->media[i]->desc.media, sizeof(media));
  198. session_media = ao2_find(session->media, media, OBJ_KEY);
  199. if (!session_media) {
  200. /* if the session_media doesn't exist, there weren't
  201. * any handlers at the time of its creation */
  202. continue;
  203. }
  204. if (session_media->handler) {
  205. handler = session_media->handler;
  206. ast_debug(1, "Negotiating incoming SDP media stream '%s' using %s SDP handler\n",
  207. session_media->stream_type,
  208. session_media->handler->id);
  209. res = handler->negotiate_incoming_sdp_stream(session, session_media, sdp,
  210. sdp->media[i]);
  211. if (res < 0) {
  212. /* Catastrophic failure. Abort! */
  213. return -1;
  214. } else if (res > 0) {
  215. ast_debug(1, "Media stream '%s' handled by %s\n",
  216. session_media->stream_type,
  217. session_media->handler->id);
  218. /* Handled by this handler. Move to the next stream */
  219. handled = 1;
  220. continue;
  221. }
  222. }
  223. handler_list = ao2_find(sdp_handlers, media, OBJ_KEY);
  224. if (!handler_list) {
  225. ast_debug(1, "No registered SDP handlers for media type '%s'\n", media);
  226. continue;
  227. }
  228. AST_LIST_TRAVERSE(&handler_list->list, handler, next) {
  229. if (handler == session_media->handler) {
  230. continue;
  231. }
  232. ast_debug(1, "Negotiating incoming SDP media stream '%s' using %s SDP handler\n",
  233. session_media->stream_type,
  234. handler->id);
  235. res = handler->negotiate_incoming_sdp_stream(session, session_media, sdp,
  236. sdp->media[i]);
  237. if (res < 0) {
  238. /* Catastrophic failure. Abort! */
  239. return -1;
  240. }
  241. if (res > 0) {
  242. ast_debug(1, "Media stream '%s' handled by %s\n",
  243. session_media->stream_type,
  244. handler->id);
  245. /* Handled by this handler. Move to the next stream */
  246. session_media_set_handler(session_media, handler);
  247. handled = 1;
  248. break;
  249. }
  250. }
  251. }
  252. if (!handled) {
  253. return -1;
  254. }
  255. return 0;
  256. }
  257. struct handle_negotiated_sdp_cb {
  258. struct ast_sip_session *session;
  259. const pjmedia_sdp_session *local;
  260. const pjmedia_sdp_session *remote;
  261. };
  262. static int handle_negotiated_sdp_session_media(void *obj, void *arg, int flags)
  263. {
  264. struct ast_sip_session_media *session_media = obj;
  265. struct handle_negotiated_sdp_cb *callback_data = arg;
  266. struct ast_sip_session *session = callback_data->session;
  267. const pjmedia_sdp_session *local = callback_data->local;
  268. const pjmedia_sdp_session *remote = callback_data->remote;
  269. int i;
  270. for (i = 0; i < local->media_count; ++i) {
  271. /* See if there are registered handlers for this media stream type */
  272. char media[20];
  273. struct ast_sip_session_sdp_handler *handler;
  274. RAII_VAR(struct sdp_handler_list *, handler_list, NULL, ao2_cleanup);
  275. int res;
  276. if (!remote->media[i]) {
  277. continue;
  278. }
  279. /* We need a null-terminated version of the media string */
  280. ast_copy_pj_str(media, &local->media[i]->desc.media, sizeof(media));
  281. /* stream type doesn't match the one we're looking to fill */
  282. if (strcasecmp(session_media->stream_type, media)) {
  283. continue;
  284. }
  285. handler = session_media->handler;
  286. if (handler) {
  287. ast_debug(1, "Applying negotiated SDP media stream '%s' using %s SDP handler\n",
  288. session_media->stream_type,
  289. handler->id);
  290. res = handler->apply_negotiated_sdp_stream(session, session_media, local,
  291. local->media[i], remote, remote->media[i]);
  292. if (res >= 0) {
  293. ast_debug(1, "Applied negotiated SDP media stream '%s' using %s SDP handler\n",
  294. session_media->stream_type,
  295. handler->id);
  296. return CMP_MATCH;
  297. }
  298. return 0;
  299. }
  300. handler_list = ao2_find(sdp_handlers, media, OBJ_KEY);
  301. if (!handler_list) {
  302. ast_debug(1, "No registered SDP handlers for media type '%s'\n", media);
  303. continue;
  304. }
  305. AST_LIST_TRAVERSE(&handler_list->list, handler, next) {
  306. if (handler == session_media->handler) {
  307. continue;
  308. }
  309. ast_debug(1, "Applying negotiated SDP media stream '%s' using %s SDP handler\n",
  310. session_media->stream_type,
  311. handler->id);
  312. res = handler->apply_negotiated_sdp_stream(session, session_media, local,
  313. local->media[i], remote, remote->media[i]);
  314. if (res < 0) {
  315. /* Catastrophic failure. Abort! */
  316. return 0;
  317. }
  318. if (res > 0) {
  319. ast_debug(1, "Applied negotiated SDP media stream '%s' using %s SDP handler\n",
  320. session_media->stream_type,
  321. handler->id);
  322. /* Handled by this handler. Move to the next stream */
  323. session_media_set_handler(session_media, handler);
  324. return CMP_MATCH;
  325. }
  326. }
  327. }
  328. if (session_media->handler && session_media->handler->stream_stop) {
  329. ast_debug(1, "Stopping SDP media stream '%s' as it is not currently negotiated\n",
  330. session_media->stream_type);
  331. session_media->handler->stream_stop(session_media);
  332. }
  333. return CMP_MATCH;
  334. }
  335. static int handle_negotiated_sdp(struct ast_sip_session *session, const pjmedia_sdp_session *local, const pjmedia_sdp_session *remote)
  336. {
  337. RAII_VAR(struct ao2_iterator *, successful, NULL, ao2_iterator_cleanup);
  338. struct handle_negotiated_sdp_cb callback_data = {
  339. .session = session,
  340. .local = local,
  341. .remote = remote,
  342. };
  343. successful = ao2_callback(session->media, OBJ_MULTIPLE, handle_negotiated_sdp_session_media, &callback_data);
  344. if (successful && ao2_iterator_count(successful) == ao2_container_count(session->media)) {
  345. /* Nothing experienced a catastrophic failure */
  346. ast_queue_frame(session->channel, &ast_null_frame);
  347. return 0;
  348. }
  349. return -1;
  350. }
  351. #define DATASTORE_BUCKETS 53
  352. #define MEDIA_BUCKETS 7
  353. static void session_datastore_destroy(void *obj)
  354. {
  355. struct ast_datastore *datastore = obj;
  356. /* Using the destroy function (if present) destroy the data */
  357. if (datastore->info->destroy != NULL && datastore->data != NULL) {
  358. datastore->info->destroy(datastore->data);
  359. datastore->data = NULL;
  360. }
  361. ast_free((void *) datastore->uid);
  362. datastore->uid = NULL;
  363. }
  364. struct ast_datastore *ast_sip_session_alloc_datastore(const struct ast_datastore_info *info, const char *uid)
  365. {
  366. RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
  367. char uuid_buf[AST_UUID_STR_LEN];
  368. const char *uid_ptr = uid;
  369. if (!info) {
  370. return NULL;
  371. }
  372. datastore = ao2_alloc(sizeof(*datastore), session_datastore_destroy);
  373. if (!datastore) {
  374. return NULL;
  375. }
  376. datastore->info = info;
  377. if (ast_strlen_zero(uid)) {
  378. /* They didn't provide an ID so we'll provide one ourself */
  379. uid_ptr = ast_uuid_generate_str(uuid_buf, sizeof(uuid_buf));
  380. }
  381. datastore->uid = ast_strdup(uid_ptr);
  382. if (!datastore->uid) {
  383. return NULL;
  384. }
  385. ao2_ref(datastore, +1);
  386. return datastore;
  387. }
  388. int ast_sip_session_add_datastore(struct ast_sip_session *session, struct ast_datastore *datastore)
  389. {
  390. ast_assert(datastore != NULL);
  391. ast_assert(datastore->info != NULL);
  392. ast_assert(ast_strlen_zero(datastore->uid) == 0);
  393. if (!ao2_link(session->datastores, datastore)) {
  394. return -1;
  395. }
  396. return 0;
  397. }
  398. struct ast_datastore *ast_sip_session_get_datastore(struct ast_sip_session *session, const char *name)
  399. {
  400. return ao2_find(session->datastores, name, OBJ_KEY);
  401. }
  402. void ast_sip_session_remove_datastore(struct ast_sip_session *session, const char *name)
  403. {
  404. ao2_callback(session->datastores, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA, NULL, (void *) name);
  405. }
  406. enum delayed_method {
  407. DELAYED_METHOD_INVITE,
  408. DELAYED_METHOD_UPDATE,
  409. DELAYED_METHOD_BYE,
  410. };
  411. /*!
  412. * \internal
  413. * \brief Convert delayed method enum value to a string.
  414. * \since 13.3.0
  415. *
  416. * \param method Delayed method enum value to convert to a string.
  417. *
  418. * \return String value of delayed method.
  419. */
  420. static const char *delayed_method2str(enum delayed_method method)
  421. {
  422. const char *str = "<unknown>";
  423. switch (method) {
  424. case DELAYED_METHOD_INVITE:
  425. str = "INVITE";
  426. break;
  427. case DELAYED_METHOD_UPDATE:
  428. str = "UPDATE";
  429. break;
  430. case DELAYED_METHOD_BYE:
  431. str = "BYE";
  432. break;
  433. }
  434. return str;
  435. }
  436. /*!
  437. * \brief Structure used for sending delayed requests
  438. *
  439. * Requests are typically delayed because the current transaction
  440. * state of an INVITE. Once the pending INVITE transaction terminates,
  441. * the delayed request will be sent
  442. */
  443. struct ast_sip_session_delayed_request {
  444. /*! Method of the request */
  445. enum delayed_method method;
  446. /*! Callback to call when the delayed request is created. */
  447. ast_sip_session_request_creation_cb on_request_creation;
  448. /*! Callback to call when the delayed request SDP is created */
  449. ast_sip_session_sdp_creation_cb on_sdp_creation;
  450. /*! Callback to call when the delayed request receives a response */
  451. ast_sip_session_response_cb on_response;
  452. /*! Whether to generate new SDP */
  453. int generate_new_sdp;
  454. AST_LIST_ENTRY(ast_sip_session_delayed_request) next;
  455. };
  456. static struct ast_sip_session_delayed_request *delayed_request_alloc(
  457. enum delayed_method method,
  458. ast_sip_session_request_creation_cb on_request_creation,
  459. ast_sip_session_sdp_creation_cb on_sdp_creation,
  460. ast_sip_session_response_cb on_response,
  461. int generate_new_sdp)
  462. {
  463. struct ast_sip_session_delayed_request *delay = ast_calloc(1, sizeof(*delay));
  464. if (!delay) {
  465. return NULL;
  466. }
  467. delay->method = method;
  468. delay->on_request_creation = on_request_creation;
  469. delay->on_sdp_creation = on_sdp_creation;
  470. delay->on_response = on_response;
  471. delay->generate_new_sdp = generate_new_sdp;
  472. return delay;
  473. }
  474. static int send_delayed_request(struct ast_sip_session *session, struct ast_sip_session_delayed_request *delay)
  475. {
  476. ast_debug(3, "Endpoint '%s(%s)' sending delayed %s request.\n",
  477. ast_sorcery_object_get_id(session->endpoint),
  478. session->channel ? ast_channel_name(session->channel) : "",
  479. delayed_method2str(delay->method));
  480. switch (delay->method) {
  481. case DELAYED_METHOD_INVITE:
  482. ast_sip_session_refresh(session, delay->on_request_creation,
  483. delay->on_sdp_creation, delay->on_response,
  484. AST_SIP_SESSION_REFRESH_METHOD_INVITE, delay->generate_new_sdp);
  485. return 0;
  486. case DELAYED_METHOD_UPDATE:
  487. ast_sip_session_refresh(session, delay->on_request_creation,
  488. delay->on_sdp_creation, delay->on_response,
  489. AST_SIP_SESSION_REFRESH_METHOD_UPDATE, delay->generate_new_sdp);
  490. return 0;
  491. case DELAYED_METHOD_BYE:
  492. ast_sip_session_terminate(session, 0);
  493. return 0;
  494. }
  495. ast_log(LOG_WARNING, "Don't know how to send delayed %s(%d) request.\n",
  496. delayed_method2str(delay->method), delay->method);
  497. return -1;
  498. }
  499. /*!
  500. * \internal
  501. * \brief The current INVITE transaction is in the PROCEEDING state.
  502. * \since 13.3.0
  503. *
  504. * \param vsession Session object.
  505. *
  506. * \retval 0 on success.
  507. * \retval -1 on error.
  508. */
  509. static int invite_proceeding(void *vsession)
  510. {
  511. struct ast_sip_session *session = vsession;
  512. struct ast_sip_session_delayed_request *delay;
  513. int found = 0;
  514. int res = 0;
  515. AST_LIST_TRAVERSE_SAFE_BEGIN(&session->delayed_requests, delay, next) {
  516. switch (delay->method) {
  517. case DELAYED_METHOD_INVITE:
  518. break;
  519. case DELAYED_METHOD_UPDATE:
  520. AST_LIST_REMOVE_CURRENT(next);
  521. res = send_delayed_request(session, delay);
  522. ast_free(delay);
  523. found = 1;
  524. break;
  525. case DELAYED_METHOD_BYE:
  526. /* A BYE is pending so don't bother anymore. */
  527. found = 1;
  528. break;
  529. }
  530. if (found) {
  531. break;
  532. }
  533. }
  534. AST_LIST_TRAVERSE_SAFE_END;
  535. ao2_ref(session, -1);
  536. return res;
  537. }
  538. /*!
  539. * \internal
  540. * \brief The current INVITE transaction is in the TERMINATED state.
  541. * \since 13.3.0
  542. *
  543. * \param vsession Session object.
  544. *
  545. * \retval 0 on success.
  546. * \retval -1 on error.
  547. */
  548. static int invite_terminated(void *vsession)
  549. {
  550. struct ast_sip_session *session = vsession;
  551. struct ast_sip_session_delayed_request *delay;
  552. int found = 0;
  553. int res = 0;
  554. int timer_running;
  555. /* re-INVITE collision timer running? */
  556. timer_running = pj_timer_entry_running(&session->rescheduled_reinvite);
  557. AST_LIST_TRAVERSE_SAFE_BEGIN(&session->delayed_requests, delay, next) {
  558. switch (delay->method) {
  559. case DELAYED_METHOD_INVITE:
  560. if (!timer_running) {
  561. found = 1;
  562. }
  563. break;
  564. case DELAYED_METHOD_UPDATE:
  565. case DELAYED_METHOD_BYE:
  566. found = 1;
  567. break;
  568. }
  569. if (found) {
  570. AST_LIST_REMOVE_CURRENT(next);
  571. res = send_delayed_request(session, delay);
  572. ast_free(delay);
  573. break;
  574. }
  575. }
  576. AST_LIST_TRAVERSE_SAFE_END;
  577. ao2_ref(session, -1);
  578. return res;
  579. }
  580. /*!
  581. * \internal
  582. * \brief INVITE collision timeout.
  583. * \since 13.3.0
  584. *
  585. * \param vsession Session object.
  586. *
  587. * \retval 0 on success.
  588. * \retval -1 on error.
  589. */
  590. static int invite_collision_timeout(void *vsession)
  591. {
  592. struct ast_sip_session *session = vsession;
  593. int res;
  594. if (session->inv_session->invite_tsx) {
  595. /*
  596. * INVITE transaction still active. Let it send
  597. * the collision re-INVITE when it terminates.
  598. */
  599. ao2_ref(session, -1);
  600. res = 0;
  601. } else {
  602. res = invite_terminated(session);
  603. }
  604. return res;
  605. }
  606. /*!
  607. * \internal
  608. * \brief The current UPDATE transaction is in the COMPLETED state.
  609. * \since 13.3.0
  610. *
  611. * \param vsession Session object.
  612. *
  613. * \retval 0 on success.
  614. * \retval -1 on error.
  615. */
  616. static int update_completed(void *vsession)
  617. {
  618. struct ast_sip_session *session = vsession;
  619. int res;
  620. if (session->inv_session->invite_tsx) {
  621. res = invite_proceeding(session);
  622. } else {
  623. res = invite_terminated(session);
  624. }
  625. return res;
  626. }
  627. static void check_delayed_requests(struct ast_sip_session *session,
  628. int (*cb)(void *vsession))
  629. {
  630. ao2_ref(session, +1);
  631. if (ast_sip_push_task(session->serializer, cb, session)) {
  632. ao2_ref(session, -1);
  633. }
  634. }
  635. static int delay_request(struct ast_sip_session *session,
  636. ast_sip_session_request_creation_cb on_request,
  637. ast_sip_session_sdp_creation_cb on_sdp_creation,
  638. ast_sip_session_response_cb on_response,
  639. int generate_new_sdp,
  640. enum delayed_method method)
  641. {
  642. struct ast_sip_session_delayed_request *delay = delayed_request_alloc(method,
  643. on_request, on_sdp_creation, on_response, generate_new_sdp);
  644. if (!delay) {
  645. return -1;
  646. }
  647. if (method == DELAYED_METHOD_BYE) {
  648. /* Send BYE as early as possible */
  649. AST_LIST_INSERT_HEAD(&session->delayed_requests, delay, next);
  650. } else {
  651. AST_LIST_INSERT_TAIL(&session->delayed_requests, delay, next);
  652. }
  653. return 0;
  654. }
  655. static pjmedia_sdp_session *generate_session_refresh_sdp(struct ast_sip_session *session)
  656. {
  657. pjsip_inv_session *inv_session = session->inv_session;
  658. const pjmedia_sdp_session *previous_sdp = NULL;
  659. if (inv_session->neg) {
  660. if (pjmedia_sdp_neg_was_answer_remote(inv_session->neg)) {
  661. pjmedia_sdp_neg_get_active_remote(inv_session->neg, &previous_sdp);
  662. } else {
  663. pjmedia_sdp_neg_get_active_local(inv_session->neg, &previous_sdp);
  664. }
  665. }
  666. return create_local_sdp(inv_session, session, previous_sdp);
  667. }
  668. static void set_from_header(struct ast_sip_session *session)
  669. {
  670. struct ast_party_id effective_id;
  671. struct ast_party_id connected_id;
  672. pj_pool_t *dlg_pool;
  673. pjsip_fromto_hdr *dlg_info;
  674. pjsip_contact_hdr *dlg_contact;
  675. pjsip_name_addr *dlg_info_name_addr;
  676. pjsip_sip_uri *dlg_info_uri;
  677. pjsip_sip_uri *dlg_contact_uri;
  678. int restricted;
  679. if (!session->channel || session->saved_from_hdr) {
  680. return;
  681. }
  682. /* We need to save off connected_id for RPID/PAI generation */
  683. ast_party_id_init(&connected_id);
  684. ast_channel_lock(session->channel);
  685. effective_id = ast_channel_connected_effective_id(session->channel);
  686. ast_party_id_copy(&connected_id, &effective_id);
  687. ast_channel_unlock(session->channel);
  688. restricted =
  689. ((ast_party_id_presentation(&connected_id) & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED);
  690. /* Now set up dlg->local.info so pjsip can correctly generate From */
  691. dlg_pool = session->inv_session->dlg->pool;
  692. dlg_info = session->inv_session->dlg->local.info;
  693. dlg_contact = session->inv_session->dlg->local.contact;
  694. dlg_info_name_addr = (pjsip_name_addr *) dlg_info->uri;
  695. dlg_info_uri = pjsip_uri_get_uri(dlg_info_name_addr);
  696. dlg_contact_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(dlg_contact->uri);
  697. if (session->endpoint->id.trust_outbound || !restricted) {
  698. ast_sip_modify_id_header(dlg_pool, dlg_info, &connected_id);
  699. if (ast_sip_get_use_callerid_contact() && ast_strlen_zero(session->endpoint->contact_user)) {
  700. pj_strdup2(dlg_pool, &dlg_contact_uri->user, S_COR(connected_id.number.valid, connected_id.number.str, ""));
  701. }
  702. }
  703. ast_party_id_free(&connected_id);
  704. if (!ast_strlen_zero(session->endpoint->fromuser)) {
  705. dlg_info_name_addr->display.ptr = NULL;
  706. dlg_info_name_addr->display.slen = 0;
  707. pj_strdup2(dlg_pool, &dlg_info_uri->user, session->endpoint->fromuser);
  708. }
  709. if (!ast_strlen_zero(session->endpoint->fromdomain)) {
  710. pj_strdup2(dlg_pool, &dlg_info_uri->host, session->endpoint->fromdomain);
  711. }
  712. /* We need to save off the non-anonymized From for RPID/PAI generation (for domain) */
  713. session->saved_from_hdr = pjsip_hdr_clone(dlg_pool, dlg_info);
  714. ast_sip_add_usereqphone(session->endpoint, dlg_pool, session->saved_from_hdr->uri);
  715. /* In chan_sip, fromuser and fromdomain trump restricted so we only
  716. * anonymize if they're not set.
  717. */
  718. if (restricted) {
  719. /* fromuser doesn't provide a display name so we always set it */
  720. pj_strdup2(dlg_pool, &dlg_info_name_addr->display, "Anonymous");
  721. if (ast_strlen_zero(session->endpoint->fromuser)) {
  722. pj_strdup2(dlg_pool, &dlg_info_uri->user, "anonymous");
  723. }
  724. if (ast_sip_get_use_callerid_contact() && ast_strlen_zero(session->endpoint->contact_user)) {
  725. pj_strdup2(dlg_pool, &dlg_contact_uri->user, "anonymous");
  726. }
  727. if (ast_strlen_zero(session->endpoint->fromdomain)) {
  728. pj_strdup2(dlg_pool, &dlg_info_uri->host, "anonymous.invalid");
  729. }
  730. } else {
  731. ast_sip_add_usereqphone(session->endpoint, dlg_pool, dlg_info->uri);
  732. }
  733. }
  734. int ast_sip_session_refresh(struct ast_sip_session *session,
  735. ast_sip_session_request_creation_cb on_request_creation,
  736. ast_sip_session_sdp_creation_cb on_sdp_creation,
  737. ast_sip_session_response_cb on_response,
  738. enum ast_sip_session_refresh_method method, int generate_new_sdp)
  739. {
  740. pjsip_inv_session *inv_session = session->inv_session;
  741. pjmedia_sdp_session *new_sdp = NULL;
  742. pjsip_tx_data *tdata;
  743. if (inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
  744. /* Don't try to do anything with a hung-up call */
  745. ast_debug(3, "Not sending reinvite to %s because of disconnected state...\n",
  746. ast_sorcery_object_get_id(session->endpoint));
  747. return 0;
  748. }
  749. /* If the dialog has not yet been established we have to defer until it has */
  750. if (inv_session->dlg->state != PJSIP_DIALOG_STATE_ESTABLISHED) {
  751. ast_debug(3, "Delay sending request to %s because dialog has not been established...\n",
  752. ast_sorcery_object_get_id(session->endpoint));
  753. return delay_request(session, on_request_creation, on_sdp_creation, on_response,
  754. generate_new_sdp,
  755. method == AST_SIP_SESSION_REFRESH_METHOD_INVITE
  756. ? DELAYED_METHOD_INVITE : DELAYED_METHOD_UPDATE);
  757. }
  758. if (method == AST_SIP_SESSION_REFRESH_METHOD_INVITE) {
  759. if (inv_session->invite_tsx) {
  760. /* We can't send a reinvite yet, so delay it */
  761. ast_debug(3, "Delay sending reinvite to %s because of outstanding transaction...\n",
  762. ast_sorcery_object_get_id(session->endpoint));
  763. return delay_request(session, on_request_creation, on_sdp_creation,
  764. on_response, generate_new_sdp, DELAYED_METHOD_INVITE);
  765. } else if (inv_session->state != PJSIP_INV_STATE_CONFIRMED) {
  766. /* Initial INVITE transaction failed to progress us to a confirmed state
  767. * which means re-invites are not possible
  768. */
  769. ast_debug(3, "Not sending reinvite to %s because not in confirmed state...\n",
  770. ast_sorcery_object_get_id(session->endpoint));
  771. return 0;
  772. }
  773. }
  774. if (generate_new_sdp) {
  775. /* SDP can only be generated if current negotiation has already completed */
  776. if (inv_session->neg
  777. && pjmedia_sdp_neg_get_state(inv_session->neg)
  778. != PJMEDIA_SDP_NEG_STATE_DONE) {
  779. ast_debug(3, "Delay session refresh with new SDP to %s because SDP negotiation is not yet done...\n",
  780. ast_sorcery_object_get_id(session->endpoint));
  781. return delay_request(session, on_request_creation, on_sdp_creation,
  782. on_response, generate_new_sdp,
  783. method == AST_SIP_SESSION_REFRESH_METHOD_INVITE
  784. ? DELAYED_METHOD_INVITE : DELAYED_METHOD_UPDATE);
  785. }
  786. new_sdp = generate_session_refresh_sdp(session);
  787. if (!new_sdp) {
  788. ast_log(LOG_ERROR, "Failed to generate session refresh SDP. Not sending session refresh\n");
  789. return -1;
  790. }
  791. if (on_sdp_creation) {
  792. if (on_sdp_creation(session, new_sdp)) {
  793. return -1;
  794. }
  795. }
  796. }
  797. if (method == AST_SIP_SESSION_REFRESH_METHOD_INVITE) {
  798. if (pjsip_inv_reinvite(inv_session, NULL, new_sdp, &tdata)) {
  799. ast_log(LOG_WARNING, "Failed to create reinvite properly.\n");
  800. return -1;
  801. }
  802. } else if (pjsip_inv_update(inv_session, NULL, new_sdp, &tdata)) {
  803. ast_log(LOG_WARNING, "Failed to create UPDATE properly.\n");
  804. return -1;
  805. }
  806. if (on_request_creation) {
  807. if (on_request_creation(session, tdata)) {
  808. return -1;
  809. }
  810. }
  811. ast_debug(3, "Sending session refresh SDP via %s to %s\n",
  812. method == AST_SIP_SESSION_REFRESH_METHOD_INVITE ? "re-INVITE" : "UPDATE",
  813. ast_sorcery_object_get_id(session->endpoint));
  814. ast_sip_session_send_request_with_cb(session, tdata, on_response);
  815. return 0;
  816. }
  817. int ast_sip_session_regenerate_answer(struct ast_sip_session *session,
  818. ast_sip_session_sdp_creation_cb on_sdp_creation)
  819. {
  820. pjsip_inv_session *inv_session = session->inv_session;
  821. pjmedia_sdp_session *new_answer = NULL;
  822. const pjmedia_sdp_session *previous_offer = NULL;
  823. /* The SDP answer can only be regenerated if it is still pending to be sent */
  824. if (!inv_session->neg || (pjmedia_sdp_neg_get_state(inv_session->neg) != PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER &&
  825. pjmedia_sdp_neg_get_state(inv_session->neg) != PJMEDIA_SDP_NEG_STATE_WAIT_NEGO)) {
  826. ast_log(LOG_WARNING, "Requested to regenerate local SDP answer for channel '%s' but negotiation in state '%s'\n",
  827. ast_channel_name(session->channel), pjmedia_sdp_neg_state_str(pjmedia_sdp_neg_get_state(inv_session->neg)));
  828. return -1;
  829. }
  830. pjmedia_sdp_neg_get_neg_remote(inv_session->neg, &previous_offer);
  831. if (pjmedia_sdp_neg_get_state(inv_session->neg) == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO) {
  832. /* Transition the SDP negotiator back to when it received the remote offer */
  833. pjmedia_sdp_neg_negotiate(inv_session->pool, inv_session->neg, 0);
  834. pjmedia_sdp_neg_set_remote_offer(inv_session->pool, inv_session->neg, previous_offer);
  835. }
  836. new_answer = create_local_sdp(inv_session, session, previous_offer);
  837. if (!new_answer) {
  838. ast_log(LOG_WARNING, "Could not create a new local SDP answer for channel '%s'\n",
  839. ast_channel_name(session->channel));
  840. return -1;
  841. }
  842. if (on_sdp_creation) {
  843. if (on_sdp_creation(session, new_answer)) {
  844. return -1;
  845. }
  846. }
  847. pjsip_inv_set_sdp_answer(inv_session, new_answer);
  848. return 0;
  849. }
  850. void ast_sip_session_send_response(struct ast_sip_session *session, pjsip_tx_data *tdata)
  851. {
  852. handle_outgoing_response(session, tdata);
  853. pjsip_inv_send_msg(session->inv_session, tdata);
  854. return;
  855. }
  856. static pj_bool_t session_on_rx_request(pjsip_rx_data *rdata);
  857. static pjsip_module session_module = {
  858. .name = {"Session Module", 14},
  859. .priority = PJSIP_MOD_PRIORITY_APPLICATION,
  860. .on_rx_request = session_on_rx_request,
  861. };
  862. /*! \brief Determine whether the SDP provided requires deferral of negotiating or not
  863. *
  864. * \retval 1 re-invite should be deferred and resumed later
  865. * \retval 0 re-invite should not be deferred
  866. */
  867. static int sdp_requires_deferral(struct ast_sip_session *session, const pjmedia_sdp_session *sdp)
  868. {
  869. int i;
  870. for (i = 0; i < sdp->media_count; ++i) {
  871. /* See if there are registered handlers for this media stream type */
  872. char media[20];
  873. struct ast_sip_session_sdp_handler *handler;
  874. RAII_VAR(struct sdp_handler_list *, handler_list, NULL, ao2_cleanup);
  875. RAII_VAR(struct ast_sip_session_media *, session_media, NULL, ao2_cleanup);
  876. enum ast_sip_session_sdp_stream_defer res;
  877. /* We need a null-terminated version of the media string */
  878. ast_copy_pj_str(media, &sdp->media[i]->desc.media, sizeof(media));
  879. session_media = ao2_find(session->media, media, OBJ_KEY);
  880. if (!session_media) {
  881. /* if the session_media doesn't exist, there weren't
  882. * any handlers at the time of its creation */
  883. continue;
  884. }
  885. if (session_media->handler) {
  886. handler = session_media->handler;
  887. if (handler->defer_incoming_sdp_stream) {
  888. res = handler->defer_incoming_sdp_stream(session, session_media, sdp,
  889. sdp->media[i]);
  890. switch (res) {
  891. case AST_SIP_SESSION_SDP_DEFER_NOT_HANDLED:
  892. break;
  893. case AST_SIP_SESSION_SDP_DEFER_ERROR:
  894. return 0;
  895. case AST_SIP_SESSION_SDP_DEFER_NOT_NEEDED:
  896. break;
  897. case AST_SIP_SESSION_SDP_DEFER_NEEDED:
  898. return 1;
  899. }
  900. }
  901. /* Handled by this handler. Move to the next stream */
  902. continue;
  903. }
  904. handler_list = ao2_find(sdp_handlers, media, OBJ_KEY);
  905. if (!handler_list) {
  906. ast_debug(1, "No registered SDP handlers for media type '%s'\n", media);
  907. continue;
  908. }
  909. AST_LIST_TRAVERSE(&handler_list->list, handler, next) {
  910. if (handler == session_media->handler) {
  911. continue;
  912. }
  913. if (!handler->defer_incoming_sdp_stream) {
  914. continue;
  915. }
  916. res = handler->defer_incoming_sdp_stream(session, session_media, sdp,
  917. sdp->media[i]);
  918. switch (res) {
  919. case AST_SIP_SESSION_SDP_DEFER_NOT_HANDLED:
  920. continue;
  921. case AST_SIP_SESSION_SDP_DEFER_ERROR:
  922. session_media_set_handler(session_media, handler);
  923. return 0;
  924. case AST_SIP_SESSION_SDP_DEFER_NOT_NEEDED:
  925. /* Handled by this handler. */
  926. session_media_set_handler(session_media, handler);
  927. break;
  928. case AST_SIP_SESSION_SDP_DEFER_NEEDED:
  929. /* Handled by this handler. */
  930. session_media_set_handler(session_media, handler);
  931. return 1;
  932. }
  933. /* Move to the next stream */
  934. break;
  935. }
  936. }
  937. return 0;
  938. }
  939. static pj_bool_t session_reinvite_on_rx_request(pjsip_rx_data *rdata)
  940. {
  941. pjsip_dialog *dlg;
  942. RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
  943. pjsip_rdata_sdp_info *sdp_info;
  944. if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD ||
  945. !(dlg = pjsip_ua_find_dialog(&rdata->msg_info.cid->id, &rdata->msg_info.to->tag, &rdata->msg_info.from->tag, PJ_FALSE)) ||
  946. !(session = ast_sip_dialog_get_session(dlg)) ||
  947. !session->channel) {
  948. return PJ_FALSE;
  949. }
  950. if (session->deferred_reinvite) {
  951. pj_str_t key, deferred_key;
  952. pjsip_tx_data *tdata;
  953. /* We use memory from the new request on purpose so the deferred reinvite pool does not grow uncontrollably */
  954. pjsip_tsx_create_key(rdata->tp_info.pool, &key, PJSIP_ROLE_UAS, &rdata->msg_info.cseq->method, rdata);
  955. pjsip_tsx_create_key(rdata->tp_info.pool, &deferred_key, PJSIP_ROLE_UAS, &session->deferred_reinvite->msg_info.cseq->method,
  956. session->deferred_reinvite);
  957. /* If this is a retransmission ignore it */
  958. if (!pj_strcmp(&key, &deferred_key)) {
  959. return PJ_TRUE;
  960. }
  961. /* Otherwise this is a new re-invite, so reject it */
  962. if (pjsip_dlg_create_response(dlg, rdata, 491, NULL, &tdata) == PJ_SUCCESS) {
  963. if (pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL) != PJ_SUCCESS) {
  964. pjsip_tx_data_dec_ref(tdata);
  965. }
  966. }
  967. return PJ_TRUE;
  968. }
  969. if (!(sdp_info = pjsip_rdata_get_sdp_info(rdata)) ||
  970. (sdp_info->sdp_err != PJ_SUCCESS)) {
  971. return PJ_FALSE;
  972. }
  973. if (!sdp_info->sdp) {
  974. const pjmedia_sdp_session *local;
  975. int i;
  976. ast_queue_unhold(session->channel);
  977. pjmedia_sdp_neg_get_active_local(session->inv_session->neg, &local);
  978. if (!local) {
  979. return PJ_FALSE;
  980. }
  981. /*
  982. * Some devices indicate hold with deferred SDP reinvites (i.e. no SDP in the reinvite).
  983. * When hold is initially indicated, we
  984. * - Receive an INVITE with no SDP
  985. * - Send a 200 OK with SDP, indicating sendrecv in the media streams
  986. * - Receive an ACK with SDP, indicating sendonly in the media streams
  987. *
  988. * At this point, the pjmedia negotiator saves the state of the media direction so that
  989. * if we are to send any offers, we'll offer recvonly in the media streams. This is
  990. * problematic if the device is attempting to unhold, though. If the device unholds
  991. * by sending a reinvite with no SDP, then we will respond with a 200 OK with recvonly.
  992. * According to RFC 3264, if an offerer offers recvonly, then the answerer MUST respond
  993. * with sendonly or inactive. The result of this is that the stream is not off hold.
  994. *
  995. * Therefore, in this case, when we receive a reinvite while the stream is on hold, we
  996. * need to be sure to offer sendrecv. This way, the answerer can respond with sendrecv
  997. * in order to get the stream off hold. If this is actually a different purpose reinvite
  998. * (like a session timer refresh), then the answerer can respond to our sendrecv with
  999. * sendonly, keeping the stream on hold.
  1000. */
  1001. for (i = 0; i < local->media_count; ++i) {
  1002. pjmedia_sdp_media *m = local->media[i];
  1003. pjmedia_sdp_attr *recvonly;
  1004. pjmedia_sdp_attr *inactive;
  1005. recvonly = pjmedia_sdp_attr_find2(m->attr_count, m->attr, "recvonly", NULL);
  1006. inactive = pjmedia_sdp_attr_find2(m->attr_count, m->attr, "inactive", NULL);
  1007. if (recvonly || inactive) {
  1008. pjmedia_sdp_attr *to_remove = recvonly ?: inactive;
  1009. pjmedia_sdp_attr *sendrecv;
  1010. pjmedia_sdp_attr_remove(&m->attr_count, m->attr, to_remove);
  1011. sendrecv = pjmedia_sdp_attr_create(session->inv_session->pool, "sendrecv", NULL);
  1012. pjmedia_sdp_media_add_attr(m, sendrecv);
  1013. }
  1014. }
  1015. return PJ_FALSE;
  1016. }
  1017. if (!sdp_requires_deferral(session, sdp_info->sdp)) {
  1018. return PJ_FALSE;
  1019. }
  1020. pjsip_rx_data_clone(rdata, 0, &session->deferred_reinvite);
  1021. return PJ_TRUE;
  1022. }
  1023. void ast_sip_session_resume_reinvite(struct ast_sip_session *session)
  1024. {
  1025. if (!session->deferred_reinvite) {
  1026. return;
  1027. }
  1028. if (session->channel) {
  1029. pjsip_endpt_process_rx_data(ast_sip_get_pjsip_endpoint(),
  1030. session->deferred_reinvite, NULL, NULL);
  1031. }
  1032. pjsip_rx_data_free_cloned(session->deferred_reinvite);
  1033. session->deferred_reinvite = NULL;
  1034. }
  1035. static pjsip_module session_reinvite_module = {
  1036. .name = { "Session Re-Invite Module", 24 },
  1037. .priority = PJSIP_MOD_PRIORITY_UA_PROXY_LAYER - 1,
  1038. .on_rx_request = session_reinvite_on_rx_request,
  1039. };
  1040. void ast_sip_session_send_request_with_cb(struct ast_sip_session *session, pjsip_tx_data *tdata,
  1041. ast_sip_session_response_cb on_response)
  1042. {
  1043. pjsip_inv_session *inv_session = session->inv_session;
  1044. /* For every request except BYE we disallow sending of the message when
  1045. * the session has been disconnected. A BYE request is special though
  1046. * because it can be sent again after the session is disconnected except
  1047. * with credentials.
  1048. */
  1049. if (inv_session->state == PJSIP_INV_STATE_DISCONNECTED &&
  1050. tdata->msg->line.req.method.id != PJSIP_BYE_METHOD) {
  1051. return;
  1052. }
  1053. ast_sip_mod_data_set(tdata->pool, tdata->mod_data, session_module.id,
  1054. MOD_DATA_ON_RESPONSE, on_response);
  1055. handle_outgoing_request(session, tdata);
  1056. pjsip_inv_send_msg(session->inv_session, tdata);
  1057. return;
  1058. }
  1059. void ast_sip_session_send_request(struct ast_sip_session *session, pjsip_tx_data *tdata)
  1060. {
  1061. ast_sip_session_send_request_with_cb(session, tdata, NULL);
  1062. }
  1063. int ast_sip_session_create_invite(struct ast_sip_session *session, pjsip_tx_data **tdata)
  1064. {
  1065. pjmedia_sdp_session *offer;
  1066. if (!(offer = create_local_sdp(session->inv_session, session, NULL))) {
  1067. pjsip_inv_terminate(session->inv_session, 500, PJ_FALSE);
  1068. return -1;
  1069. }
  1070. pjsip_inv_set_local_sdp(session->inv_session, offer);
  1071. pjmedia_sdp_neg_set_prefer_remote_codec_order(session->inv_session->neg, PJ_FALSE);
  1072. #ifdef PJMEDIA_SDP_NEG_ANSWER_MULTIPLE_CODECS
  1073. pjmedia_sdp_neg_set_answer_multiple_codecs(session->inv_session->neg, PJ_TRUE);
  1074. #endif
  1075. /*
  1076. * We MUST call set_from_header() before pjsip_inv_invite. If we don't, the
  1077. * From in the initial INVITE will be wrong but the rest of the messages will be OK.
  1078. */
  1079. set_from_header(session);
  1080. if (pjsip_inv_invite(session->inv_session, tdata) != PJ_SUCCESS) {
  1081. return -1;
  1082. }
  1083. return 0;
  1084. }
  1085. static int datastore_hash(const void *obj, int flags)
  1086. {
  1087. const struct ast_datastore *datastore = obj;
  1088. const char *uid = flags & OBJ_KEY ? obj : datastore->uid;
  1089. ast_assert(uid != NULL);
  1090. return ast_str_hash(uid);
  1091. }
  1092. static int datastore_cmp(void *obj, void *arg, int flags)
  1093. {
  1094. const struct ast_datastore *datastore1 = obj;
  1095. const struct ast_datastore *datastore2 = arg;
  1096. const char *uid2 = flags & OBJ_KEY ? arg : datastore2->uid;
  1097. ast_assert(datastore1->uid != NULL);
  1098. ast_assert(uid2 != NULL);
  1099. return strcmp(datastore1->uid, uid2) ? 0 : CMP_MATCH | CMP_STOP;
  1100. }
  1101. static void session_media_dtor(void *obj)
  1102. {
  1103. struct ast_sip_session_media *session_media = obj;
  1104. struct sdp_handler_list *handler_list;
  1105. /* It is possible for SDP handlers to allocate memory on a session_media but
  1106. * not end up getting set as the handler for this session_media. This traversal
  1107. * ensures that all memory allocated by SDP handlers on the session_media is
  1108. * cleared (as well as file descriptors, etc.).
  1109. */
  1110. handler_list = ao2_find(sdp_handlers, session_media->stream_type, OBJ_KEY);
  1111. if (handler_list) {
  1112. struct ast_sip_session_sdp_handler *handler;
  1113. AST_LIST_TRAVERSE(&handler_list->list, handler, next) {
  1114. handler->stream_destroy(session_media);
  1115. }
  1116. }
  1117. ao2_cleanup(handler_list);
  1118. if (session_media->srtp) {
  1119. ast_sdp_srtp_destroy(session_media->srtp);
  1120. }
  1121. }
  1122. static void session_destructor(void *obj)
  1123. {
  1124. struct ast_sip_session *session = obj;
  1125. struct ast_sip_session_supplement *supplement;
  1126. struct ast_sip_session_delayed_request *delay;
  1127. const char *endpoint_name = session->endpoint ?
  1128. ast_sorcery_object_get_id(session->endpoint) : "<none>";
  1129. ast_debug(3, "Destroying SIP session with endpoint %s\n", endpoint_name);
  1130. ast_test_suite_event_notify("SESSION_DESTROYING",
  1131. "Endpoint: %s\r\n"
  1132. "AOR: %s\r\n"
  1133. "Contact: %s"
  1134. , endpoint_name
  1135. , session->aor ? ast_sorcery_object_get_id(session->aor) : "<none>"
  1136. , session->contact ? ast_sorcery_object_get_id(session->contact) : "<none>"
  1137. );
  1138. while ((supplement = AST_LIST_REMOVE_HEAD(&session->supplements, next))) {
  1139. if (supplement->session_destroy) {
  1140. supplement->session_destroy(session);
  1141. }
  1142. ast_free(supplement);
  1143. }
  1144. ast_taskprocessor_unreference(session->serializer);
  1145. ao2_cleanup(session->datastores);
  1146. ao2_cleanup(session->media);
  1147. AST_LIST_HEAD_DESTROY(&session->supplements);
  1148. while ((delay = AST_LIST_REMOVE_HEAD(&session->delayed_requests, next))) {
  1149. ast_free(delay);
  1150. }
  1151. ast_party_id_free(&session->id);
  1152. ao2_cleanup(session->endpoint);
  1153. ao2_cleanup(session->aor);
  1154. ao2_cleanup(session->contact);
  1155. ao2_cleanup(session->req_caps);
  1156. ao2_cleanup(session->direct_media_cap);
  1157. ast_dsp_free(session->dsp);
  1158. if (session->inv_session) {
  1159. pjsip_dlg_dec_session(session->inv_session->dlg, &session_module);
  1160. }
  1161. ast_test_suite_event_notify("SESSION_DESTROYED", "Endpoint: %s", endpoint_name);
  1162. }
  1163. static int add_session_media(void *obj, void *arg, int flags)
  1164. {
  1165. struct sdp_handler_list *handler_list = obj;
  1166. struct ast_sip_session *session = arg;
  1167. RAII_VAR(struct ast_sip_session_media *, session_media, NULL, ao2_cleanup);
  1168. session_media = ao2_alloc(sizeof(*session_media) + strlen(handler_list->stream_type), session_media_dtor);
  1169. if (!session_media) {
  1170. return CMP_STOP;
  1171. }
  1172. session_media->encryption = session->endpoint->media.rtp.encryption;
  1173. session_media->remote_ice = session->endpoint->media.rtp.ice_support;
  1174. session_media->remote_rtcp_mux = session->endpoint->rtcp_mux;
  1175. session_media->keepalive_sched_id = -1;
  1176. session_media->timeout_sched_id = -1;
  1177. /* Safe use of strcpy */
  1178. strcpy(session_media->stream_type, handler_list->stream_type);
  1179. ao2_link(session->media, session_media);
  1180. return 0;
  1181. }
  1182. /*! \brief Destructor for SIP channel */
  1183. static void sip_channel_destroy(void *obj)
  1184. {
  1185. struct ast_sip_channel_pvt *channel = obj;
  1186. ao2_cleanup(channel->pvt);
  1187. ao2_cleanup(channel->session);
  1188. }
  1189. struct ast_sip_channel_pvt *ast_sip_channel_pvt_alloc(void *pvt, struct ast_sip_session *session)
  1190. {
  1191. struct ast_sip_channel_pvt *channel = ao2_alloc(sizeof(*channel), sip_channel_destroy);
  1192. if (!channel) {
  1193. return NULL;
  1194. }
  1195. ao2_ref(pvt, +1);
  1196. channel->pvt = pvt;
  1197. ao2_ref(session, +1);
  1198. channel->session = session;
  1199. return channel;
  1200. }
  1201. struct ast_sip_session *ast_sip_session_alloc(struct ast_sip_endpoint *endpoint,
  1202. struct ast_sip_contact *contact, pjsip_inv_session *inv_session, pjsip_rx_data *rdata)
  1203. {
  1204. RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
  1205. struct ast_sip_session *ret_session;
  1206. struct ast_sip_session_supplement *iter;
  1207. int dsp_features = 0;
  1208. session = ao2_alloc(sizeof(*session), session_destructor);
  1209. if (!session) {
  1210. return NULL;
  1211. }
  1212. AST_LIST_HEAD_INIT(&session->supplements);
  1213. AST_LIST_HEAD_INIT_NOLOCK(&session->delayed_requests);
  1214. ast_party_id_init(&session->id);
  1215. session->direct_media_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  1216. if (!session->direct_media_cap) {
  1217. return NULL;
  1218. }
  1219. session->req_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  1220. if (!session->req_caps) {
  1221. return NULL;
  1222. }
  1223. session->datastores = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  1224. DATASTORE_BUCKETS, datastore_hash, NULL, datastore_cmp);
  1225. if (!session->datastores) {
  1226. return NULL;
  1227. }
  1228. if (endpoint->dtmf == AST_SIP_DTMF_INBAND || endpoint->dtmf == AST_SIP_DTMF_AUTO) {
  1229. dsp_features |= DSP_FEATURE_DIGIT_DETECT;
  1230. }
  1231. if (endpoint->faxdetect) {
  1232. dsp_features |= DSP_FEATURE_FAX_DETECT;
  1233. }
  1234. if (dsp_features) {
  1235. session->dsp = ast_dsp_new();
  1236. if (!session->dsp) {
  1237. return NULL;
  1238. }
  1239. ast_dsp_set_features(session->dsp, dsp_features);
  1240. }
  1241. session->endpoint = ao2_bump(endpoint);
  1242. session->media = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, MEDIA_BUCKETS,
  1243. session_media_hash, NULL, session_media_cmp);
  1244. if (!session->media) {
  1245. return NULL;
  1246. }
  1247. /* fill session->media with available types */
  1248. ao2_callback(sdp_handlers, OBJ_NODATA, add_session_media, session);
  1249. if (rdata) {
  1250. /*
  1251. * We must continue using the serializer that the original
  1252. * INVITE came in on for the dialog. There may be
  1253. * retransmissions already enqueued in the original
  1254. * serializer that can result in reentrancy and message
  1255. * sequencing problems.
  1256. */
  1257. session->serializer = ast_sip_get_distributor_serializer(rdata);
  1258. } else {
  1259. char tps_name[AST_TASKPROCESSOR_MAX_NAME + 1];
  1260. /* Create name with seq number appended. */
  1261. ast_taskprocessor_build_name(tps_name, sizeof(tps_name), "pjsip/outsess/%s",
  1262. ast_sorcery_object_get_id(endpoint));
  1263. session->serializer = ast_sip_create_serializer_named(tps_name);
  1264. }
  1265. if (!session->serializer) {
  1266. return NULL;
  1267. }
  1268. ast_sip_dialog_set_serializer(inv_session->dlg, session->serializer);
  1269. ast_sip_dialog_set_endpoint(inv_session->dlg, endpoint);
  1270. pjsip_dlg_inc_session(inv_session->dlg, &session_module);
  1271. inv_session->mod_data[session_module.id] = ao2_bump(session);
  1272. session->contact = ao2_bump(contact);
  1273. session->inv_session = inv_session;
  1274. session->dtmf = endpoint->dtmf;
  1275. if (ast_sip_session_add_supplements(session)) {
  1276. /* Release the ref held by session->inv_session */
  1277. ao2_ref(session, -1);
  1278. return NULL;
  1279. }
  1280. AST_LIST_TRAVERSE(&session->supplements, iter, next) {
  1281. if (iter->session_begin) {
  1282. iter->session_begin(session);
  1283. }
  1284. }
  1285. /* Avoid unnecessary ref manipulation to return a session */
  1286. ret_session = session;
  1287. session = NULL;
  1288. return ret_session;
  1289. }
  1290. /*! \brief struct controlling the suspension of the session's serializer. */
  1291. struct ast_sip_session_suspender {
  1292. ast_cond_t cond_suspended;
  1293. ast_cond_t cond_complete;
  1294. int suspended;
  1295. int complete;
  1296. };
  1297. static void sip_session_suspender_dtor(void *vdoomed)
  1298. {
  1299. struct ast_sip_session_suspender *doomed = vdoomed;
  1300. ast_cond_destroy(&doomed->cond_suspended);
  1301. ast_cond_destroy(&doomed->cond_complete);
  1302. }
  1303. /*!
  1304. * \internal
  1305. * \brief Block the session serializer thread task.
  1306. *
  1307. * \param data Pushed serializer task data for suspension.
  1308. *
  1309. * \retval 0
  1310. */
  1311. static int sip_session_suspend_task(void *data)
  1312. {
  1313. struct ast_sip_session_suspender *suspender = data;
  1314. ao2_lock(suspender);
  1315. /* Signal that the serializer task is now suspended. */
  1316. suspender->suspended = 1;
  1317. ast_cond_signal(&suspender->cond_suspended);
  1318. /* Wait for the serializer suspension to be completed. */
  1319. while (!suspender->complete) {
  1320. ast_cond_wait(&suspender->cond_complete, ao2_object_get_lockaddr(suspender));
  1321. }
  1322. ao2_unlock(suspender);
  1323. ao2_ref(suspender, -1);
  1324. return 0;
  1325. }
  1326. void ast_sip_session_suspend(struct ast_sip_session *session)
  1327. {
  1328. struct ast_sip_session_suspender *suspender;
  1329. int res;
  1330. ast_assert(session->suspended == NULL);
  1331. if (ast_taskprocessor_is_task(session->serializer)) {
  1332. /* I am the session's serializer thread so I cannot suspend. */
  1333. return;
  1334. }
  1335. if (ast_taskprocessor_is_suspended(session->serializer)) {
  1336. /* The serializer already suspended. */
  1337. return;
  1338. }
  1339. suspender = ao2_alloc(sizeof(*suspender), sip_session_suspender_dtor);
  1340. if (!suspender) {
  1341. /* We will just have to hope that the system does not deadlock */
  1342. return;
  1343. }
  1344. ast_cond_init(&suspender->cond_suspended, NULL);
  1345. ast_cond_init(&suspender->cond_complete, NULL);
  1346. ao2_ref(suspender, +1);
  1347. res = ast_sip_push_task(session->serializer, sip_session_suspend_task, suspender);
  1348. if (res) {
  1349. /* We will just have to hope that the system does not deadlock */
  1350. ao2_ref(suspender, -2);
  1351. return;
  1352. }
  1353. session->suspended = suspender;
  1354. /* Wait for the serializer to get suspended. */
  1355. ao2_lock(suspender);
  1356. while (!suspender->suspended) {
  1357. ast_cond_wait(&suspender->cond_suspended, ao2_object_get_lockaddr(suspender));
  1358. }
  1359. ao2_unlock(suspender);
  1360. ast_taskprocessor_suspend(session->serializer);
  1361. }
  1362. void ast_sip_session_unsuspend(struct ast_sip_session *session)
  1363. {
  1364. struct ast_sip_session_suspender *suspender = session->suspended;
  1365. if (!suspender) {
  1366. /* Nothing to do */
  1367. return;
  1368. }
  1369. session->suspended = NULL;
  1370. /* Signal that the serializer task suspension is now complete. */
  1371. ao2_lock(suspender);
  1372. suspender->complete = 1;
  1373. ast_cond_signal(&suspender->cond_complete);
  1374. ao2_unlock(suspender);
  1375. ao2_ref(suspender, -1);
  1376. ast_taskprocessor_unsuspend(session->serializer);
  1377. }
  1378. /*!
  1379. * \internal
  1380. * \brief Handle initial INVITE challenge response message.
  1381. * \since 13.5.0
  1382. *
  1383. * \param rdata PJSIP receive response message data.
  1384. *
  1385. * \retval PJ_FALSE Did not handle message.
  1386. * \retval PJ_TRUE Handled message.
  1387. */
  1388. static pj_bool_t outbound_invite_auth(pjsip_rx_data *rdata)
  1389. {
  1390. pjsip_transaction *tsx;
  1391. pjsip_dialog *dlg;
  1392. pjsip_inv_session *inv;
  1393. pjsip_tx_data *tdata;
  1394. struct ast_sip_session *session;
  1395. if (rdata->msg_info.msg->line.status.code != 401
  1396. && rdata->msg_info.msg->line.status.code != 407) {
  1397. /* Doesn't pertain to us. Move on */
  1398. return PJ_FALSE;
  1399. }
  1400. tsx = pjsip_rdata_get_tsx(rdata);
  1401. dlg = pjsip_rdata_get_dlg(rdata);
  1402. if (!dlg || !tsx) {
  1403. return PJ_FALSE;
  1404. }
  1405. if (tsx->method.id != PJSIP_INVITE_METHOD) {
  1406. /* Not an INVITE that needs authentication */
  1407. return PJ_FALSE;
  1408. }
  1409. inv = pjsip_dlg_get_inv_session(dlg);
  1410. if (PJSIP_INV_STATE_CONFIRMED <= inv->state) {
  1411. /*
  1412. * We cannot handle reINVITE authentication at this
  1413. * time because the reINVITE transaction is still in
  1414. * progress.
  1415. */
  1416. ast_debug(1, "A reINVITE is being challenged.\n");
  1417. return PJ_FALSE;
  1418. }
  1419. ast_debug(1, "Initial INVITE is being challenged.\n");
  1420. session = inv->mod_data[session_module.id];
  1421. if (ast_sip_create_request_with_auth(&session->endpoint->outbound_auths, rdata, tsx,
  1422. &tdata)) {
  1423. return PJ_FALSE;
  1424. }
  1425. /*
  1426. * Restart the outgoing initial INVITE transaction to deal
  1427. * with authentication.
  1428. */
  1429. pjsip_inv_uac_restart(inv, PJ_FALSE);
  1430. ast_sip_session_send_request(session, tdata);
  1431. return PJ_TRUE;
  1432. }
  1433. static pjsip_module outbound_invite_auth_module = {
  1434. .name = {"Outbound INVITE Auth", 20},
  1435. .priority = PJSIP_MOD_PRIORITY_DIALOG_USAGE,
  1436. .on_rx_response = outbound_invite_auth,
  1437. };
  1438. /*!
  1439. * \internal
  1440. * \brief Setup outbound initial INVITE authentication.
  1441. * \since 13.5.0
  1442. *
  1443. * \param dlg PJSIP dialog to attach outbound authentication.
  1444. *
  1445. * \retval 0 on success.
  1446. * \retval -1 on error.
  1447. */
  1448. static int setup_outbound_invite_auth(pjsip_dialog *dlg)
  1449. {
  1450. pj_status_t status;
  1451. ++dlg->sess_count;
  1452. status = pjsip_dlg_add_usage(dlg, &outbound_invite_auth_module, NULL);
  1453. --dlg->sess_count;
  1454. return status != PJ_SUCCESS ? -1 : 0;
  1455. }
  1456. struct ast_sip_session *ast_sip_session_create_outgoing(struct ast_sip_endpoint *endpoint,
  1457. struct ast_sip_contact *contact, const char *location, const char *request_user,
  1458. struct ast_format_cap *req_caps)
  1459. {
  1460. const char *uri = NULL;
  1461. RAII_VAR(struct ast_sip_aor *, found_aor, NULL, ao2_cleanup);
  1462. RAII_VAR(struct ast_sip_contact *, found_contact, NULL, ao2_cleanup);
  1463. pjsip_timer_setting timer;
  1464. pjsip_dialog *dlg;
  1465. struct pjsip_inv_session *inv_session;
  1466. RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
  1467. struct ast_sip_session *ret_session;
  1468. /* If no location has been provided use the AOR list from the endpoint itself */
  1469. if (location || !contact) {
  1470. location = S_OR(location, endpoint->aors);
  1471. ast_sip_location_retrieve_contact_and_aor_from_list_filtered(location, AST_SIP_CONTACT_FILTER_REACHABLE,
  1472. &found_aor, &found_contact);
  1473. if (!found_contact || ast_strlen_zero(found_contact->uri)) {
  1474. uri = location;
  1475. } else {
  1476. uri = found_contact->uri;
  1477. }
  1478. } else {
  1479. uri = contact->uri;
  1480. }
  1481. /* If we still have no URI to dial fail to create the session */
  1482. if (ast_strlen_zero(uri)) {
  1483. ast_log(LOG_ERROR, "Endpoint '%s': No URI available. Is endpoint registered?\n",
  1484. ast_sorcery_object_get_id(endpoint));
  1485. return NULL;
  1486. }
  1487. if (!(dlg = ast_sip_create_dialog_uac(endpoint, uri, request_user))) {
  1488. return NULL;
  1489. }
  1490. if (setup_outbound_invite_auth(dlg)) {
  1491. pjsip_dlg_terminate(dlg);
  1492. return NULL;
  1493. }
  1494. if (pjsip_inv_create_uac(dlg, NULL, endpoint->extensions.flags, &inv_session) != PJ_SUCCESS) {
  1495. pjsip_dlg_terminate(dlg);
  1496. return NULL;
  1497. }
  1498. #if defined(HAVE_PJSIP_REPLACE_MEDIA_STREAM) || defined(PJMEDIA_SDP_NEG_ALLOW_MEDIA_CHANGE)
  1499. inv_session->sdp_neg_flags = PJMEDIA_SDP_NEG_ALLOW_MEDIA_CHANGE;
  1500. #endif
  1501. pjsip_timer_setting_default(&timer);
  1502. timer.min_se = endpoint->extensions.timer.min_se;
  1503. timer.sess_expires = endpoint->extensions.timer.sess_expires;
  1504. pjsip_timer_init_session(inv_session, &timer);
  1505. session = ast_sip_session_alloc(endpoint, found_contact ? found_contact : contact,
  1506. inv_session, NULL);
  1507. if (!session) {
  1508. pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
  1509. return NULL;
  1510. }
  1511. session->aor = ao2_bump(found_aor);
  1512. ast_party_id_copy(&session->id, &endpoint->id.self);
  1513. if (ast_format_cap_count(req_caps)) {
  1514. /* get joint caps between req_caps and endpoint caps */
  1515. struct ast_format_cap *joint_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  1516. ast_format_cap_get_compatible(req_caps, endpoint->media.codecs, joint_caps);
  1517. /* if joint caps */
  1518. if (ast_format_cap_count(joint_caps)) {
  1519. /* copy endpoint caps into session->req_caps */
  1520. ast_format_cap_append_from_cap(session->req_caps,
  1521. endpoint->media.codecs, AST_MEDIA_TYPE_UNKNOWN);
  1522. /* replace instances of joint caps equivalents in session->req_caps */
  1523. ast_format_cap_replace_from_cap(session->req_caps, joint_caps,
  1524. AST_MEDIA_TYPE_UNKNOWN);
  1525. }
  1526. ao2_cleanup(joint_caps);
  1527. }
  1528. if (pjsip_dlg_add_usage(dlg, &session_module, NULL) != PJ_SUCCESS) {
  1529. pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
  1530. /* Since we are not notifying ourselves that the INVITE session is being terminated
  1531. * we need to manually drop its reference to session
  1532. */
  1533. ao2_ref(session, -1);
  1534. return NULL;
  1535. }
  1536. /* Avoid unnecessary ref manipulation to return a session */
  1537. ret_session = session;
  1538. session = NULL;
  1539. return ret_session;
  1540. }
  1541. static int session_end(void *vsession);
  1542. static int session_end_completion(void *vsession);
  1543. void ast_sip_session_terminate(struct ast_sip_session *session, int response)
  1544. {
  1545. pj_status_t status;
  1546. pjsip_tx_data *packet = NULL;
  1547. if (session->defer_terminate) {
  1548. session->terminate_while_deferred = 1;
  1549. return;
  1550. }
  1551. if (!response) {
  1552. response = 603;
  1553. }
  1554. switch (session->inv_session->state) {
  1555. case PJSIP_INV_STATE_NULL:
  1556. if (!session->inv_session->invite_tsx) {
  1557. /*
  1558. * Normally, it's pjproject's transaction cleanup that ultimately causes the
  1559. * final session reference to be released but if both STATE and invite_tsx are NULL,
  1560. * we never created a transaction in the first place. In this case, we need to
  1561. * do the cleanup ourselves.
  1562. */
  1563. /* Transfer the inv_session session reference to the session_end_task */
  1564. session->inv_session->mod_data[session_module.id] = NULL;
  1565. pjsip_inv_terminate(session->inv_session, response, PJ_TRUE);
  1566. session_end(session);
  1567. /*
  1568. * session_end_completion will cleanup the final session reference unless
  1569. * ast_sip_session_terminate's caller is holding one.
  1570. */
  1571. session_end_completion(session);
  1572. } else {
  1573. pjsip_inv_terminate(session->inv_session, response, PJ_TRUE);
  1574. }
  1575. break;
  1576. case PJSIP_INV_STATE_CONFIRMED:
  1577. if (session->inv_session->invite_tsx) {
  1578. ast_debug(3, "Delay sending BYE to %s because of outstanding transaction...\n",
  1579. ast_sorcery_object_get_id(session->endpoint));
  1580. /* If this is delayed the only thing that will happen is a BYE request so we don't
  1581. * actually need to store the response code for when it happens.
  1582. */
  1583. delay_request(session, NULL, NULL, NULL, 0, DELAYED_METHOD_BYE);
  1584. break;
  1585. }
  1586. /* Fall through */
  1587. default:
  1588. status = pjsip_inv_end_session(session->inv_session, response, NULL, &packet);
  1589. if (status == PJ_SUCCESS && packet) {
  1590. struct ast_sip_session_delayed_request *delay;
  1591. /* Flush any delayed requests so they cannot overlap this transaction. */
  1592. while ((delay = AST_LIST_REMOVE_HEAD(&session->delayed_requests, next))) {
  1593. ast_free(delay);
  1594. }
  1595. if (packet->msg->type == PJSIP_RESPONSE_MSG) {
  1596. ast_sip_session_send_response(session, packet);
  1597. } else {
  1598. ast_sip_session_send_request(session, packet);
  1599. }
  1600. }
  1601. break;
  1602. }
  1603. }
  1604. static int session_termination_task(void *data)
  1605. {
  1606. struct ast_sip_session *session = data;
  1607. if (session->defer_terminate) {
  1608. session->defer_terminate = 0;
  1609. if (session->inv_session) {
  1610. ast_sip_session_terminate(session, 0);
  1611. }
  1612. }
  1613. ao2_ref(session, -1);
  1614. return 0;
  1615. }
  1616. static void session_termination_cb(pj_timer_heap_t *timer_heap, struct pj_timer_entry *entry)
  1617. {
  1618. struct ast_sip_session *session = entry->user_data;
  1619. if (ast_sip_push_task(session->serializer, session_termination_task, session)) {
  1620. ao2_cleanup(session);
  1621. }
  1622. }
  1623. int ast_sip_session_defer_termination(struct ast_sip_session *session)
  1624. {
  1625. pj_time_val delay = { .sec = 60, };
  1626. int res;
  1627. /* The session should not have an active deferred termination request. */
  1628. ast_assert(!session->defer_terminate);
  1629. session->defer_terminate = 1;
  1630. session->defer_end = 1;
  1631. session->ended_while_deferred = 0;
  1632. ao2_ref(session, +1);
  1633. pj_timer_entry_init(&session->scheduled_termination, 0, session, session_termination_cb);
  1634. res = (pjsip_endpt_schedule_timer(ast_sip_get_pjsip_endpoint(),
  1635. &session->scheduled_termination, &delay) != PJ_SUCCESS) ? -1 : 0;
  1636. if (res) {
  1637. session->defer_terminate = 0;
  1638. ao2_ref(session, -1);
  1639. }
  1640. return res;
  1641. }
  1642. /*!
  1643. * \internal
  1644. * \brief Stop the defer termination timer if it is still running.
  1645. * \since 13.5.0
  1646. *
  1647. * \param session Which session to stop the timer.
  1648. *
  1649. * \return Nothing
  1650. */
  1651. static void sip_session_defer_termination_stop_timer(struct ast_sip_session *session)
  1652. {
  1653. if (pj_timer_heap_cancel_if_active(pjsip_endpt_get_timer_heap(ast_sip_get_pjsip_endpoint()),
  1654. &session->scheduled_termination, session->scheduled_termination.id)) {
  1655. ao2_ref(session, -1);
  1656. }
  1657. }
  1658. void ast_sip_session_defer_termination_cancel(struct ast_sip_session *session)
  1659. {
  1660. if (!session->defer_terminate) {
  1661. /* Already canceled or timer fired. */
  1662. return;
  1663. }
  1664. session->defer_terminate = 0;
  1665. if (session->terminate_while_deferred) {
  1666. /* Complete the termination started by the upper layer. */
  1667. ast_sip_session_terminate(session, 0);
  1668. }
  1669. /* Stop the termination timer if it is still running. */
  1670. sip_session_defer_termination_stop_timer(session);
  1671. }
  1672. void ast_sip_session_end_if_deferred(struct ast_sip_session *session)
  1673. {
  1674. if (!session->defer_end) {
  1675. return;
  1676. }
  1677. session->defer_end = 0;
  1678. if (session->ended_while_deferred) {
  1679. /* Complete the session end started by the remote hangup. */
  1680. ast_debug(3, "Ending session (%p) after being deferred\n", session);
  1681. session->ended_while_deferred = 0;
  1682. session_end(session);
  1683. }
  1684. }
  1685. struct ast_sip_session *ast_sip_dialog_get_session(pjsip_dialog *dlg)
  1686. {
  1687. pjsip_inv_session *inv_session = pjsip_dlg_get_inv_session(dlg);
  1688. struct ast_sip_session *session;
  1689. if (!inv_session ||
  1690. !(session = inv_session->mod_data[session_module.id])) {
  1691. return NULL;
  1692. }
  1693. ao2_ref(session, +1);
  1694. return session;
  1695. }
  1696. enum sip_get_destination_result {
  1697. /*! The extension was successfully found */
  1698. SIP_GET_DEST_EXTEN_FOUND,
  1699. /*! The extension specified in the RURI was not found */
  1700. SIP_GET_DEST_EXTEN_NOT_FOUND,
  1701. /*! The extension specified in the RURI was a partial match */
  1702. SIP_GET_DEST_EXTEN_PARTIAL,
  1703. /*! The RURI is of an unsupported scheme */
  1704. SIP_GET_DEST_UNSUPPORTED_URI,
  1705. };
  1706. /*!
  1707. * \brief Determine where in the dialplan a call should go
  1708. *
  1709. * This uses the username in the request URI to try to match
  1710. * an extension in the endpoint's configured context in order
  1711. * to route the call.
  1712. *
  1713. * \param session The inbound SIP session
  1714. * \param rdata The SIP INVITE
  1715. */
  1716. static enum sip_get_destination_result get_destination(struct ast_sip_session *session, pjsip_rx_data *rdata)
  1717. {
  1718. pjsip_uri *ruri = rdata->msg_info.msg->line.req.uri;
  1719. pjsip_sip_uri *sip_ruri;
  1720. struct ast_features_pickup_config *pickup_cfg;
  1721. const char *pickupexten;
  1722. if (!PJSIP_URI_SCHEME_IS_SIP(ruri) && !PJSIP_URI_SCHEME_IS_SIPS(ruri)) {
  1723. return SIP_GET_DEST_UNSUPPORTED_URI;
  1724. }
  1725. sip_ruri = pjsip_uri_get_uri(ruri);
  1726. ast_copy_pj_str(session->exten, &sip_ruri->user, sizeof(session->exten));
  1727. /*
  1728. * We may want to match in the dialplan without any user
  1729. * options getting in the way.
  1730. */
  1731. AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(session->exten);
  1732. pickup_cfg = ast_get_chan_features_pickup_config(session->channel);
  1733. if (!pickup_cfg) {
  1734. ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension\n");
  1735. pickupexten = "";
  1736. } else {
  1737. pickupexten = ast_strdupa(pickup_cfg->pickupexten);
  1738. ao2_ref(pickup_cfg, -1);
  1739. }
  1740. if (!strcmp(session->exten, pickupexten) ||
  1741. ast_exists_extension(NULL, session->endpoint->context, session->exten, 1, NULL)) {
  1742. size_t size = pj_strlen(&sip_ruri->host) + 1;
  1743. char *domain = ast_alloca(size);
  1744. ast_copy_pj_str(domain, &sip_ruri->host, size);
  1745. pbx_builtin_setvar_helper(session->channel, "SIPDOMAIN", domain);
  1746. /*
  1747. * Save off the INVITE Request-URI in case it is
  1748. * needed: CHANNEL(pjsip,request_uri)
  1749. */
  1750. session->request_uri = pjsip_uri_clone(session->inv_session->pool, ruri);
  1751. return SIP_GET_DEST_EXTEN_FOUND;
  1752. }
  1753. /*
  1754. * Check for partial match via overlap dialling (if enabled)
  1755. */
  1756. if (session->endpoint->allow_overlap && (
  1757. !strncmp(session->exten, pickupexten, strlen(session->exten)) ||
  1758. ast_canmatch_extension(NULL, session->endpoint->context, session->exten, 1, NULL))) {
  1759. /* Overlap partial match */
  1760. return SIP_GET_DEST_EXTEN_PARTIAL;
  1761. }
  1762. return SIP_GET_DEST_EXTEN_NOT_FOUND;
  1763. }
  1764. static pjsip_inv_session *pre_session_setup(pjsip_rx_data *rdata, const struct ast_sip_endpoint *endpoint)
  1765. {
  1766. pjsip_tx_data *tdata;
  1767. pjsip_dialog *dlg;
  1768. pjsip_inv_session *inv_session;
  1769. unsigned int options = endpoint->extensions.flags;
  1770. pj_status_t dlg_status;
  1771. if (pjsip_inv_verify_request(rdata, &options, NULL, NULL, ast_sip_get_pjsip_endpoint(), &tdata) != PJ_SUCCESS) {
  1772. if (tdata) {
  1773. if (pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL) != PJ_SUCCESS) {
  1774. pjsip_tx_data_dec_ref(tdata);
  1775. }
  1776. } else {
  1777. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
  1778. }
  1779. return NULL;
  1780. }
  1781. dlg = ast_sip_create_dialog_uas(endpoint, rdata, &dlg_status);
  1782. if (!dlg) {
  1783. if (dlg_status != PJ_EEXISTS) {
  1784. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
  1785. }
  1786. return NULL;
  1787. }
  1788. if (pjsip_inv_create_uas(dlg, rdata, NULL, options, &inv_session) != PJ_SUCCESS) {
  1789. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
  1790. pjsip_dlg_terminate(dlg);
  1791. return NULL;
  1792. }
  1793. #if defined(HAVE_PJSIP_REPLACE_MEDIA_STREAM) || defined(PJMEDIA_SDP_NEG_ALLOW_MEDIA_CHANGE)
  1794. inv_session->sdp_neg_flags = PJMEDIA_SDP_NEG_ALLOW_MEDIA_CHANGE;
  1795. #endif
  1796. if (pjsip_dlg_add_usage(dlg, &session_module, NULL) != PJ_SUCCESS) {
  1797. if (pjsip_inv_initial_answer(inv_session, rdata, 500, NULL, NULL, &tdata) != PJ_SUCCESS) {
  1798. pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
  1799. }
  1800. pjsip_inv_send_msg(inv_session, tdata);
  1801. return NULL;
  1802. }
  1803. return inv_session;
  1804. }
  1805. struct new_invite {
  1806. /*! \brief Session created for the new INVITE */
  1807. struct ast_sip_session *session;
  1808. /*! \brief INVITE request itself */
  1809. pjsip_rx_data *rdata;
  1810. };
  1811. static int new_invite(struct new_invite *invite)
  1812. {
  1813. pjsip_tx_data *tdata = NULL;
  1814. pjsip_timer_setting timer;
  1815. pjsip_rdata_sdp_info *sdp_info;
  1816. pjmedia_sdp_session *local = NULL;
  1817. char buffer[AST_SOCKADDR_BUFLEN];
  1818. /* From this point on, any calls to pjsip_inv_terminate have the last argument as PJ_TRUE
  1819. * so that we will be notified so we can destroy the session properly
  1820. */
  1821. if (invite->session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
  1822. ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
  1823. invite->session->inv_session->cause,
  1824. pjsip_get_status_text(invite->session->inv_session->cause)->ptr);
  1825. #ifdef HAVE_PJSIP_INV_SESSION_REF
  1826. pjsip_inv_dec_ref(invite->session->inv_session);
  1827. #endif
  1828. return -1;
  1829. }
  1830. switch (get_destination(invite->session, invite->rdata)) {
  1831. case SIP_GET_DEST_EXTEN_FOUND:
  1832. /* Things worked. Keep going */
  1833. break;
  1834. case SIP_GET_DEST_UNSUPPORTED_URI:
  1835. if (pjsip_inv_initial_answer(invite->session->inv_session, invite->rdata, 416, NULL, NULL, &tdata) == PJ_SUCCESS) {
  1836. ast_sip_session_send_response(invite->session, tdata);
  1837. } else {
  1838. pjsip_inv_terminate(invite->session->inv_session, 416, PJ_TRUE);
  1839. }
  1840. goto end;
  1841. case SIP_GET_DEST_EXTEN_PARTIAL:
  1842. ast_debug(1, "Call from '%s' (%s:%s) to extension '%s' - partial match\n",
  1843. ast_sorcery_object_get_id(invite->session->endpoint),
  1844. invite->rdata->tp_info.transport->type_name,
  1845. pj_sockaddr_print(&invite->rdata->pkt_info.src_addr, buffer, sizeof(buffer), 3),
  1846. invite->session->exten);
  1847. if (pjsip_inv_initial_answer(invite->session->inv_session, invite->rdata, 484, NULL, NULL, &tdata) == PJ_SUCCESS) {
  1848. ast_sip_session_send_response(invite->session, tdata);
  1849. } else {
  1850. pjsip_inv_terminate(invite->session->inv_session, 484, PJ_TRUE);
  1851. }
  1852. goto end;
  1853. case SIP_GET_DEST_EXTEN_NOT_FOUND:
  1854. default:
  1855. ast_log(LOG_NOTICE, "Call from '%s' (%s:%s) to extension '%s' rejected because extension not found in context '%s'.\n",
  1856. ast_sorcery_object_get_id(invite->session->endpoint),
  1857. invite->rdata->tp_info.transport->type_name,
  1858. pj_sockaddr_print(&invite->rdata->pkt_info.src_addr, buffer, sizeof(buffer), 3),
  1859. invite->session->exten,
  1860. invite->session->endpoint->context);
  1861. if (pjsip_inv_initial_answer(invite->session->inv_session, invite->rdata, 404, NULL, NULL, &tdata) == PJ_SUCCESS) {
  1862. ast_sip_session_send_response(invite->session, tdata);
  1863. } else {
  1864. pjsip_inv_terminate(invite->session->inv_session, 404, PJ_TRUE);
  1865. }
  1866. goto end;
  1867. };
  1868. pjsip_timer_setting_default(&timer);
  1869. timer.min_se = invite->session->endpoint->extensions.timer.min_se;
  1870. timer.sess_expires = invite->session->endpoint->extensions.timer.sess_expires;
  1871. pjsip_timer_init_session(invite->session->inv_session, &timer);
  1872. /*
  1873. * At this point, we've verified what we can that won't take awhile,
  1874. * so let's go ahead and send a 100 Trying out to stop any
  1875. * retransmissions.
  1876. */
  1877. if (pjsip_inv_initial_answer(invite->session->inv_session, invite->rdata, 100, NULL, NULL, &tdata) != PJ_SUCCESS) {
  1878. pjsip_inv_terminate(invite->session->inv_session, 500, PJ_TRUE);
  1879. goto end;
  1880. }
  1881. ast_sip_session_send_response(invite->session, tdata);
  1882. sdp_info = pjsip_rdata_get_sdp_info(invite->rdata);
  1883. if (sdp_info && (sdp_info->sdp_err == PJ_SUCCESS) && sdp_info->sdp) {
  1884. if (handle_incoming_sdp(invite->session, sdp_info->sdp)) {
  1885. tdata = NULL;
  1886. if (pjsip_inv_end_session(invite->session->inv_session, 488, NULL, &tdata) == PJ_SUCCESS
  1887. && tdata) {
  1888. ast_sip_session_send_response(invite->session, tdata);
  1889. }
  1890. goto end;
  1891. }
  1892. /* We are creating a local SDP which is an answer to their offer */
  1893. local = create_local_sdp(invite->session->inv_session, invite->session, sdp_info->sdp);
  1894. } else {
  1895. /* We are creating a local SDP which is an offer */
  1896. local = create_local_sdp(invite->session->inv_session, invite->session, NULL);
  1897. }
  1898. /* If we were unable to create a local SDP terminate the session early, it won't go anywhere */
  1899. if (!local) {
  1900. tdata = NULL;
  1901. if (pjsip_inv_end_session(invite->session->inv_session, 500, NULL, &tdata) == PJ_SUCCESS
  1902. && tdata) {
  1903. ast_sip_session_send_response(invite->session, tdata);
  1904. }
  1905. goto end;
  1906. }
  1907. pjsip_inv_set_local_sdp(invite->session->inv_session, local);
  1908. pjmedia_sdp_neg_set_prefer_remote_codec_order(invite->session->inv_session->neg, PJ_FALSE);
  1909. #ifdef PJMEDIA_SDP_NEG_ANSWER_MULTIPLE_CODECS
  1910. pjmedia_sdp_neg_set_answer_multiple_codecs(invite->session->inv_session->neg, PJ_TRUE);
  1911. #endif
  1912. handle_incoming_request(invite->session, invite->rdata);
  1913. end:
  1914. #ifdef HAVE_PJSIP_INV_SESSION_REF
  1915. pjsip_inv_dec_ref(invite->session->inv_session);
  1916. #endif
  1917. return 0;
  1918. }
  1919. static void handle_new_invite_request(pjsip_rx_data *rdata)
  1920. {
  1921. RAII_VAR(struct ast_sip_endpoint *, endpoint,
  1922. ast_pjsip_rdata_get_endpoint(rdata), ao2_cleanup);
  1923. pjsip_tx_data *tdata = NULL;
  1924. pjsip_inv_session *inv_session = NULL;
  1925. struct ast_sip_session *session;
  1926. struct new_invite invite;
  1927. ast_assert(endpoint != NULL);
  1928. inv_session = pre_session_setup(rdata, endpoint);
  1929. if (!inv_session) {
  1930. /* pre_session_setup() returns a response on failure */
  1931. return;
  1932. }
  1933. #ifdef HAVE_PJSIP_INV_SESSION_REF
  1934. if (pjsip_inv_add_ref(inv_session) != PJ_SUCCESS) {
  1935. ast_log(LOG_ERROR, "Can't increase the session reference counter\n");
  1936. if (inv_session->state != PJSIP_INV_STATE_DISCONNECTED) {
  1937. if (pjsip_inv_initial_answer(inv_session, rdata, 500, NULL, NULL, &tdata) == PJ_SUCCESS) {
  1938. pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
  1939. } else {
  1940. pjsip_inv_send_msg(inv_session, tdata);
  1941. }
  1942. }
  1943. return;
  1944. }
  1945. #endif
  1946. session = ast_sip_session_alloc(endpoint, NULL, inv_session, rdata);
  1947. if (!session) {
  1948. if (pjsip_inv_initial_answer(inv_session, rdata, 500, NULL, NULL, &tdata) == PJ_SUCCESS) {
  1949. pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
  1950. } else {
  1951. pjsip_inv_send_msg(inv_session, tdata);
  1952. }
  1953. #ifdef HAVE_PJSIP_INV_SESSION_REF
  1954. pjsip_inv_dec_ref(inv_session);
  1955. #endif
  1956. return;
  1957. }
  1958. /*
  1959. * The current thread is supposed be the session serializer to prevent
  1960. * any initial INVITE retransmissions from trying to setup the same
  1961. * call again.
  1962. */
  1963. ast_assert(ast_taskprocessor_is_task(session->serializer));
  1964. invite.session = session;
  1965. invite.rdata = rdata;
  1966. new_invite(&invite);
  1967. ao2_ref(session, -1);
  1968. }
  1969. static pj_bool_t does_method_match(const pj_str_t *message_method, const char *supplement_method)
  1970. {
  1971. pj_str_t method;
  1972. if (ast_strlen_zero(supplement_method)) {
  1973. return PJ_TRUE;
  1974. }
  1975. pj_cstr(&method, supplement_method);
  1976. return pj_stristr(&method, message_method) ? PJ_TRUE : PJ_FALSE;
  1977. }
  1978. static pj_bool_t has_supplement(const struct ast_sip_session *session, const pjsip_rx_data *rdata)
  1979. {
  1980. struct ast_sip_session_supplement *supplement;
  1981. struct pjsip_method *method = &rdata->msg_info.msg->line.req.method;
  1982. if (!session) {
  1983. return PJ_FALSE;
  1984. }
  1985. AST_LIST_TRAVERSE(&session->supplements, supplement, next) {
  1986. if (does_method_match(&method->name, supplement->method)) {
  1987. return PJ_TRUE;
  1988. }
  1989. }
  1990. return PJ_FALSE;
  1991. }
  1992. /*!
  1993. * \brief Called when a new SIP request comes into PJSIP
  1994. *
  1995. * This function is called under two circumstances
  1996. * 1) An out-of-dialog request is received by PJSIP
  1997. * 2) An in-dialog request that the inv_session layer does not
  1998. * handle is received (such as an in-dialog INFO)
  1999. *
  2000. * Except for INVITEs, there is very little we actually do in this function
  2001. * 1) For requests we don't handle, we return PJ_FALSE
  2002. * 2) For new INVITEs, handle them now to prevent retransmissions from
  2003. * trying to setup the same call again.
  2004. * 3) For in-dialog requests we handle, we process them in the
  2005. * .on_state_changed = session_inv_on_state_changed or
  2006. * .on_tsx_state_changed = session_inv_on_tsx_state_changed
  2007. * callbacks instead.
  2008. */
  2009. static pj_bool_t session_on_rx_request(pjsip_rx_data *rdata)
  2010. {
  2011. pj_status_t handled = PJ_FALSE;
  2012. pjsip_dialog *dlg = pjsip_rdata_get_dlg(rdata);
  2013. pjsip_inv_session *inv_session;
  2014. switch (rdata->msg_info.msg->line.req.method.id) {
  2015. case PJSIP_INVITE_METHOD:
  2016. if (dlg) {
  2017. ast_log(LOG_WARNING, "on_rx_request called for INVITE in mid-dialog?\n");
  2018. break;
  2019. }
  2020. handled = PJ_TRUE;
  2021. handle_new_invite_request(rdata);
  2022. break;
  2023. default:
  2024. /* Handle other in-dialog methods if their supplements have been registered */
  2025. handled = dlg && (inv_session = pjsip_dlg_get_inv_session(dlg)) &&
  2026. has_supplement(inv_session->mod_data[session_module.id], rdata);
  2027. break;
  2028. }
  2029. return handled;
  2030. }
  2031. static void resend_reinvite(pj_timer_heap_t *timer, pj_timer_entry *entry)
  2032. {
  2033. struct ast_sip_session *session = entry->user_data;
  2034. ast_debug(3, "Endpoint '%s(%s)' re-INVITE collision timer expired.\n",
  2035. ast_sorcery_object_get_id(session->endpoint),
  2036. session->channel ? ast_channel_name(session->channel) : "");
  2037. if (AST_LIST_EMPTY(&session->delayed_requests)) {
  2038. /* No delayed request pending, so just return */
  2039. ao2_ref(session, -1);
  2040. return;
  2041. }
  2042. if (ast_sip_push_task(session->serializer, invite_collision_timeout, session)) {
  2043. /*
  2044. * Uh oh. We now have nothing in the foreseeable future
  2045. * to trigger sending the delayed requests.
  2046. */
  2047. ao2_ref(session, -1);
  2048. }
  2049. }
  2050. static void reschedule_reinvite(struct ast_sip_session *session, ast_sip_session_response_cb on_response)
  2051. {
  2052. pjsip_inv_session *inv = session->inv_session;
  2053. pj_time_val tv;
  2054. ast_debug(3, "Endpoint '%s(%s)' re-INVITE collision.\n",
  2055. ast_sorcery_object_get_id(session->endpoint),
  2056. session->channel ? ast_channel_name(session->channel) : "");
  2057. if (delay_request(session, NULL, NULL, on_response, 1, DELAYED_METHOD_INVITE)) {
  2058. return;
  2059. }
  2060. if (pj_timer_entry_running(&session->rescheduled_reinvite)) {
  2061. /* Timer already running. Something weird is going on. */
  2062. ast_debug(1, "Endpoint '%s(%s)' re-INVITE collision while timer running!!!\n",
  2063. ast_sorcery_object_get_id(session->endpoint),
  2064. session->channel ? ast_channel_name(session->channel) : "");
  2065. return;
  2066. }
  2067. tv.sec = 0;
  2068. if (inv->role == PJSIP_ROLE_UAC) {
  2069. tv.msec = 2100 + ast_random() % 2000;
  2070. } else {
  2071. tv.msec = ast_random() % 2000;
  2072. }
  2073. pj_timer_entry_init(&session->rescheduled_reinvite, 0, session, resend_reinvite);
  2074. ao2_ref(session, +1);
  2075. if (pjsip_endpt_schedule_timer(ast_sip_get_pjsip_endpoint(),
  2076. &session->rescheduled_reinvite, &tv) != PJ_SUCCESS) {
  2077. ao2_ref(session, -1);
  2078. }
  2079. }
  2080. static void __print_debug_details(const char *function, pjsip_inv_session *inv, pjsip_transaction *tsx, pjsip_event *e)
  2081. {
  2082. int id = session_module.id;
  2083. struct ast_sip_session *session = NULL;
  2084. if (!DEBUG_ATLEAST(5)) {
  2085. /* Debug not spamy enough */
  2086. return;
  2087. }
  2088. ast_log(LOG_DEBUG, "Function %s called on event %s\n",
  2089. function, pjsip_event_str(e->type));
  2090. if (!inv) {
  2091. ast_log(LOG_DEBUG, "Transaction %p does not belong to an inv_session?\n", tsx);
  2092. ast_log(LOG_DEBUG, "The transaction state is %s\n",
  2093. pjsip_tsx_state_str(tsx->state));
  2094. return;
  2095. }
  2096. if (id > -1) {
  2097. session = inv->mod_data[session_module.id];
  2098. }
  2099. if (!session) {
  2100. ast_log(LOG_DEBUG, "inv_session %p has no ast session\n", inv);
  2101. } else {
  2102. ast_log(LOG_DEBUG, "The state change pertains to the endpoint '%s(%s)'\n",
  2103. ast_sorcery_object_get_id(session->endpoint),
  2104. session->channel ? ast_channel_name(session->channel) : "");
  2105. }
  2106. if (inv->invite_tsx) {
  2107. ast_log(LOG_DEBUG, "The inv session still has an invite_tsx (%p)\n",
  2108. inv->invite_tsx);
  2109. } else {
  2110. ast_log(LOG_DEBUG, "The inv session does NOT have an invite_tsx\n");
  2111. }
  2112. if (tsx) {
  2113. ast_log(LOG_DEBUG, "The %s %.*s transaction involved in this state change is %p\n",
  2114. pjsip_role_name(tsx->role),
  2115. (int) pj_strlen(&tsx->method.name), pj_strbuf(&tsx->method.name),
  2116. tsx);
  2117. ast_log(LOG_DEBUG, "The current transaction state is %s\n",
  2118. pjsip_tsx_state_str(tsx->state));
  2119. ast_log(LOG_DEBUG, "The transaction state change event is %s\n",
  2120. pjsip_event_str(e->body.tsx_state.type));
  2121. } else {
  2122. ast_log(LOG_DEBUG, "There is no transaction involved in this state change\n");
  2123. }
  2124. ast_log(LOG_DEBUG, "The current inv state is %s\n", pjsip_inv_state_name(inv->state));
  2125. }
  2126. #define print_debug_details(inv, tsx, e) __print_debug_details(__PRETTY_FUNCTION__, (inv), (tsx), (e))
  2127. static void handle_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
  2128. {
  2129. struct ast_sip_session_supplement *supplement;
  2130. struct pjsip_request_line req = rdata->msg_info.msg->line.req;
  2131. ast_debug(3, "Method is %.*s\n", (int) pj_strlen(&req.method.name), pj_strbuf(&req.method.name));
  2132. AST_LIST_TRAVERSE(&session->supplements, supplement, next) {
  2133. if (supplement->incoming_request && does_method_match(&req.method.name, supplement->method)) {
  2134. if (supplement->incoming_request(session, rdata)) {
  2135. break;
  2136. }
  2137. }
  2138. }
  2139. }
  2140. static void handle_incoming_response(struct ast_sip_session *session, pjsip_rx_data *rdata,
  2141. enum ast_sip_session_response_priority response_priority)
  2142. {
  2143. struct ast_sip_session_supplement *supplement;
  2144. struct pjsip_status_line status = rdata->msg_info.msg->line.status;
  2145. ast_debug(3, "Response is %d %.*s\n", status.code, (int) pj_strlen(&status.reason),
  2146. pj_strbuf(&status.reason));
  2147. AST_LIST_TRAVERSE(&session->supplements, supplement, next) {
  2148. if (!(supplement->response_priority & response_priority)) {
  2149. continue;
  2150. }
  2151. if (supplement->incoming_response && does_method_match(&rdata->msg_info.cseq->method.name, supplement->method)) {
  2152. supplement->incoming_response(session, rdata);
  2153. }
  2154. }
  2155. }
  2156. static int handle_incoming(struct ast_sip_session *session, pjsip_rx_data *rdata,
  2157. enum ast_sip_session_response_priority response_priority)
  2158. {
  2159. ast_debug(3, "Received %s\n", rdata->msg_info.msg->type == PJSIP_REQUEST_MSG ?
  2160. "request" : "response");
  2161. if (rdata->msg_info.msg->type == PJSIP_REQUEST_MSG) {
  2162. handle_incoming_request(session, rdata);
  2163. } else {
  2164. handle_incoming_response(session, rdata, response_priority);
  2165. }
  2166. return 0;
  2167. }
  2168. static void handle_outgoing_request(struct ast_sip_session *session, pjsip_tx_data *tdata)
  2169. {
  2170. struct ast_sip_session_supplement *supplement;
  2171. struct pjsip_request_line req = tdata->msg->line.req;
  2172. ast_debug(3, "Method is %.*s\n", (int) pj_strlen(&req.method.name), pj_strbuf(&req.method.name));
  2173. AST_LIST_TRAVERSE(&session->supplements, supplement, next) {
  2174. if (supplement->outgoing_request && does_method_match(&req.method.name, supplement->method)) {
  2175. supplement->outgoing_request(session, tdata);
  2176. }
  2177. }
  2178. }
  2179. static void handle_outgoing_response(struct ast_sip_session *session, pjsip_tx_data *tdata)
  2180. {
  2181. struct ast_sip_session_supplement *supplement;
  2182. struct pjsip_status_line status = tdata->msg->line.status;
  2183. pjsip_cseq_hdr *cseq = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ, NULL);
  2184. if (!cseq) {
  2185. ast_log(LOG_ERROR, "Cannot send response due to missing sequence header");
  2186. return;
  2187. }
  2188. ast_debug(3, "Method is %.*s, Response is %d %.*s\n", (int) pj_strlen(&cseq->method.name),
  2189. pj_strbuf(&cseq->method.name), status.code, (int) pj_strlen(&status.reason),
  2190. pj_strbuf(&status.reason));
  2191. AST_LIST_TRAVERSE(&session->supplements, supplement, next) {
  2192. if (supplement->outgoing_response && does_method_match(&cseq->method.name, supplement->method)) {
  2193. supplement->outgoing_response(session, tdata);
  2194. }
  2195. }
  2196. }
  2197. static int session_end(void *vsession)
  2198. {
  2199. struct ast_sip_session *session = vsession;
  2200. struct ast_sip_session_supplement *iter;
  2201. /* Stop the scheduled termination */
  2202. sip_session_defer_termination_stop_timer(session);
  2203. /* Session is dead. Notify the supplements. */
  2204. AST_LIST_TRAVERSE(&session->supplements, iter, next) {
  2205. if (iter->session_end) {
  2206. iter->session_end(session);
  2207. }
  2208. }
  2209. /* Release any media resources. */
  2210. ao2_cleanup(session->media);
  2211. session->media = NULL;
  2212. return 0;
  2213. }
  2214. /*!
  2215. * \internal
  2216. * \brief Complete ending session activities.
  2217. * \since 13.5.0
  2218. *
  2219. * \param vsession Which session to complete stopping.
  2220. *
  2221. * \retval 0 on success.
  2222. * \retval -1 on error.
  2223. */
  2224. static int session_end_completion(void *vsession)
  2225. {
  2226. struct ast_sip_session *session = vsession;
  2227. ast_sip_dialog_set_serializer(session->inv_session->dlg, NULL);
  2228. ast_sip_dialog_set_endpoint(session->inv_session->dlg, NULL);
  2229. /* Now we can release the ref that was held by session->inv_session */
  2230. ao2_cleanup(session);
  2231. return 0;
  2232. }
  2233. static void handle_incoming_before_media(pjsip_inv_session *inv,
  2234. struct ast_sip_session *session, pjsip_rx_data *rdata)
  2235. {
  2236. pjsip_msg *msg;
  2237. handle_incoming(session, rdata, AST_SIP_SESSION_BEFORE_MEDIA);
  2238. msg = rdata->msg_info.msg;
  2239. if (msg->type == PJSIP_REQUEST_MSG
  2240. && msg->line.req.method.id == PJSIP_ACK_METHOD
  2241. && pjmedia_sdp_neg_get_state(inv->neg) != PJMEDIA_SDP_NEG_STATE_DONE) {
  2242. pjsip_tx_data *tdata;
  2243. /*
  2244. * SDP negotiation failed on an incoming call that delayed
  2245. * negotiation and then gave us an invalid SDP answer. We
  2246. * need to send a BYE to end the call because of the invalid
  2247. * SDP answer.
  2248. */
  2249. ast_debug(1,
  2250. "Endpoint '%s(%s)': Ending session due to incomplete SDP negotiation. %s\n",
  2251. ast_sorcery_object_get_id(session->endpoint),
  2252. session->channel ? ast_channel_name(session->channel) : "",
  2253. pjsip_rx_data_get_info(rdata));
  2254. if (pjsip_inv_end_session(inv, 400, NULL, &tdata) == PJ_SUCCESS
  2255. && tdata) {
  2256. ast_sip_session_send_request(session, tdata);
  2257. }
  2258. }
  2259. }
  2260. static void session_inv_on_state_changed(pjsip_inv_session *inv, pjsip_event *e)
  2261. {
  2262. struct ast_sip_session *session;
  2263. pjsip_event_id_e type;
  2264. if (ast_shutdown_final()) {
  2265. return;
  2266. }
  2267. if (e) {
  2268. print_debug_details(inv, NULL, e);
  2269. type = e->type;
  2270. } else {
  2271. type = PJSIP_EVENT_UNKNOWN;
  2272. }
  2273. session = inv->mod_data[session_module.id];
  2274. if (!session) {
  2275. return;
  2276. }
  2277. switch(type) {
  2278. case PJSIP_EVENT_TX_MSG:
  2279. break;
  2280. case PJSIP_EVENT_RX_MSG:
  2281. handle_incoming_before_media(inv, session, e->body.rx_msg.rdata);
  2282. break;
  2283. case PJSIP_EVENT_TSX_STATE:
  2284. ast_debug(3, "Source of transaction state change is %s\n", pjsip_event_str(e->body.tsx_state.type));
  2285. /* Transaction state changes are prompted by some other underlying event. */
  2286. switch(e->body.tsx_state.type) {
  2287. case PJSIP_EVENT_TX_MSG:
  2288. break;
  2289. case PJSIP_EVENT_RX_MSG:
  2290. handle_incoming_before_media(inv, session, e->body.tsx_state.src.rdata);
  2291. break;
  2292. case PJSIP_EVENT_TRANSPORT_ERROR:
  2293. case PJSIP_EVENT_TIMER:
  2294. case PJSIP_EVENT_USER:
  2295. case PJSIP_EVENT_UNKNOWN:
  2296. case PJSIP_EVENT_TSX_STATE:
  2297. /* Inception? */
  2298. break;
  2299. }
  2300. break;
  2301. case PJSIP_EVENT_TRANSPORT_ERROR:
  2302. case PJSIP_EVENT_TIMER:
  2303. case PJSIP_EVENT_UNKNOWN:
  2304. case PJSIP_EVENT_USER:
  2305. default:
  2306. break;
  2307. }
  2308. if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
  2309. if (session->defer_end) {
  2310. ast_debug(3, "Deferring session (%p) end\n", session);
  2311. session->ended_while_deferred = 1;
  2312. return;
  2313. }
  2314. if (ast_sip_push_task(session->serializer, session_end, session)) {
  2315. /* Do it anyway even though this is not the right thread. */
  2316. session_end(session);
  2317. }
  2318. }
  2319. }
  2320. static void session_inv_on_new_session(pjsip_inv_session *inv, pjsip_event *e)
  2321. {
  2322. /* XXX STUB */
  2323. }
  2324. static int session_end_if_disconnected(int id, pjsip_inv_session *inv)
  2325. {
  2326. struct ast_sip_session *session;
  2327. if (inv->state != PJSIP_INV_STATE_DISCONNECTED) {
  2328. return 0;
  2329. }
  2330. /*
  2331. * We are locking because ast_sip_dialog_get_session() needs
  2332. * the dialog locked to get the session by other threads.
  2333. */
  2334. pjsip_dlg_inc_lock(inv->dlg);
  2335. session = inv->mod_data[id];
  2336. inv->mod_data[id] = NULL;
  2337. pjsip_dlg_dec_lock(inv->dlg);
  2338. /*
  2339. * Pass the session ref held by session->inv_session to
  2340. * session_end_completion().
  2341. */
  2342. if (session
  2343. && ast_sip_push_task(session->serializer, session_end_completion, session)) {
  2344. /* Do it anyway even though this is not the right thread. */
  2345. session_end_completion(session);
  2346. }
  2347. return 1;
  2348. }
  2349. static void session_inv_on_tsx_state_changed(pjsip_inv_session *inv, pjsip_transaction *tsx, pjsip_event *e)
  2350. {
  2351. ast_sip_session_response_cb cb;
  2352. int id = session_module.id;
  2353. struct ast_sip_session *session;
  2354. pjsip_tx_data *tdata;
  2355. if (ast_shutdown_final()) {
  2356. return;
  2357. }
  2358. session = inv->mod_data[id];
  2359. print_debug_details(inv, tsx, e);
  2360. if (!session) {
  2361. /* The session has ended. Ignore the transaction change. */
  2362. return;
  2363. }
  2364. /*
  2365. * If the session is disconnected really nothing else to do unless currently transacting
  2366. * a BYE. If a BYE then hold off destruction until the transaction timeout occurs. This
  2367. * has to be done for BYEs because sometimes the dialog can be in a disconnected
  2368. * state but the BYE request transaction has not yet completed.
  2369. */
  2370. if (tsx->method.id != PJSIP_BYE_METHOD && session_end_if_disconnected(id, inv)) {
  2371. return;
  2372. }
  2373. switch (e->body.tsx_state.type) {
  2374. case PJSIP_EVENT_TX_MSG:
  2375. /* When we create an outgoing request, we do not have access to the transaction that
  2376. * is created. Instead, We have to place transaction-specific data in the tdata. Here,
  2377. * we transfer the data into the transaction. This way, when we receive a response, we
  2378. * can dig this data out again
  2379. */
  2380. tsx->mod_data[id] = e->body.tsx_state.src.tdata->mod_data[id];
  2381. break;
  2382. case PJSIP_EVENT_RX_MSG:
  2383. cb = ast_sip_mod_data_get(tsx->mod_data, id, MOD_DATA_ON_RESPONSE);
  2384. /* As the PJSIP invite session implementation responds with a 200 OK before we have a
  2385. * chance to be invoked session supplements for BYE requests actually end up executing
  2386. * in the invite session state callback as well. To prevent session supplements from
  2387. * running on the BYE request again we explicitly squash invocation of them here.
  2388. */
  2389. if ((e->body.tsx_state.src.rdata->msg_info.msg->type != PJSIP_REQUEST_MSG) ||
  2390. (tsx->method.id != PJSIP_BYE_METHOD)) {
  2391. handle_incoming(session, e->body.tsx_state.src.rdata,
  2392. AST_SIP_SESSION_AFTER_MEDIA);
  2393. }
  2394. if (tsx->method.id == PJSIP_INVITE_METHOD) {
  2395. if (tsx->role == PJSIP_ROLE_UAC) {
  2396. if (tsx->state == PJSIP_TSX_STATE_COMPLETED) {
  2397. /* This means we got a non 2XX final response to our outgoing INVITE */
  2398. if (tsx->status_code == PJSIP_SC_REQUEST_PENDING) {
  2399. reschedule_reinvite(session, cb);
  2400. return;
  2401. }
  2402. if (inv->state == PJSIP_INV_STATE_CONFIRMED) {
  2403. ast_debug(1, "reINVITE received final response code %d\n",
  2404. tsx->status_code);
  2405. if ((tsx->status_code == 401 || tsx->status_code == 407)
  2406. && !ast_sip_create_request_with_auth(
  2407. &session->endpoint->outbound_auths,
  2408. e->body.tsx_state.src.rdata, tsx, &tdata)) {
  2409. /* Send authed reINVITE */
  2410. ast_sip_session_send_request_with_cb(session, tdata, cb);
  2411. return;
  2412. }
  2413. if (tsx->status_code != 488) {
  2414. /* Other reinvite failures (except 488) result in destroying the session. */
  2415. if (pjsip_inv_end_session(inv, 500, NULL, &tdata) == PJ_SUCCESS
  2416. && tdata) {
  2417. ast_sip_session_send_request(session, tdata);
  2418. }
  2419. }
  2420. }
  2421. } else if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
  2422. if (inv->cancelling && tsx->status_code == PJSIP_SC_OK) {
  2423. int sdp_negotiation_done =
  2424. pjmedia_sdp_neg_get_state(inv->neg) == PJMEDIA_SDP_NEG_STATE_DONE;
  2425. /*
  2426. * We can get here for the following reasons.
  2427. *
  2428. * 1) The race condition detailed in RFC5407 section 3.1.2.
  2429. * We sent a CANCEL at the same time that the UAS sent us a
  2430. * 200 OK with a valid SDP for the original INVITE. As a
  2431. * result, we have now received a 200 OK for a cancelled
  2432. * call and the SDP negotiation is complete. We need to
  2433. * immediately send a BYE to end the dialog.
  2434. *
  2435. * 2) We sent a CANCEL and hit the race condition but the
  2436. * UAS sent us an invalid SDP with the 200 OK. In this case
  2437. * the SDP negotiation is incomplete and PJPROJECT has
  2438. * already sent the BYE for us because of the invalid SDP.
  2439. *
  2440. * 3) We didn't send a CANCEL but the UAS sent us an invalid
  2441. * SDP with the 200 OK. In this case the SDP negotiation is
  2442. * incomplete and PJPROJECT has already sent the BYE for us
  2443. * because of the invalid SDP.
  2444. */
  2445. ast_test_suite_event_notify("PJSIP_SESSION_CANCELED",
  2446. "Endpoint: %s\r\n"
  2447. "Channel: %s\r\n"
  2448. "Message: %s\r\n"
  2449. "SDP: %s",
  2450. ast_sorcery_object_get_id(session->endpoint),
  2451. session->channel ? ast_channel_name(session->channel) : "",
  2452. pjsip_rx_data_get_info(e->body.tsx_state.src.rdata),
  2453. sdp_negotiation_done ? "complete" : "incomplete");
  2454. if (!sdp_negotiation_done) {
  2455. ast_debug(1, "Endpoint '%s(%s)': Incomplete SDP negotiation cancelled session. %s\n",
  2456. ast_sorcery_object_get_id(session->endpoint),
  2457. session->channel ? ast_channel_name(session->channel) : "",
  2458. pjsip_rx_data_get_info(e->body.tsx_state.src.rdata));
  2459. } else if (pjsip_inv_end_session(inv, 500, NULL, &tdata) == PJ_SUCCESS
  2460. && tdata) {
  2461. ast_debug(1, "Endpoint '%s(%s)': Ending session due to RFC5407 race condition. %s\n",
  2462. ast_sorcery_object_get_id(session->endpoint),
  2463. session->channel ? ast_channel_name(session->channel) : "",
  2464. pjsip_rx_data_get_info(e->body.tsx_state.src.rdata));
  2465. ast_sip_session_send_request(session, tdata);
  2466. }
  2467. }
  2468. }
  2469. }
  2470. } else {
  2471. /* All other methods */
  2472. if (tsx->role == PJSIP_ROLE_UAC) {
  2473. if (tsx->state == PJSIP_TSX_STATE_COMPLETED) {
  2474. /* This means we got a final response to our outgoing method */
  2475. ast_debug(1, "%.*s received final response code %d\n",
  2476. (int) pj_strlen(&tsx->method.name), pj_strbuf(&tsx->method.name),
  2477. tsx->status_code);
  2478. if ((tsx->status_code == 401 || tsx->status_code == 407)
  2479. && !ast_sip_create_request_with_auth(
  2480. &session->endpoint->outbound_auths,
  2481. e->body.tsx_state.src.rdata, tsx, &tdata)) {
  2482. /* Send authed version of the method */
  2483. ast_sip_session_send_request_with_cb(session, tdata, cb);
  2484. return;
  2485. }
  2486. }
  2487. }
  2488. }
  2489. if (cb) {
  2490. cb(session, e->body.tsx_state.src.rdata);
  2491. }
  2492. break;
  2493. case PJSIP_EVENT_TRANSPORT_ERROR:
  2494. case PJSIP_EVENT_TIMER:
  2495. /*
  2496. * The timer event is run by the pjsip monitor thread and not
  2497. * by the session serializer.
  2498. */
  2499. if (session_end_if_disconnected(id, inv)) {
  2500. return;
  2501. }
  2502. break;
  2503. case PJSIP_EVENT_USER:
  2504. case PJSIP_EVENT_UNKNOWN:
  2505. case PJSIP_EVENT_TSX_STATE:
  2506. /* Inception? */
  2507. break;
  2508. }
  2509. if (AST_LIST_EMPTY(&session->delayed_requests)) {
  2510. /* No delayed request pending, so just return */
  2511. return;
  2512. }
  2513. if (tsx->method.id == PJSIP_INVITE_METHOD) {
  2514. if (tsx->state == PJSIP_TSX_STATE_PROCEEDING) {
  2515. ast_debug(3, "Endpoint '%s(%s)' INVITE delay check. tsx-state:%s\n",
  2516. ast_sorcery_object_get_id(session->endpoint),
  2517. session->channel ? ast_channel_name(session->channel) : "",
  2518. pjsip_tsx_state_str(tsx->state));
  2519. check_delayed_requests(session, invite_proceeding);
  2520. } else if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
  2521. /*
  2522. * Terminated INVITE transactions always should result in
  2523. * queuing delayed requests, no matter what event caused
  2524. * the transaction to terminate.
  2525. */
  2526. ast_debug(3, "Endpoint '%s(%s)' INVITE delay check. tsx-state:%s\n",
  2527. ast_sorcery_object_get_id(session->endpoint),
  2528. session->channel ? ast_channel_name(session->channel) : "",
  2529. pjsip_tsx_state_str(tsx->state));
  2530. check_delayed_requests(session, invite_terminated);
  2531. }
  2532. } else if (tsx->role == PJSIP_ROLE_UAC
  2533. && tsx->state == PJSIP_TSX_STATE_COMPLETED
  2534. && !pj_strcmp2(&tsx->method.name, "UPDATE")) {
  2535. ast_debug(3, "Endpoint '%s(%s)' UPDATE delay check. tsx-state:%s\n",
  2536. ast_sorcery_object_get_id(session->endpoint),
  2537. session->channel ? ast_channel_name(session->channel) : "",
  2538. pjsip_tsx_state_str(tsx->state));
  2539. check_delayed_requests(session, update_completed);
  2540. }
  2541. }
  2542. static int add_sdp_streams(void *obj, void *arg, void *data, int flags)
  2543. {
  2544. struct ast_sip_session_media *session_media = obj;
  2545. pjmedia_sdp_session *answer = arg;
  2546. struct ast_sip_session *session = data;
  2547. struct ast_sip_session_sdp_handler *handler = session_media->handler;
  2548. RAII_VAR(struct sdp_handler_list *, handler_list, NULL, ao2_cleanup);
  2549. int res;
  2550. if (handler) {
  2551. /* if an already assigned handler reports a catastrophic error, fail */
  2552. res = handler->create_outgoing_sdp_stream(session, session_media, answer);
  2553. if (res < 0) {
  2554. return 0;
  2555. }
  2556. return CMP_MATCH;
  2557. }
  2558. handler_list = ao2_find(sdp_handlers, session_media->stream_type, OBJ_KEY);
  2559. if (!handler_list) {
  2560. return CMP_MATCH;
  2561. }
  2562. /* no handler for this stream type and we have a list to search */
  2563. AST_LIST_TRAVERSE(&handler_list->list, handler, next) {
  2564. if (handler == session_media->handler) {
  2565. continue;
  2566. }
  2567. res = handler->create_outgoing_sdp_stream(session, session_media, answer);
  2568. if (res < 0) {
  2569. /* catastrophic error */
  2570. return 0;
  2571. }
  2572. if (res > 0) {
  2573. /* Handled by this handler. Move to the next stream */
  2574. session_media_set_handler(session_media, handler);
  2575. return CMP_MATCH;
  2576. }
  2577. }
  2578. /* streams that weren't handled won't be included in generated outbound SDP */
  2579. return CMP_MATCH;
  2580. }
  2581. static struct pjmedia_sdp_session *create_local_sdp(pjsip_inv_session *inv, struct ast_sip_session *session, const pjmedia_sdp_session *offer)
  2582. {
  2583. RAII_VAR(struct ao2_iterator *, successful, NULL, ao2_iterator_cleanup);
  2584. static const pj_str_t STR_IN = { "IN", 2 };
  2585. static const pj_str_t STR_IP4 = { "IP4", 3 };
  2586. static const pj_str_t STR_IP6 = { "IP6", 3 };
  2587. pjmedia_sdp_session *local;
  2588. if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
  2589. ast_log(LOG_ERROR, "Failed to create session SDP. Session has been already disconnected\n");
  2590. return NULL;
  2591. }
  2592. if (!inv->pool_prov || !(local = PJ_POOL_ZALLOC_T(inv->pool_prov, pjmedia_sdp_session))) {
  2593. return NULL;
  2594. }
  2595. if (!offer) {
  2596. local->origin.version = local->origin.id = (pj_uint32_t)(ast_random());
  2597. } else {
  2598. local->origin.version = offer->origin.version + 1;
  2599. local->origin.id = offer->origin.id;
  2600. }
  2601. pj_strdup2(inv->pool_prov, &local->origin.user, session->endpoint->media.sdpowner);
  2602. pj_strdup2(inv->pool_prov, &local->name, session->endpoint->media.sdpsession);
  2603. /* Now let the handlers add streams of various types, pjmedia will automatically reorder the media streams for us */
  2604. successful = ao2_callback_data(session->media, OBJ_MULTIPLE, add_sdp_streams, local, session);
  2605. if (!successful || ao2_iterator_count(successful) != ao2_container_count(session->media)) {
  2606. /* Something experienced a catastrophic failure */
  2607. return NULL;
  2608. }
  2609. /* Use the connection details of the first media stream if possible for SDP level */
  2610. if (local->media_count) {
  2611. int stream;
  2612. /* Since we are using the first media stream as the SDP level we can get rid of it
  2613. * from the stream itself
  2614. */
  2615. local->conn = local->media[0]->conn;
  2616. local->media[0]->conn = NULL;
  2617. pj_strassign(&local->origin.net_type, &local->conn->net_type);
  2618. pj_strassign(&local->origin.addr_type, &local->conn->addr_type);
  2619. pj_strassign(&local->origin.addr, &local->conn->addr);
  2620. /* Go through each media stream seeing if the connection details actually differ,
  2621. * if not just use SDP level and reduce the SDP size
  2622. */
  2623. for (stream = 1; stream < local->media_count; stream++) {
  2624. if (!pj_strcmp(&local->conn->net_type, &local->media[stream]->conn->net_type) &&
  2625. !pj_strcmp(&local->conn->addr_type, &local->media[stream]->conn->addr_type) &&
  2626. !pj_strcmp(&local->conn->addr, &local->media[stream]->conn->addr)) {
  2627. local->media[stream]->conn = NULL;
  2628. }
  2629. }
  2630. } else {
  2631. local->origin.net_type = STR_IN;
  2632. local->origin.addr_type = session->endpoint->media.rtp.ipv6 ? STR_IP6 : STR_IP4;
  2633. if (!ast_strlen_zero(session->endpoint->media.address)) {
  2634. pj_strdup2(inv->pool_prov, &local->origin.addr, session->endpoint->media.address);
  2635. } else {
  2636. pj_strdup2(inv->pool_prov, &local->origin.addr, ast_sip_get_host_ip_string(session->endpoint->media.rtp.ipv6 ? pj_AF_INET6() : pj_AF_INET()));
  2637. }
  2638. }
  2639. return local;
  2640. }
  2641. static void session_inv_on_rx_offer(pjsip_inv_session *inv, const pjmedia_sdp_session *offer)
  2642. {
  2643. struct ast_sip_session *session;
  2644. pjmedia_sdp_session *answer;
  2645. if (ast_shutdown_final()) {
  2646. return;
  2647. }
  2648. session = inv->mod_data[session_module.id];
  2649. if (handle_incoming_sdp(session, offer)) {
  2650. return;
  2651. }
  2652. if ((answer = create_local_sdp(inv, session, offer))) {
  2653. pjsip_inv_set_sdp_answer(inv, answer);
  2654. }
  2655. }
  2656. #if 0
  2657. static void session_inv_on_create_offer(pjsip_inv_session *inv, pjmedia_sdp_session **p_offer)
  2658. {
  2659. /* XXX STUB */
  2660. }
  2661. #endif
  2662. static void session_inv_on_media_update(pjsip_inv_session *inv, pj_status_t status)
  2663. {
  2664. struct ast_sip_session *session;
  2665. const pjmedia_sdp_session *local, *remote;
  2666. if (ast_shutdown_final()) {
  2667. return;
  2668. }
  2669. session = inv->mod_data[session_module.id];
  2670. if (!session || !session->channel) {
  2671. /*
  2672. * If we don't have a session or channel then we really
  2673. * don't care about media updates.
  2674. * Just ignore
  2675. */
  2676. return;
  2677. }
  2678. if (session->endpoint) {
  2679. int bail = 0;
  2680. /*
  2681. * If following_fork is set, then this is probably the result of a
  2682. * forked INVITE and SDP asnwers coming from the different fork UAS
  2683. * destinations. In this case updated_sdp_answer will also be set.
  2684. *
  2685. * If only updated_sdp_answer is set, then this is the non-forking
  2686. * scenario where the same UAS just needs to change something like
  2687. * the media port.
  2688. */
  2689. if (inv->following_fork) {
  2690. if (session->endpoint->follow_early_media_fork) {
  2691. ast_debug(3, "Following early media fork with different To tags\n");
  2692. } else {
  2693. ast_debug(3, "Not following early media fork with different To tags\n");
  2694. bail = 1;
  2695. }
  2696. }
  2697. #ifdef HAVE_PJSIP_INV_ACCEPT_MULTIPLE_SDP_ANSWERS
  2698. else if (inv->updated_sdp_answer) {
  2699. if (session->endpoint->accept_multiple_sdp_answers) {
  2700. ast_debug(3, "Accepting updated SDP with same To tag\n");
  2701. } else {
  2702. ast_debug(3, "Ignoring updated SDP answer with same To tag\n");
  2703. bail = 1;
  2704. }
  2705. }
  2706. #endif
  2707. if (bail) {
  2708. return;
  2709. }
  2710. }
  2711. if ((status != PJ_SUCCESS) || (pjmedia_sdp_neg_get_active_local(inv->neg, &local) != PJ_SUCCESS) ||
  2712. (pjmedia_sdp_neg_get_active_remote(inv->neg, &remote) != PJ_SUCCESS)) {
  2713. ast_channel_hangupcause_set(session->channel, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
  2714. ast_set_hangupsource(session->channel, ast_channel_name(session->channel), 0);
  2715. ast_queue_hangup(session->channel);
  2716. return;
  2717. }
  2718. handle_negotiated_sdp(session, local, remote);
  2719. }
  2720. static pjsip_redirect_op session_inv_on_redirected(pjsip_inv_session *inv, const pjsip_uri *target, const pjsip_event *e)
  2721. {
  2722. struct ast_sip_session *session;
  2723. const pjsip_sip_uri *uri;
  2724. if (ast_shutdown_final()) {
  2725. return PJSIP_REDIRECT_STOP;
  2726. }
  2727. session = inv->mod_data[session_module.id];
  2728. if (!session || !session->channel) {
  2729. return PJSIP_REDIRECT_STOP;
  2730. }
  2731. if (session->endpoint->redirect_method == AST_SIP_REDIRECT_URI_PJSIP) {
  2732. return PJSIP_REDIRECT_ACCEPT;
  2733. }
  2734. if (!PJSIP_URI_SCHEME_IS_SIP(target) && !PJSIP_URI_SCHEME_IS_SIPS(target)) {
  2735. return PJSIP_REDIRECT_STOP;
  2736. }
  2737. handle_incoming(session, e->body.rx_msg.rdata, AST_SIP_SESSION_BEFORE_REDIRECTING);
  2738. uri = pjsip_uri_get_uri(target);
  2739. if (session->endpoint->redirect_method == AST_SIP_REDIRECT_USER) {
  2740. char exten[AST_MAX_EXTENSION];
  2741. ast_copy_pj_str(exten, &uri->user, sizeof(exten));
  2742. /*
  2743. * We may want to match in the dialplan without any user
  2744. * options getting in the way.
  2745. */
  2746. AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(exten);
  2747. ast_channel_call_forward_set(session->channel, exten);
  2748. } else if (session->endpoint->redirect_method == AST_SIP_REDIRECT_URI_CORE) {
  2749. char target_uri[PJSIP_MAX_URL_SIZE];
  2750. /* PJSIP/ + endpoint length + / + max URL size */
  2751. char forward[8 + strlen(ast_sorcery_object_get_id(session->endpoint)) + PJSIP_MAX_URL_SIZE];
  2752. pjsip_uri_print(PJSIP_URI_IN_REQ_URI, uri, target_uri, sizeof(target_uri));
  2753. sprintf(forward, "PJSIP/%s/%s", ast_sorcery_object_get_id(session->endpoint), target_uri);
  2754. ast_channel_call_forward_set(session->channel, forward);
  2755. }
  2756. return PJSIP_REDIRECT_STOP;
  2757. }
  2758. static pjsip_inv_callback inv_callback = {
  2759. .on_state_changed = session_inv_on_state_changed,
  2760. .on_new_session = session_inv_on_new_session,
  2761. .on_tsx_state_changed = session_inv_on_tsx_state_changed,
  2762. .on_rx_offer = session_inv_on_rx_offer,
  2763. .on_media_update = session_inv_on_media_update,
  2764. .on_redirected = session_inv_on_redirected,
  2765. };
  2766. /*! \brief Hook for modifying outgoing messages with SDP to contain the proper address information */
  2767. static void session_outgoing_nat_hook(pjsip_tx_data *tdata, struct ast_sip_transport *transport)
  2768. {
  2769. RAII_VAR(struct ast_sip_transport_state *, transport_state, ast_sip_get_transport_state(ast_sorcery_object_get_id(transport)), ao2_cleanup);
  2770. struct ast_sip_nat_hook *hook = ast_sip_mod_data_get(
  2771. tdata->mod_data, session_module.id, MOD_DATA_NAT_HOOK);
  2772. struct pjmedia_sdp_session *sdp;
  2773. int stream;
  2774. /* SDP produced by us directly will never be multipart */
  2775. if (!transport_state || hook || !tdata->msg->body ||
  2776. !ast_sip_is_content_type(&tdata->msg->body->content_type, "application", "sdp") ||
  2777. ast_strlen_zero(transport->external_media_address)) {
  2778. return;
  2779. }
  2780. sdp = tdata->msg->body->data;
  2781. if (sdp->conn) {
  2782. char host[NI_MAXHOST];
  2783. struct ast_sockaddr our_sdp_addr = { { 0, } };
  2784. ast_copy_pj_str(host, &sdp->conn->addr, sizeof(host));
  2785. ast_sockaddr_parse(&our_sdp_addr, host, PARSE_PORT_FORBID);
  2786. /* Reversed check here. We don't check the remote
  2787. * endpoint being in our local net, but whether our
  2788. * outgoing session IP is local. If it is, we'll do
  2789. * rewriting. No localnet configured? Always rewrite. */
  2790. if (ast_sip_transport_is_local(transport_state, &our_sdp_addr) || !transport_state->localnet) {
  2791. ast_debug(5, "Setting external media address to %s\n", ast_sockaddr_stringify_host(&transport_state->external_media_address));
  2792. pj_strdup2(tdata->pool, &sdp->conn->addr, ast_sockaddr_stringify_host(&transport_state->external_media_address));
  2793. pj_strassign(&sdp->origin.addr, &sdp->conn->addr);
  2794. }
  2795. }
  2796. for (stream = 0; stream < sdp->media_count; ++stream) {
  2797. /* See if there are registered handlers for this media stream type */
  2798. char media[20];
  2799. struct ast_sip_session_sdp_handler *handler;
  2800. RAII_VAR(struct sdp_handler_list *, handler_list, NULL, ao2_cleanup);
  2801. /* We need a null-terminated version of the media string */
  2802. ast_copy_pj_str(media, &sdp->media[stream]->desc.media, sizeof(media));
  2803. handler_list = ao2_find(sdp_handlers, media, OBJ_KEY);
  2804. if (!handler_list) {
  2805. ast_debug(1, "No registered SDP handlers for media type '%s'\n", media);
  2806. continue;
  2807. }
  2808. AST_LIST_TRAVERSE(&handler_list->list, handler, next) {
  2809. if (handler->change_outgoing_sdp_stream_media_address) {
  2810. handler->change_outgoing_sdp_stream_media_address(tdata, sdp->media[stream], transport);
  2811. }
  2812. }
  2813. }
  2814. /* We purposely do this so that the hook will not be invoked multiple times, ie: if a retransmit occurs */
  2815. ast_sip_mod_data_set(tdata->pool, tdata->mod_data, session_module.id, MOD_DATA_NAT_HOOK, nat_hook);
  2816. }
  2817. static int load_module(void)
  2818. {
  2819. pjsip_endpoint *endpt;
  2820. CHECK_PJSIP_MODULE_LOADED();
  2821. if (!ast_sip_get_sorcery() || !ast_sip_get_pjsip_endpoint()) {
  2822. return AST_MODULE_LOAD_DECLINE;
  2823. }
  2824. if (!(nat_hook = ast_sorcery_alloc(ast_sip_get_sorcery(), "nat_hook", NULL))) {
  2825. return AST_MODULE_LOAD_DECLINE;
  2826. }
  2827. nat_hook->outgoing_external_message = session_outgoing_nat_hook;
  2828. ast_sorcery_create(ast_sip_get_sorcery(), nat_hook);
  2829. sdp_handlers = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  2830. SDP_HANDLER_BUCKETS, sdp_handler_list_hash, NULL, sdp_handler_list_cmp);
  2831. if (!sdp_handlers) {
  2832. return AST_MODULE_LOAD_DECLINE;
  2833. }
  2834. endpt = ast_sip_get_pjsip_endpoint();
  2835. pjsip_inv_usage_init(endpt, &inv_callback);
  2836. pjsip_100rel_init_module(endpt);
  2837. pjsip_timer_init_module(endpt);
  2838. if (ast_sip_register_service(&session_module)) {
  2839. return AST_MODULE_LOAD_DECLINE;
  2840. }
  2841. ast_sip_register_service(&session_reinvite_module);
  2842. ast_sip_register_service(&outbound_invite_auth_module);
  2843. ast_module_shutdown_ref(ast_module_info->self);
  2844. return AST_MODULE_LOAD_SUCCESS;
  2845. }
  2846. static int unload_module(void)
  2847. {
  2848. ast_sip_unregister_service(&outbound_invite_auth_module);
  2849. ast_sip_unregister_service(&session_reinvite_module);
  2850. ast_sip_unregister_service(&session_module);
  2851. ast_sorcery_delete(ast_sip_get_sorcery(), nat_hook);
  2852. ao2_cleanup(nat_hook);
  2853. ao2_cleanup(sdp_handlers);
  2854. return 0;
  2855. }
  2856. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "PJSIP Session resource",
  2857. .support_level = AST_MODULE_SUPPORT_CORE,
  2858. .load = load_module,
  2859. .unload = unload_module,
  2860. .load_pri = AST_MODPRI_APP_DEPEND,
  2861. );