res_pjsip_pubsub.c 132 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298
  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. /*!
  19. * \brief Opaque structure representing an RFC 3265 SIP subscription
  20. */
  21. /*** MODULEINFO
  22. <depend>pjproject</depend>
  23. <depend>res_pjsip</depend>
  24. <support_level>core</support_level>
  25. ***/
  26. #include "asterisk.h"
  27. #include <pjsip.h>
  28. #include <pjsip_simple.h>
  29. #include <pjlib.h>
  30. #include "asterisk/res_pjsip_pubsub.h"
  31. #include "asterisk/module.h"
  32. #include "asterisk/linkedlists.h"
  33. #include "asterisk/astobj2.h"
  34. #include "asterisk/datastore.h"
  35. #include "asterisk/uuid.h"
  36. #include "asterisk/taskprocessor.h"
  37. #include "asterisk/sched.h"
  38. #include "asterisk/res_pjsip.h"
  39. #include "asterisk/callerid.h"
  40. #include "asterisk/manager.h"
  41. #include "asterisk/test.h"
  42. #include "res_pjsip/include/res_pjsip_private.h"
  43. #include "asterisk/res_pjsip_presence_xml.h"
  44. /*** DOCUMENTATION
  45. <manager name="PJSIPShowSubscriptionsInbound" language="en_US">
  46. <synopsis>
  47. Lists subscriptions.
  48. </synopsis>
  49. <syntax />
  50. <description>
  51. <para>
  52. Provides a listing of all inbound subscriptions. An event <literal>InboundSubscriptionDetail</literal>
  53. is issued for each subscription object. Once all detail events are completed an
  54. <literal>InboundSubscriptionDetailComplete</literal> event is issued.
  55. </para>
  56. </description>
  57. </manager>
  58. <manager name="PJSIPShowSubscriptionsOutbound" language="en_US">
  59. <synopsis>
  60. Lists subscriptions.
  61. </synopsis>
  62. <syntax />
  63. <description>
  64. <para>
  65. Provides a listing of all outbound subscriptions. An event <literal>OutboundSubscriptionDetail</literal>
  66. is issued for each subscription object. Once all detail events are completed an
  67. <literal>OutboundSubscriptionDetailComplete</literal> event is issued.
  68. </para>
  69. </description>
  70. </manager>
  71. <manager name="PJSIPShowResourceLists" language="en_US">
  72. <synopsis>
  73. Displays settings for configured resource lists.
  74. </synopsis>
  75. <syntax />
  76. <description>
  77. <para>
  78. Provides a listing of all resource lists. An event <literal>ResourceListDetail</literal>
  79. is issued for each resource list object. Once all detail events are completed a
  80. <literal>ResourceListDetailComplete</literal> event is issued.
  81. </para>
  82. </description>
  83. </manager>
  84. <configInfo name="res_pjsip_pubsub" language="en_US">
  85. <synopsis>Module that implements publish and subscribe support.</synopsis>
  86. <configFile name="pjsip.conf">
  87. <configObject name="subscription_persistence">
  88. <synopsis>Persists SIP subscriptions so they survive restarts.</synopsis>
  89. <configOption name="packet">
  90. <synopsis>Entire SIP SUBSCRIBE packet that created the subscription</synopsis>
  91. </configOption>
  92. <configOption name="src_name">
  93. <synopsis>The source address of the subscription</synopsis>
  94. </configOption>
  95. <configOption name="src_port">
  96. <synopsis>The source port of the subscription</synopsis>
  97. </configOption>
  98. <configOption name="transport_key">
  99. <synopsis>The type of transport the subscription was received on</synopsis>
  100. </configOption>
  101. <configOption name="local_name">
  102. <synopsis>The local address the subscription was received on</synopsis>
  103. </configOption>
  104. <configOption name="local_port">
  105. <synopsis>The local port the subscription was received on</synopsis>
  106. </configOption>
  107. <configOption name="cseq">
  108. <synopsis>The sequence number of the next NOTIFY to be sent</synopsis>
  109. </configOption>
  110. <configOption name="tag">
  111. <synopsis>The local tag of the dialog for the subscription</synopsis>
  112. </configOption>
  113. <configOption name="endpoint">
  114. <synopsis>The name of the endpoint that subscribed</synopsis>
  115. </configOption>
  116. <configOption name="expires">
  117. <synopsis>The time at which the subscription expires</synopsis>
  118. </configOption>
  119. </configObject>
  120. <configObject name="resource_list">
  121. <synopsis>Resource list configuration parameters.</synopsis>
  122. <description>
  123. <para>This configuration object allows for RFC 4662 resource list subscriptions
  124. to be specified. This can be useful to decrease the amount of subscription traffic
  125. that a server has to process.</para>
  126. <note>
  127. <para>Current limitations limit the size of SIP NOTIFY requests that Asterisk sends
  128. to 64000 bytes. If your resource list notifications are larger than this maximum, you
  129. will need to make adjustments.</para>
  130. </note>
  131. </description>
  132. <configOption name="type">
  133. <synopsis>Must be of type 'resource_list'</synopsis>
  134. </configOption>
  135. <configOption name="event">
  136. <synopsis>The SIP event package that the list resource belong to.</synopsis>
  137. <description><para>
  138. The SIP event package describes the types of resources that Asterisk reports
  139. the state of.
  140. </para>
  141. <enumlist>
  142. <enum name="presence"><para>
  143. Device state and presence reporting.
  144. </para></enum>
  145. <enum name="message-summary"><para>
  146. Message-waiting indication (MWI) reporting.
  147. </para></enum>
  148. </enumlist>
  149. </description>
  150. </configOption>
  151. <configOption name="list_item">
  152. <synopsis>The name of a resource to report state on</synopsis>
  153. <description>
  154. <para>In general Asterisk looks up list items in the following way:</para>
  155. <para>1. Check if the list item refers to another configured resource list.</para>
  156. <para>2. Pass the name of the resource off to event-package-specific handlers
  157. to find the specified resource.</para>
  158. <para>The second part means that the way the list item is specified depends
  159. on what type of list this is. For instance, if you have the <replaceable>event</replaceable>
  160. set to <literal>presence</literal>, then list items should be in the form of
  161. dialplan_extension@dialplan_context. For <literal>message-summary</literal> mailbox
  162. names should be listed.</para>
  163. </description>
  164. </configOption>
  165. <configOption name="full_state" default="no">
  166. <synopsis>Indicates if the entire list's state should be sent out.</synopsis>
  167. <description>
  168. <para>If this option is enabled, and a resource changes state, then Asterisk will construct
  169. a notification that contains the state of all resources in the list. If the option is
  170. disabled, Asterisk will construct a notification that only contains the states of
  171. resources that have changed.</para>
  172. <note>
  173. <para>Even with this option disabled, there are certain situations where Asterisk is forced
  174. to send a notification with the states of all resources in the list. When a subscriber
  175. renews or terminates its subscription to the list, Asterisk MUST send a full state
  176. notification.</para>
  177. </note>
  178. </description>
  179. </configOption>
  180. <configOption name="notification_batch_interval" default="0">
  181. <synopsis>Time Asterisk should wait, in milliseconds, before sending notifications.</synopsis>
  182. <description>
  183. <para>When a resource's state changes, it may be desired to wait a certain amount before Asterisk
  184. sends a notification to subscribers. This allows for other state changes to accumulate, so that
  185. Asterisk can communicate multiple state changes in a single notification instead of rapidly sending
  186. many notifications.</para>
  187. </description>
  188. </configOption>
  189. </configObject>
  190. <configObject name="inbound-publication">
  191. <synopsis>The configuration for inbound publications</synopsis>
  192. <configOption name="endpoint" default="">
  193. <synopsis>Optional name of an endpoint that is only allowed to publish to this resource</synopsis>
  194. </configOption>
  195. <configOption name="type">
  196. <synopsis>Must be of type 'inbound-publication'.</synopsis>
  197. </configOption>
  198. </configObject>
  199. </configFile>
  200. </configInfo>
  201. ***/
  202. static pj_bool_t pubsub_on_rx_request(pjsip_rx_data *rdata);
  203. static struct pjsip_module pubsub_module = {
  204. .name = { "PubSub Module", 13 },
  205. .priority = PJSIP_MOD_PRIORITY_APPLICATION,
  206. .on_rx_request = pubsub_on_rx_request,
  207. };
  208. #define MOD_DATA_PERSISTENCE "sub_persistence"
  209. #define MOD_DATA_MSG "sub_msg"
  210. static const pj_str_t str_event_name = { "Event", 5 };
  211. /*! \brief Scheduler used for automatically expiring publications */
  212. static struct ast_sched_context *sched;
  213. /*! \brief Number of buckets for publications (on a per handler) */
  214. #define PUBLICATIONS_BUCKETS 37
  215. /*! \brief Default expiration time for PUBLISH if one is not specified */
  216. #define DEFAULT_PUBLISH_EXPIRES 3600
  217. /*! \brief Number of buckets for subscription datastore */
  218. #define DATASTORE_BUCKETS 53
  219. /*! \brief Default expiration for subscriptions */
  220. #define DEFAULT_EXPIRES 3600
  221. /*! \brief Defined method for PUBLISH */
  222. const pjsip_method pjsip_publish_method =
  223. {
  224. PJSIP_OTHER_METHOD,
  225. { "PUBLISH", 7 }
  226. };
  227. /*!
  228. * \brief The types of PUBLISH messages defined in RFC 3903
  229. */
  230. enum sip_publish_type {
  231. /*!
  232. * \brief Unknown
  233. *
  234. * \details
  235. * This actually is not defined in RFC 3903. We use this as a constant
  236. * to indicate that an incoming PUBLISH does not fit into any of the
  237. * other categories and is thus invalid.
  238. */
  239. SIP_PUBLISH_UNKNOWN,
  240. /*!
  241. * \brief Initial
  242. *
  243. * \details
  244. * The first PUBLISH sent. This will contain a non-zero Expires header
  245. * as well as a body that indicates the current state of the endpoint
  246. * that has sent the message. The initial PUBLISH is the only type
  247. * of PUBLISH to not contain a Sip-If-Match header in it.
  248. */
  249. SIP_PUBLISH_INITIAL,
  250. /*!
  251. * \brief Refresh
  252. *
  253. * \details
  254. * Used to keep a published state from expiring. This will contain a
  255. * non-zero Expires header but no body since its purpose is not to
  256. * update state.
  257. */
  258. SIP_PUBLISH_REFRESH,
  259. /*!
  260. * \brief Modify
  261. *
  262. * \details
  263. * Used to change state from its previous value. This will contain
  264. * a body updating the published state. May or may not contain an
  265. * Expires header.
  266. */
  267. SIP_PUBLISH_MODIFY,
  268. /*!
  269. * \brief Remove
  270. *
  271. * \details
  272. * Used to remove published state from an ESC. This will contain
  273. * an Expires header set to 0 and likely no body.
  274. */
  275. SIP_PUBLISH_REMOVE,
  276. };
  277. /*!
  278. * \brief A vector of strings commonly used throughout this module
  279. */
  280. AST_VECTOR(resources, const char *);
  281. /*!
  282. * \brief Resource list configuration item
  283. */
  284. struct resource_list {
  285. SORCERY_OBJECT(details);
  286. /*! SIP event package the list uses. */
  287. char event[32];
  288. /*! Strings representing resources in the list. */
  289. struct resources items;
  290. /*! Indicates if Asterisk sends full or partial state on notifications. */
  291. unsigned int full_state;
  292. /*! Time, in milliseconds Asterisk waits before sending a batched notification.*/
  293. unsigned int notification_batch_interval;
  294. };
  295. /*!
  296. * Used to create new entity IDs by ESCs.
  297. */
  298. static int esc_etag_counter;
  299. /*!
  300. * \brief Structure representing a SIP publication
  301. */
  302. struct ast_sip_publication {
  303. /*! Publication datastores set up by handlers */
  304. struct ao2_container *datastores;
  305. /*! \brief Entity tag for the publication */
  306. int entity_tag;
  307. /*! \brief Handler for this publication */
  308. struct ast_sip_publish_handler *handler;
  309. /*! \brief The endpoint with which the subscription is communicating */
  310. struct ast_sip_endpoint *endpoint;
  311. /*! \brief Expiration time of the publication */
  312. int expires;
  313. /*! \brief Scheduled item for expiration of publication */
  314. int sched_id;
  315. /*! \brief The resource the publication is to */
  316. char *resource;
  317. /*! \brief The name of the event type configuration */
  318. char *event_configuration_name;
  319. /*! \brief Data containing the above */
  320. char data[0];
  321. };
  322. /*!
  323. * \brief Structure used for persisting an inbound subscription
  324. */
  325. struct subscription_persistence {
  326. /*! Sorcery object details */
  327. SORCERY_OBJECT(details);
  328. /*! The name of the endpoint involved in the subscrption */
  329. char *endpoint;
  330. /*! SIP message that creates the subscription */
  331. char packet[PJSIP_MAX_PKT_LEN];
  332. /*! Source address of the message */
  333. char src_name[PJ_INET6_ADDRSTRLEN];
  334. /*! Source port of the message */
  335. int src_port;
  336. /*! Local transport key type */
  337. char transport_key[32];
  338. /*! Local transport address */
  339. char local_name[PJ_INET6_ADDRSTRLEN];
  340. /*! Local transport port */
  341. int local_port;
  342. /*! Next CSeq to use for message */
  343. unsigned int cseq;
  344. /*! Local tag of the dialog */
  345. char *tag;
  346. /*! When this subscription expires */
  347. struct timeval expires;
  348. };
  349. /*!
  350. * \brief A tree of SIP subscriptions
  351. *
  352. * Because of the ability to subscribe to resource lists, a SIP
  353. * subscription can result in a tree of subscriptions being created.
  354. * This structure represents the information relevant to the subscription
  355. * as a whole, to include the underlying PJSIP structure for the
  356. * subscription.
  357. */
  358. struct sip_subscription_tree {
  359. /*! The endpoint with which the subscription is communicating */
  360. struct ast_sip_endpoint *endpoint;
  361. /*! Serializer on which to place operations for this subscription */
  362. struct ast_taskprocessor *serializer;
  363. /*! The role for this subscription */
  364. enum ast_sip_subscription_role role;
  365. /*! Persistence information */
  366. struct subscription_persistence *persistence;
  367. /*! The underlying PJSIP event subscription structure */
  368. pjsip_evsub *evsub;
  369. /*! The underlying PJSIP dialog */
  370. pjsip_dialog *dlg;
  371. /*! Interval to use for batching notifications */
  372. unsigned int notification_batch_interval;
  373. /*! Scheduler ID for batched notification */
  374. int notify_sched_id;
  375. /*! Indicator if scheduled batched notification should be sent */
  376. unsigned int send_scheduled_notify;
  377. /*! The root of the subscription tree */
  378. struct ast_sip_subscription *root;
  379. /*! Is this subscription to a list? */
  380. int is_list;
  381. /*! Next item in the list */
  382. AST_LIST_ENTRY(sip_subscription_tree) next;
  383. };
  384. /*!
  385. * \brief Structure representing a "virtual" SIP subscription.
  386. *
  387. * This structure serves a dual purpose. Structurally, it is
  388. * the constructed tree of subscriptions based on the resources
  389. * being subscribed to. API-wise, this serves as the handle that
  390. * subscription handlers use in order to interact with the pubsub API.
  391. */
  392. struct ast_sip_subscription {
  393. /*! Subscription datastores set up by handlers */
  394. struct ao2_container *datastores;
  395. /*! The handler for this subscription */
  396. const struct ast_sip_subscription_handler *handler;
  397. /*! Pointer to the base of the tree */
  398. struct sip_subscription_tree *tree;
  399. /*! Body generaator for NOTIFYs */
  400. struct ast_sip_pubsub_body_generator *body_generator;
  401. /*! Vector of child subscriptions */
  402. AST_VECTOR(, struct ast_sip_subscription *) children;
  403. /*! Saved NOTIFY body text for this subscription */
  404. struct ast_str *body_text;
  405. /*! Indicator that the body text has changed since the last notification */
  406. int body_changed;
  407. /*! The current state of the subscription */
  408. pjsip_evsub_state subscription_state;
  409. /*! For lists, the current version to place in the RLMI body */
  410. unsigned int version;
  411. /*! For lists, indicates if full state should always be communicated. */
  412. unsigned int full_state;
  413. /*! URI associated with the subscription */
  414. pjsip_sip_uri *uri;
  415. /*! Name of resource being subscribed to */
  416. char resource[0];
  417. };
  418. /*!
  419. * \brief Structure representing a publication resource
  420. */
  421. struct ast_sip_publication_resource {
  422. /*! \brief Sorcery object details */
  423. SORCERY_OBJECT(details);
  424. /*! \brief Optional name of an endpoint that is only allowed to publish to this resource */
  425. char *endpoint;
  426. /*! \brief Mapping for event types to configuration */
  427. struct ast_variable *events;
  428. };
  429. static const char *sip_subscription_roles_map[] = {
  430. [AST_SIP_SUBSCRIBER] = "Subscriber",
  431. [AST_SIP_NOTIFIER] = "Notifier"
  432. };
  433. AST_RWLIST_HEAD_STATIC(subscriptions, sip_subscription_tree);
  434. AST_RWLIST_HEAD_STATIC(body_generators, ast_sip_pubsub_body_generator);
  435. AST_RWLIST_HEAD_STATIC(body_supplements, ast_sip_pubsub_body_supplement);
  436. static void pubsub_on_evsub_state(pjsip_evsub *sub, pjsip_event *event);
  437. static void pubsub_on_rx_refresh(pjsip_evsub *sub, pjsip_rx_data *rdata,
  438. int *p_st_code, pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body);
  439. static void pubsub_on_rx_notify(pjsip_evsub *sub, pjsip_rx_data *rdata, int *p_st_code,
  440. pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body);
  441. static void pubsub_on_client_refresh(pjsip_evsub *sub);
  442. static void pubsub_on_server_timeout(pjsip_evsub *sub);
  443. static pjsip_evsub_user pubsub_cb = {
  444. .on_evsub_state = pubsub_on_evsub_state,
  445. .on_rx_refresh = pubsub_on_rx_refresh,
  446. .on_rx_notify = pubsub_on_rx_notify,
  447. .on_client_refresh = pubsub_on_client_refresh,
  448. .on_server_timeout = pubsub_on_server_timeout,
  449. };
  450. /*! \brief Destructor for publication resource */
  451. static void publication_resource_destroy(void *obj)
  452. {
  453. struct ast_sip_publication_resource *resource = obj;
  454. ast_free(resource->endpoint);
  455. ast_variables_destroy(resource->events);
  456. }
  457. /*! \brief Allocator for publication resource */
  458. static void *publication_resource_alloc(const char *name)
  459. {
  460. return ast_sorcery_generic_alloc(sizeof(struct ast_sip_publication_resource), publication_resource_destroy);
  461. }
  462. /*! \brief Destructor for subscription persistence */
  463. static void subscription_persistence_destroy(void *obj)
  464. {
  465. struct subscription_persistence *persistence = obj;
  466. ast_free(persistence->endpoint);
  467. ast_free(persistence->tag);
  468. }
  469. /*! \brief Allocator for subscription persistence */
  470. static void *subscription_persistence_alloc(const char *name)
  471. {
  472. return ast_sorcery_generic_alloc(sizeof(struct subscription_persistence), subscription_persistence_destroy);
  473. }
  474. /*! \brief Function which creates initial persistence information of a subscription in sorcery */
  475. static struct subscription_persistence *subscription_persistence_create(struct sip_subscription_tree *sub_tree)
  476. {
  477. char tag[PJ_GUID_STRING_LENGTH + 1];
  478. /* The id of this persistence object doesn't matter as we keep it on the subscription and don't need to
  479. * look it up by id at all.
  480. */
  481. struct subscription_persistence *persistence = ast_sorcery_alloc(ast_sip_get_sorcery(),
  482. "subscription_persistence", NULL);
  483. pjsip_dialog *dlg = sub_tree->dlg;
  484. if (!persistence) {
  485. return NULL;
  486. }
  487. persistence->endpoint = ast_strdup(ast_sorcery_object_get_id(sub_tree->endpoint));
  488. ast_copy_pj_str(tag, &dlg->local.info->tag, sizeof(tag));
  489. persistence->tag = ast_strdup(tag);
  490. ast_sorcery_create(ast_sip_get_sorcery(), persistence);
  491. return persistence;
  492. }
  493. /*! \brief Function which updates persistence information of a subscription in sorcery */
  494. static void subscription_persistence_update(struct sip_subscription_tree *sub_tree,
  495. pjsip_rx_data *rdata)
  496. {
  497. pjsip_dialog *dlg;
  498. if (!sub_tree->persistence) {
  499. return;
  500. }
  501. dlg = sub_tree->dlg;
  502. sub_tree->persistence->cseq = dlg->local.cseq;
  503. if (rdata) {
  504. int expires;
  505. pjsip_expires_hdr *expires_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL);
  506. expires = expires_hdr ? expires_hdr->ivalue : DEFAULT_PUBLISH_EXPIRES;
  507. sub_tree->persistence->expires = ast_tvadd(ast_tvnow(), ast_samp2tv(expires, 1));
  508. ast_copy_string(sub_tree->persistence->packet, rdata->pkt_info.packet,
  509. sizeof(sub_tree->persistence->packet));
  510. ast_copy_string(sub_tree->persistence->src_name, rdata->pkt_info.src_name,
  511. sizeof(sub_tree->persistence->src_name));
  512. sub_tree->persistence->src_port = rdata->pkt_info.src_port;
  513. ast_copy_string(sub_tree->persistence->transport_key, rdata->tp_info.transport->type_name,
  514. sizeof(sub_tree->persistence->transport_key));
  515. ast_copy_pj_str(sub_tree->persistence->local_name, &rdata->tp_info.transport->local_name.host,
  516. sizeof(sub_tree->persistence->local_name));
  517. sub_tree->persistence->local_port = rdata->tp_info.transport->local_name.port;
  518. }
  519. ast_sorcery_update(ast_sip_get_sorcery(), sub_tree->persistence);
  520. }
  521. /*! \brief Function which removes persistence of a subscription from sorcery */
  522. static void subscription_persistence_remove(struct sip_subscription_tree *sub_tree)
  523. {
  524. if (!sub_tree->persistence) {
  525. return;
  526. }
  527. ast_sorcery_delete(ast_sip_get_sorcery(), sub_tree->persistence);
  528. ao2_ref(sub_tree->persistence, -1);
  529. }
  530. static struct ast_sip_subscription_handler *find_sub_handler_for_event_name(const char *event_name);
  531. static struct ast_sip_pubsub_body_generator *find_body_generator(char accept[AST_SIP_MAX_ACCEPT][64],
  532. size_t num_accept, const char *body_type);
  533. /*! \brief Retrieve a handler using the Event header of an rdata message */
  534. static struct ast_sip_subscription_handler *subscription_get_handler_from_rdata(pjsip_rx_data *rdata)
  535. {
  536. pjsip_event_hdr *event_header;
  537. char event[32];
  538. struct ast_sip_subscription_handler *handler;
  539. event_header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_event_name, rdata->msg_info.msg->hdr.next);
  540. if (!event_header) {
  541. ast_log(LOG_WARNING, "Incoming SUBSCRIBE request with no Event header\n");
  542. return NULL;
  543. }
  544. ast_copy_pj_str(event, &event_header->event_type, sizeof(event));
  545. handler = find_sub_handler_for_event_name(event);
  546. if (!handler) {
  547. ast_log(LOG_WARNING, "No registered subscribe handler for event %s\n", event);
  548. }
  549. return handler;
  550. }
  551. /*!
  552. * \brief Accept headers that are exceptions to the rule
  553. *
  554. * Typically, when a SUBSCRIBE arrives, we attempt to find a
  555. * body generator that matches one of the Accept headers in
  556. * the request. When subscribing to a single resource, this works
  557. * great. However, when subscribing to a list, things work
  558. * differently. Most Accept header values are fine, but there
  559. * are a couple that are endemic to resource lists that need
  560. * to be ignored when searching for a body generator to use
  561. * for the individual resources of the subscription.
  562. */
  563. const char *accept_exceptions[] = {
  564. "multipart/related",
  565. "application/rlmi+xml",
  566. };
  567. /*!
  568. * \brief Is the Accept header from the SUBSCRIBE in the list of exceptions?
  569. *
  570. * \retval 1 This Accept header value is an exception to the rule.
  571. * \retval 0 This Accept header is not an exception to the rule.
  572. */
  573. static int exceptional_accept(const pj_str_t *accept)
  574. {
  575. int i;
  576. for (i = 0; i < ARRAY_LEN(accept_exceptions); ++i) {
  577. if (!pj_strcmp2(accept, accept_exceptions[i])) {
  578. return 1;
  579. }
  580. }
  581. return 0;
  582. }
  583. /*! \brief Retrieve a body generator using the Accept header of an rdata message */
  584. static struct ast_sip_pubsub_body_generator *subscription_get_generator_from_rdata(pjsip_rx_data *rdata,
  585. const struct ast_sip_subscription_handler *handler)
  586. {
  587. pjsip_accept_hdr *accept_header = (pjsip_accept_hdr *) &rdata->msg_info.msg->hdr;
  588. char accept[AST_SIP_MAX_ACCEPT][64];
  589. size_t num_accept_headers = 0;
  590. while ((accept_header = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_ACCEPT, accept_header->next))) {
  591. int i;
  592. for (i = 0; i < accept_header->count; ++i) {
  593. if (!exceptional_accept(&accept_header->values[i])) {
  594. ast_copy_pj_str(accept[num_accept_headers], &accept_header->values[i], sizeof(accept[num_accept_headers]));
  595. ++num_accept_headers;
  596. }
  597. }
  598. }
  599. if (num_accept_headers == 0) {
  600. /* If a SUBSCRIBE contains no Accept headers, then we must assume that
  601. * the default accept type for the event package is to be used.
  602. */
  603. ast_copy_string(accept[0], handler->notifier->default_accept, sizeof(accept[0]));
  604. num_accept_headers = 1;
  605. }
  606. return find_body_generator(accept, num_accept_headers, handler->body_type);
  607. }
  608. /*! \brief Check if the rdata has a Supported header containing 'eventlist'
  609. *
  610. * \retval 1 rdata has an eventlist containing supported header
  611. * \retval 0 rdata doesn't have an eventlist containing supported header
  612. */
  613. static int ast_sip_pubsub_has_eventlist_support(pjsip_rx_data *rdata)
  614. {
  615. pjsip_supported_hdr *supported_header = (pjsip_supported_hdr *) &rdata->msg_info.msg->hdr;
  616. while ((supported_header = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_SUPPORTED, supported_header->next))) {
  617. int i;
  618. for (i = 0; i < supported_header->count; i++) {
  619. if (!pj_stricmp2(&supported_header->values[i], "eventlist")) {
  620. return 1;
  621. }
  622. }
  623. }
  624. return 0;
  625. }
  626. struct resource_tree;
  627. /*!
  628. * \brief A node for a resource tree.
  629. */
  630. struct tree_node {
  631. AST_VECTOR(, struct tree_node *) children;
  632. unsigned int full_state;
  633. char resource[0];
  634. };
  635. /*!
  636. * \brief Helper function for retrieving a resource list for a given event.
  637. *
  638. * This will retrieve a resource list that corresponds to the resource and event provided.
  639. *
  640. * \param resource The name of the resource list to retrieve
  641. * \param event The expected event name on the resource list
  642. */
  643. static struct resource_list *retrieve_resource_list(const char *resource, const char *event)
  644. {
  645. struct resource_list *list;
  646. list = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "resource_list", resource);
  647. if (!list) {
  648. return NULL;
  649. }
  650. if (strcmp(list->event, event)) {
  651. ast_log(LOG_WARNING, "Found resource list %s, but its event type (%s) does not match SUBSCRIBE's (%s)\n",
  652. resource, list->event, event);
  653. ao2_cleanup(list);
  654. return NULL;
  655. }
  656. return list;
  657. }
  658. /*!
  659. * \brief Allocate a tree node
  660. *
  661. * In addition to allocating and initializing the tree node, the node is also added
  662. * to the vector of visited resources. See \ref build_resource_tree for more information
  663. * on the visited resources.
  664. *
  665. * \param resource The name of the resource for this tree node.
  666. * \param visited The vector of resources that have been visited.
  667. * \param if allocating a list, indicate whether full state is requested in notifications.
  668. * \retval NULL Allocation failure.
  669. * \retval non-NULL The newly-allocated tree_node
  670. */
  671. static struct tree_node *tree_node_alloc(const char *resource, struct resources *visited, unsigned int full_state)
  672. {
  673. struct tree_node *node;
  674. node = ast_calloc(1, sizeof(*node) + strlen(resource) + 1);
  675. if (!node) {
  676. return NULL;
  677. }
  678. strcpy(node->resource, resource);
  679. if (AST_VECTOR_INIT(&node->children, 4)) {
  680. ast_free(node);
  681. return NULL;
  682. }
  683. node->full_state = full_state;
  684. if (visited) {
  685. AST_VECTOR_APPEND(visited, resource);
  686. }
  687. return node;
  688. }
  689. /*!
  690. * \brief Destructor for a tree node
  691. *
  692. * This function calls recursively in order to destroy
  693. * all nodes lower in the tree from the given node in
  694. * addition to the node itself.
  695. *
  696. * \param node The node to destroy.
  697. */
  698. static void tree_node_destroy(struct tree_node *node)
  699. {
  700. int i;
  701. if (!node) {
  702. return;
  703. }
  704. for (i = 0; i < AST_VECTOR_SIZE(&node->children); ++i) {
  705. tree_node_destroy(AST_VECTOR_GET(&node->children, i));
  706. }
  707. AST_VECTOR_FREE(&node->children);
  708. ast_free(node);
  709. }
  710. /*!
  711. * \brief Determine if this resource has been visited already
  712. *
  713. * See \ref build_resource_tree for more information
  714. *
  715. * \param resource The resource currently being visited
  716. * \param visited The resources that have previously been visited
  717. */
  718. static int have_visited(const char *resource, struct resources *visited)
  719. {
  720. int i;
  721. for (i = 0; i < AST_VECTOR_SIZE(visited); ++i) {
  722. if (!strcmp(resource, AST_VECTOR_GET(visited, i))) {
  723. return 1;
  724. }
  725. }
  726. return 0;
  727. }
  728. /*!
  729. * \brief Build child nodes for a given parent.
  730. *
  731. * This iterates through the items on a resource list and creates tree nodes for each one. The
  732. * tree nodes created are children of the supplied parent node. If an item in the resource
  733. * list is itself a list, then this function is called recursively to provide children for
  734. * the the new node.
  735. *
  736. * If an item in a resource list is not a list, then the supplied subscription handler is
  737. * called into as if a new SUBSCRIBE for the list item were presented. The handler's response
  738. * is used to determine if the node can be added to the tree or not.
  739. *
  740. * If a parent node ends up having no child nodes added under it, then the parent node is
  741. * pruned from the tree.
  742. *
  743. * \param endpoint The endpoint that sent the inbound SUBSCRIBE.
  744. * \param handler The subscription handler for leaf nodes in the tree.
  745. * \param list The configured resource list from which the child node is being built.
  746. * \param parent The parent node for these children.
  747. * \param visited The resources that have already been visited.
  748. */
  749. static void build_node_children(struct ast_sip_endpoint *endpoint, const struct ast_sip_subscription_handler *handler,
  750. struct resource_list *list, struct tree_node *parent, struct resources *visited)
  751. {
  752. int i;
  753. for (i = 0; i < AST_VECTOR_SIZE(&list->items); ++i) {
  754. struct tree_node *current;
  755. struct resource_list *child_list;
  756. const char *resource = AST_VECTOR_GET(&list->items, i);
  757. if (have_visited(resource, visited)) {
  758. ast_debug(1, "Already visited resource %s. Avoiding duplicate resource or potential loop.\n", resource);
  759. continue;
  760. }
  761. child_list = retrieve_resource_list(resource, list->event);
  762. if (!child_list) {
  763. int resp = handler->notifier->new_subscribe(endpoint, resource);
  764. if (PJSIP_IS_STATUS_IN_CLASS(resp, 200)) {
  765. current = tree_node_alloc(resource, visited, 0);
  766. if (!current) {
  767. ast_debug(1, "Subscription to leaf resource %s was successful, but encountered"
  768. "allocation error afterwards\n", resource);
  769. continue;
  770. }
  771. ast_debug(1, "Subscription to leaf resource %s resulted in success. Adding to parent %s\n",
  772. resource, parent->resource);
  773. AST_VECTOR_APPEND(&parent->children, current);
  774. } else {
  775. ast_debug(1, "Subscription to leaf resource %s resulted in error response %d\n",
  776. resource, resp);
  777. }
  778. } else {
  779. ast_debug(1, "Resource %s (child of %s) is a list\n", resource, parent->resource);
  780. current = tree_node_alloc(resource, visited, child_list->full_state);
  781. if (!current) {
  782. ast_debug(1, "Cannot build children of resource %s due to allocation failure\n", resource);
  783. continue;
  784. }
  785. build_node_children(endpoint, handler, child_list, current, visited);
  786. if (AST_VECTOR_SIZE(&current->children) > 0) {
  787. ast_debug(1, "List %s had no successful children.\n", resource);
  788. AST_VECTOR_APPEND(&parent->children, current);
  789. } else {
  790. ast_debug(1, "List %s had successful children. Adding to parent %s\n",
  791. resource, parent->resource);
  792. tree_node_destroy(current);
  793. }
  794. ao2_cleanup(child_list);
  795. }
  796. }
  797. }
  798. /*!
  799. * \brief A resource tree
  800. *
  801. * When an inbound SUBSCRIBE arrives, the resource being subscribed to may
  802. * be a resource list. If this is the case, the resource list may contain resources
  803. * that are themselves lists. The structure needed to hold the resources is
  804. * a tree.
  805. *
  806. * Upon receipt of the SUBSCRIBE, the tree is built by determining if subscriptions
  807. * to the individual resources in the tree would be successful or not. Any successful
  808. * subscriptions result in a node in the tree being created. Any unsuccessful subscriptions
  809. * result in no node being created.
  810. *
  811. * This tree can be seen as a bare-bones analog of the tree of ast_sip_subscriptions that
  812. * will end up being created to actually carry out the duties of a SIP SUBSCRIBE dialog.
  813. */
  814. struct resource_tree {
  815. struct tree_node *root;
  816. unsigned int notification_batch_interval;
  817. };
  818. /*!
  819. * \brief Destroy a resource tree.
  820. *
  821. * This function makes no assumptions about how the tree itself was
  822. * allocated and does not attempt to free the tree itself. Callers
  823. * of this function are responsible for freeing the tree.
  824. *
  825. * \param tree The tree to destroy.
  826. */
  827. static void resource_tree_destroy(struct resource_tree *tree)
  828. {
  829. if (tree) {
  830. tree_node_destroy(tree->root);
  831. }
  832. }
  833. /*!
  834. * \brief Build a resource tree
  835. *
  836. * This function builds a resource tree based on the requested resource in a SUBSCRIBE request.
  837. *
  838. * This function also creates a container that has all resources that have been visited during
  839. * creation of the tree, whether those resources resulted in a tree node being created or not.
  840. * Keeping this container of visited resources allows for misconfigurations such as loops in
  841. * the tree or duplicated resources to be detected.
  842. *
  843. * \param endpoint The endpoint that sent the SUBSCRIBE request.
  844. * \param handler The subscription handler for leaf nodes in the tree.
  845. * \param resource The resource requested in the SUBSCRIBE request.
  846. * \param tree The tree that is to be built.
  847. * \param has_eventlist_support
  848. *
  849. * \retval 200-299 Successfully subscribed to at least one resource.
  850. * \retval 300-699 Failure to subscribe to requested resource.
  851. */
  852. static int build_resource_tree(struct ast_sip_endpoint *endpoint, const struct ast_sip_subscription_handler *handler,
  853. const char *resource, struct resource_tree *tree, int has_eventlist_support)
  854. {
  855. struct resource_list *list;
  856. struct resources visited;
  857. if (!has_eventlist_support || !(list = retrieve_resource_list(resource, handler->event_name))) {
  858. ast_debug(1, "Subscription to resource %s is not to a list\n", resource);
  859. tree->root = tree_node_alloc(resource, NULL, 0);
  860. if (!tree->root) {
  861. return 500;
  862. }
  863. return handler->notifier->new_subscribe(endpoint, resource);
  864. }
  865. ast_debug(1, "Subscription to resource %s is a list\n", resource);
  866. if (AST_VECTOR_INIT(&visited, AST_VECTOR_SIZE(&list->items))) {
  867. return 500;
  868. }
  869. tree->root = tree_node_alloc(resource, &visited, list->full_state);
  870. if (!tree->root) {
  871. return 500;
  872. }
  873. tree->notification_batch_interval = list->notification_batch_interval;
  874. build_node_children(endpoint, handler, list, tree->root, &visited);
  875. AST_VECTOR_FREE(&visited);
  876. ao2_cleanup(list);
  877. if (AST_VECTOR_SIZE(&tree->root->children) > 0) {
  878. return 200;
  879. } else {
  880. return 500;
  881. }
  882. }
  883. static int datastore_hash(const void *obj, int flags)
  884. {
  885. const struct ast_datastore *datastore = obj;
  886. const char *uid = flags & OBJ_KEY ? obj : datastore->uid;
  887. ast_assert(uid != NULL);
  888. return ast_str_hash(uid);
  889. }
  890. static int datastore_cmp(void *obj, void *arg, int flags)
  891. {
  892. const struct ast_datastore *datastore1 = obj;
  893. const struct ast_datastore *datastore2 = arg;
  894. const char *uid2 = flags & OBJ_KEY ? arg : datastore2->uid;
  895. ast_assert(datastore1->uid != NULL);
  896. ast_assert(uid2 != NULL);
  897. return strcmp(datastore1->uid, uid2) ? 0 : CMP_MATCH | CMP_STOP;
  898. }
  899. static int subscription_remove_serializer(void *obj)
  900. {
  901. struct sip_subscription_tree *sub_tree = obj;
  902. /* This is why we keep the dialog on the subscription. When the subscription
  903. * is destroyed, there is no guarantee that the underlying dialog is ready
  904. * to be destroyed. Furthermore, there's no guarantee in the opposite direction
  905. * either. The dialog could be destroyed before our subscription is. We fix
  906. * this problem by keeping a reference to the dialog until it is time to
  907. * destroy the subscription. We need to have the dialog available when the
  908. * subscription is destroyed so that we can guarantee that our attempt to
  909. * remove the serializer will be successful.
  910. */
  911. ast_sip_dialog_set_serializer(sub_tree->dlg, NULL);
  912. pjsip_dlg_dec_session(sub_tree->dlg, &pubsub_module);
  913. return 0;
  914. }
  915. static void add_subscription(struct sip_subscription_tree *obj)
  916. {
  917. SCOPED_LOCK(lock, &subscriptions, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
  918. AST_RWLIST_INSERT_TAIL(&subscriptions, obj, next);
  919. }
  920. static void remove_subscription(struct sip_subscription_tree *obj)
  921. {
  922. struct sip_subscription_tree *i;
  923. SCOPED_LOCK(lock, &subscriptions, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
  924. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&subscriptions, i, next) {
  925. if (i == obj) {
  926. AST_RWLIST_REMOVE_CURRENT(next);
  927. if (i->root) {
  928. ast_debug(1, "Removing subscription to resource %s from list of subscriptions\n",
  929. ast_sip_subscription_get_resource_name(i->root));
  930. }
  931. break;
  932. }
  933. }
  934. AST_RWLIST_TRAVERSE_SAFE_END;
  935. }
  936. static void subscription_destructor(void *obj)
  937. {
  938. struct ast_sip_subscription *sub = obj;
  939. ast_debug(3, "Destroying SIP subscription to resource %s\n", sub->resource);
  940. ast_free(sub->body_text);
  941. ao2_cleanup(sub->datastores);
  942. }
  943. static struct ast_sip_subscription *allocate_subscription(const struct ast_sip_subscription_handler *handler,
  944. const char *resource, struct sip_subscription_tree *tree)
  945. {
  946. struct ast_sip_subscription *sub;
  947. pjsip_sip_uri *contact_uri;
  948. sub = ao2_alloc(sizeof(*sub) + strlen(resource) + 1, subscription_destructor);
  949. if (!sub) {
  950. return NULL;
  951. }
  952. strcpy(sub->resource, resource); /* Safe */
  953. sub->datastores = ao2_container_alloc(DATASTORE_BUCKETS, datastore_hash, datastore_cmp);
  954. if (!sub->datastores) {
  955. ao2_ref(sub, -1);
  956. return NULL;
  957. }
  958. sub->body_text = ast_str_create(128);
  959. if (!sub->body_text) {
  960. ao2_ref(sub, -1);
  961. return NULL;
  962. }
  963. sub->uri = pjsip_sip_uri_create(tree->dlg->pool, PJ_FALSE);
  964. contact_uri = pjsip_uri_get_uri(tree->dlg->local.contact->uri);
  965. pjsip_sip_uri_assign(tree->dlg->pool, sub->uri, contact_uri);
  966. pj_strdup2(tree->dlg->pool, &sub->uri->user, resource);
  967. sub->handler = handler;
  968. sub->subscription_state = PJSIP_EVSUB_STATE_ACTIVE;
  969. sub->tree = tree;
  970. return sub;
  971. }
  972. /*!
  973. * \brief Create a tree of virtual subscriptions based on a resource tree node.
  974. *
  975. * \param handler The handler to supply to leaf subscriptions.
  976. * \param resource The requested resource for this subscription.
  977. * \param generator Body generator to use for leaf subscriptions.
  978. * \param tree The root of the subscription tree.
  979. * \param current The tree node that corresponds to the subscription being created.
  980. */
  981. static struct ast_sip_subscription *create_virtual_subscriptions(const struct ast_sip_subscription_handler *handler,
  982. const char *resource, struct ast_sip_pubsub_body_generator *generator,
  983. struct sip_subscription_tree *tree, struct tree_node *current)
  984. {
  985. int i;
  986. struct ast_sip_subscription *sub;
  987. sub = allocate_subscription(handler, resource, tree);
  988. if (!sub) {
  989. return NULL;
  990. }
  991. sub->full_state = current->full_state;
  992. sub->body_generator = generator;
  993. for (i = 0; i < AST_VECTOR_SIZE(&current->children); ++i) {
  994. struct ast_sip_subscription *child;
  995. struct tree_node *child_node = AST_VECTOR_GET(&current->children, i);
  996. child = create_virtual_subscriptions(handler, child_node->resource, generator,
  997. tree, child_node);
  998. if (!child) {
  999. ast_debug(1, "Child subscription to resource %s could not be created\n",
  1000. child_node->resource);
  1001. continue;
  1002. }
  1003. if (AST_VECTOR_APPEND(&sub->children, child)) {
  1004. ast_debug(1, "Child subscription to resource %s could not be appended\n",
  1005. child_node->resource);
  1006. }
  1007. }
  1008. return sub;
  1009. }
  1010. static void shutdown_subscriptions(struct ast_sip_subscription *sub)
  1011. {
  1012. int i;
  1013. if (!sub) {
  1014. return;
  1015. }
  1016. if (AST_VECTOR_SIZE(&sub->children) > 0) {
  1017. for (i = 0; i < AST_VECTOR_SIZE(&sub->children); ++i) {
  1018. shutdown_subscriptions(AST_VECTOR_GET(&sub->children, i));
  1019. ao2_cleanup(AST_VECTOR_GET(&sub->children, i));
  1020. }
  1021. return;
  1022. }
  1023. if (sub->handler->subscription_shutdown) {
  1024. sub->handler->subscription_shutdown(sub);
  1025. }
  1026. }
  1027. static void subscription_tree_destructor(void *obj)
  1028. {
  1029. struct sip_subscription_tree *sub_tree = obj;
  1030. remove_subscription(sub_tree);
  1031. subscription_persistence_remove(sub_tree);
  1032. ao2_cleanup(sub_tree->endpoint);
  1033. if (sub_tree->dlg) {
  1034. ast_sip_push_task_synchronous(NULL, subscription_remove_serializer, sub_tree);
  1035. }
  1036. shutdown_subscriptions(sub_tree->root);
  1037. ao2_cleanup(sub_tree->root);
  1038. ast_taskprocessor_unreference(sub_tree->serializer);
  1039. ast_module_unref(ast_module_info->self);
  1040. }
  1041. static void subscription_setup_dialog(struct sip_subscription_tree *sub_tree, pjsip_dialog *dlg)
  1042. {
  1043. /* We keep a reference to the dialog until our subscription is destroyed. See
  1044. * the subscription_destructor for more details
  1045. */
  1046. pjsip_dlg_inc_session(dlg, &pubsub_module);
  1047. sub_tree->dlg = dlg;
  1048. ast_sip_dialog_set_serializer(dlg, sub_tree->serializer);
  1049. pjsip_evsub_set_mod_data(sub_tree->evsub, pubsub_module.id, sub_tree);
  1050. }
  1051. static struct sip_subscription_tree *allocate_subscription_tree(struct ast_sip_endpoint *endpoint)
  1052. {
  1053. struct sip_subscription_tree *sub_tree;
  1054. sub_tree = ao2_alloc(sizeof *sub_tree, subscription_tree_destructor);
  1055. if (!sub_tree) {
  1056. return NULL;
  1057. }
  1058. ast_module_ref(ast_module_info->self);
  1059. sub_tree->serializer = ast_sip_create_serializer();
  1060. if (!sub_tree->serializer) {
  1061. ao2_ref(sub_tree, -1);
  1062. return NULL;
  1063. }
  1064. sub_tree->endpoint = ao2_bump(endpoint);
  1065. sub_tree->notify_sched_id = -1;
  1066. add_subscription(sub_tree);
  1067. return sub_tree;
  1068. }
  1069. /*!
  1070. * \brief Create a subscription tree based on a resource tree.
  1071. *
  1072. * Using the previously-determined valid resources in the provided resource tree,
  1073. * a corresponding tree of ast_sip_subscriptions are created. The root of the
  1074. * subscription tree is a real subscription, and the rest in the tree are
  1075. * virtual subscriptions.
  1076. *
  1077. * \param handler The handler to use for leaf subscriptions
  1078. * \param endpoint The endpoint that sent the SUBSCRIBE request
  1079. * \param rdata The SUBSCRIBE content
  1080. * \param resource The requested resource in the SUBSCRIBE request
  1081. * \param generator The body generator to use in leaf subscriptions
  1082. * \param tree The resource tree on which the subscription tree is based
  1083. *
  1084. * \retval NULL Could not create the subscription tree
  1085. * \retval non-NULL The root of the created subscription tree
  1086. */
  1087. static struct sip_subscription_tree *create_subscription_tree(const struct ast_sip_subscription_handler *handler,
  1088. struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, const char *resource,
  1089. struct ast_sip_pubsub_body_generator *generator, struct resource_tree *tree)
  1090. {
  1091. struct sip_subscription_tree *sub_tree;
  1092. pjsip_dialog *dlg;
  1093. struct subscription_persistence *persistence;
  1094. sub_tree = allocate_subscription_tree(endpoint);
  1095. if (!sub_tree) {
  1096. return NULL;
  1097. }
  1098. sub_tree->role = AST_SIP_NOTIFIER;
  1099. dlg = ast_sip_create_dialog_uas(endpoint, rdata);
  1100. if (!dlg) {
  1101. ast_log(LOG_WARNING, "Unable to create dialog for SIP subscription\n");
  1102. ao2_ref(sub_tree, -1);
  1103. return NULL;
  1104. }
  1105. persistence = ast_sip_mod_data_get(rdata->endpt_info.mod_data,
  1106. pubsub_module.id, MOD_DATA_PERSISTENCE);
  1107. if (persistence) {
  1108. /* Update the created dialog with the persisted information */
  1109. pjsip_ua_unregister_dlg(pjsip_ua_instance(), dlg);
  1110. pj_strdup2(dlg->pool, &dlg->local.info->tag, persistence->tag);
  1111. dlg->local.tag_hval = pj_hash_calc_tolower(0, NULL, &dlg->local.info->tag);
  1112. pjsip_ua_register_dlg(pjsip_ua_instance(), dlg);
  1113. dlg->local.cseq = persistence->cseq;
  1114. dlg->remote.cseq = persistence->cseq;
  1115. }
  1116. pjsip_evsub_create_uas(dlg, &pubsub_cb, rdata, 0, &sub_tree->evsub);
  1117. subscription_setup_dialog(sub_tree, dlg);
  1118. ast_sip_mod_data_set(dlg->pool, dlg->mod_data, pubsub_module.id, MOD_DATA_MSG,
  1119. pjsip_msg_clone(dlg->pool, rdata->msg_info.msg));
  1120. sub_tree->notification_batch_interval = tree->notification_batch_interval;
  1121. sub_tree->root = create_virtual_subscriptions(handler, resource, generator, sub_tree, tree->root);
  1122. if (AST_VECTOR_SIZE(&sub_tree->root->children) > 0) {
  1123. sub_tree->is_list = 1;
  1124. }
  1125. return sub_tree;
  1126. }
  1127. /*! \brief Callback function to perform the actual recreation of a subscription */
  1128. static int subscription_persistence_recreate(void *obj, void *arg, int flags)
  1129. {
  1130. struct subscription_persistence *persistence = obj;
  1131. pj_pool_t *pool = arg;
  1132. pjsip_rx_data rdata = { { 0, }, };
  1133. RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
  1134. struct sip_subscription_tree *sub_tree;
  1135. struct ast_sip_pubsub_body_generator *generator;
  1136. int resp;
  1137. char *resource;
  1138. size_t resource_size;
  1139. pjsip_sip_uri *request_uri;
  1140. struct resource_tree tree;
  1141. pjsip_expires_hdr *expires_header;
  1142. struct ast_sip_subscription_handler *handler;
  1143. /* If this subscription has already expired remove it */
  1144. if (ast_tvdiff_ms(persistence->expires, ast_tvnow()) <= 0) {
  1145. ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
  1146. return 0;
  1147. }
  1148. endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", persistence->endpoint);
  1149. if (!endpoint) {
  1150. ast_log(LOG_WARNING, "A subscription for '%s' could not be recreated as the endpoint was not found\n",
  1151. persistence->endpoint);
  1152. ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
  1153. return 0;
  1154. }
  1155. pj_pool_reset(pool);
  1156. rdata.tp_info.pool = pool;
  1157. if (ast_sip_create_rdata(&rdata, persistence->packet, persistence->src_name, persistence->src_port,
  1158. persistence->transport_key, persistence->local_name, persistence->local_port)) {
  1159. ast_log(LOG_WARNING, "A subscription for '%s' could not be recreated as the message could not be parsed\n",
  1160. persistence->endpoint);
  1161. ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
  1162. return 0;
  1163. }
  1164. request_uri = pjsip_uri_get_uri(rdata.msg_info.msg->line.req.uri);
  1165. resource_size = pj_strlen(&request_uri->user) + 1;
  1166. resource = alloca(resource_size);
  1167. ast_copy_pj_str(resource, &request_uri->user, resource_size);
  1168. /* Update the expiration header with the new expiration */
  1169. expires_header = pjsip_msg_find_hdr(rdata.msg_info.msg, PJSIP_H_EXPIRES, rdata.msg_info.msg->hdr.next);
  1170. if (!expires_header) {
  1171. expires_header = pjsip_expires_hdr_create(pool, 0);
  1172. if (!expires_header) {
  1173. ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
  1174. return 0;
  1175. }
  1176. pjsip_msg_add_hdr(rdata.msg_info.msg, (pjsip_hdr*)expires_header);
  1177. }
  1178. expires_header->ivalue = (ast_tvdiff_ms(persistence->expires, ast_tvnow()) / 1000);
  1179. handler = subscription_get_handler_from_rdata(&rdata);
  1180. if (!handler || !handler->notifier) {
  1181. ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
  1182. return 0;
  1183. }
  1184. generator = subscription_get_generator_from_rdata(&rdata, handler);
  1185. if (!generator) {
  1186. ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
  1187. return 0;
  1188. }
  1189. ast_sip_mod_data_set(rdata.tp_info.pool, rdata.endpt_info.mod_data,
  1190. pubsub_module.id, MOD_DATA_PERSISTENCE, persistence);
  1191. memset(&tree, 0, sizeof(tree));
  1192. resp = build_resource_tree(endpoint, handler, resource, &tree,
  1193. ast_sip_pubsub_has_eventlist_support(&rdata));
  1194. if (PJSIP_IS_STATUS_IN_CLASS(resp, 200)) {
  1195. sub_tree = create_subscription_tree(handler, endpoint, &rdata, resource, generator, &tree);
  1196. if (!sub_tree) {
  1197. ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
  1198. ast_log(LOG_WARNING, "Failed to re-create subscription for %s\n", persistence->endpoint);
  1199. return 0;
  1200. }
  1201. sub_tree->persistence = ao2_bump(persistence);
  1202. subscription_persistence_update(sub_tree, &rdata);
  1203. } else {
  1204. ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
  1205. }
  1206. resource_tree_destroy(&tree);
  1207. return 0;
  1208. }
  1209. /*! \brief Function which loads and recreates persisted subscriptions upon startup when the system is fully booted */
  1210. static int subscription_persistence_load(void *data)
  1211. {
  1212. struct ao2_container *persisted_subscriptions = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(),
  1213. "subscription_persistence", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
  1214. pj_pool_t *pool;
  1215. pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "rtd%p", PJSIP_POOL_RDATA_LEN,
  1216. PJSIP_POOL_RDATA_INC);
  1217. if (!pool) {
  1218. ast_log(LOG_WARNING, "Could not create a memory pool for recreating SIP subscriptions\n");
  1219. return 0;
  1220. }
  1221. ao2_callback(persisted_subscriptions, OBJ_NODATA, subscription_persistence_recreate, pool);
  1222. pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
  1223. ao2_ref(persisted_subscriptions, -1);
  1224. return 0;
  1225. }
  1226. /*! \brief Event callback which fires subscription persistence recreation when the system is fully booted */
  1227. static void subscription_persistence_event_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
  1228. {
  1229. struct ast_json_payload *payload;
  1230. const char *type;
  1231. if (stasis_message_type(message) != ast_manager_get_generic_type()) {
  1232. return;
  1233. }
  1234. payload = stasis_message_data(message);
  1235. type = ast_json_string_get(ast_json_object_get(payload->json, "type"));
  1236. /* This subscription only responds to the FullyBooted event so that all modules have been loaded when we
  1237. * recreate SIP subscriptions.
  1238. */
  1239. if (strcmp(type, "FullyBooted")) {
  1240. return;
  1241. }
  1242. /* This has to be here so the subscription is recreated when the body generator is available */
  1243. ast_sip_push_task(NULL, subscription_persistence_load, NULL);
  1244. /* Once the system is fully booted we don't care anymore */
  1245. stasis_unsubscribe(sub);
  1246. }
  1247. typedef int (*on_subscription_t)(struct sip_subscription_tree *sub, void *arg);
  1248. static int for_each_subscription(on_subscription_t on_subscription, void *arg)
  1249. {
  1250. int num = 0;
  1251. struct sip_subscription_tree *i;
  1252. SCOPED_LOCK(lock, &subscriptions, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
  1253. if (!on_subscription) {
  1254. return num;
  1255. }
  1256. AST_RWLIST_TRAVERSE(&subscriptions, i, next) {
  1257. if (on_subscription(i, arg)) {
  1258. break;
  1259. }
  1260. ++num;
  1261. }
  1262. return num;
  1263. }
  1264. static void sip_subscription_to_ami(struct sip_subscription_tree *sub_tree,
  1265. struct ast_str **buf)
  1266. {
  1267. char str[256];
  1268. struct ast_sip_endpoint_id_configuration *id = &sub_tree->endpoint->id;
  1269. ast_str_append(buf, 0, "Role: %s\r\n",
  1270. sip_subscription_roles_map[sub_tree->role]);
  1271. ast_str_append(buf, 0, "Endpoint: %s\r\n",
  1272. ast_sorcery_object_get_id(sub_tree->endpoint));
  1273. ast_copy_pj_str(str, &sub_tree->dlg->call_id->id, sizeof(str));
  1274. ast_str_append(buf, 0, "Callid: %s\r\n", str);
  1275. ast_str_append(buf, 0, "State: %s\r\n", pjsip_evsub_get_state_name(sub_tree->evsub));
  1276. ast_callerid_merge(str, sizeof(str),
  1277. S_COR(id->self.name.valid, id->self.name.str, NULL),
  1278. S_COR(id->self.number.valid, id->self.number.str, NULL),
  1279. "Unknown");
  1280. ast_str_append(buf, 0, "Callerid: %s\r\n", str);
  1281. /* XXX This needs to be done recursively for lists */
  1282. if (sub_tree->root->handler->to_ami) {
  1283. sub_tree->root->handler->to_ami(sub_tree->root, buf);
  1284. }
  1285. }
  1286. void *ast_sip_subscription_get_header(const struct ast_sip_subscription *sub, const char *header)
  1287. {
  1288. pjsip_dialog *dlg = sub->tree->dlg;
  1289. pjsip_msg *msg = ast_sip_mod_data_get(dlg->mod_data, pubsub_module.id, MOD_DATA_MSG);
  1290. pj_str_t name;
  1291. pj_cstr(&name, header);
  1292. return pjsip_msg_find_hdr_by_name(msg, &name, NULL);
  1293. }
  1294. struct ast_sip_subscription *ast_sip_create_subscription(const struct ast_sip_subscription_handler *handler,
  1295. struct ast_sip_endpoint *endpoint, const char *resource)
  1296. {
  1297. struct ast_sip_subscription *sub;
  1298. pjsip_dialog *dlg;
  1299. struct ast_sip_contact *contact;
  1300. pj_str_t event;
  1301. pjsip_tx_data *tdata;
  1302. pjsip_evsub *evsub;
  1303. struct sip_subscription_tree *sub_tree = NULL;
  1304. sub_tree = allocate_subscription_tree(endpoint);
  1305. if (!sub_tree) {
  1306. return NULL;
  1307. }
  1308. sub = allocate_subscription(handler, resource, sub_tree);
  1309. if (!sub) {
  1310. ao2_cleanup(sub_tree);
  1311. return NULL;
  1312. }
  1313. contact = ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors);
  1314. if (!contact || ast_strlen_zero(contact->uri)) {
  1315. ast_log(LOG_WARNING, "No contacts configured for endpoint %s. Unable to create SIP subsription\n",
  1316. ast_sorcery_object_get_id(endpoint));
  1317. ao2_ref(sub_tree, -1);
  1318. ao2_cleanup(contact);
  1319. return NULL;
  1320. }
  1321. dlg = ast_sip_create_dialog_uac(endpoint, contact->uri, NULL);
  1322. ao2_cleanup(contact);
  1323. if (!dlg) {
  1324. ast_log(LOG_WARNING, "Unable to create dialog for SIP subscription\n");
  1325. ao2_ref(sub_tree, -1);
  1326. return NULL;
  1327. }
  1328. pj_cstr(&event, handler->event_name);
  1329. pjsip_evsub_create_uac(dlg, &pubsub_cb, &event, 0, &sub_tree->evsub);
  1330. subscription_setup_dialog(sub_tree, dlg);
  1331. evsub = sub_tree->evsub;
  1332. if (pjsip_evsub_initiate(evsub, NULL, -1, &tdata) == PJ_SUCCESS) {
  1333. pjsip_evsub_send_request(evsub, tdata);
  1334. } else {
  1335. /* pjsip_evsub_terminate will result in pubsub_on_evsub_state,
  1336. * being called and terminating the subscription. Therefore, we don't
  1337. * need to decrease the reference count of sub here.
  1338. */
  1339. pjsip_evsub_terminate(evsub, PJ_TRUE);
  1340. ao2_ref(sub_tree, -1);
  1341. return NULL;
  1342. }
  1343. return sub;
  1344. }
  1345. struct ast_sip_endpoint *ast_sip_subscription_get_endpoint(struct ast_sip_subscription *sub)
  1346. {
  1347. ast_assert(sub->tree->endpoint != NULL);
  1348. return ao2_bump(sub->tree->endpoint);
  1349. }
  1350. struct ast_taskprocessor *ast_sip_subscription_get_serializer(struct ast_sip_subscription *sub)
  1351. {
  1352. ast_assert(sub->tree->serializer != NULL);
  1353. return sub->tree->serializer;
  1354. }
  1355. /*!
  1356. * \brief Pre-allocate a buffer for the transmission
  1357. *
  1358. * Typically, we let PJSIP do this step for us when we send a request. PJSIP's buffer
  1359. * allocation algorithm is to allocate a buffer of PJSIP_MAX_PKT_LEN bytes and attempt
  1360. * to write the packet to the allocated buffer. If the buffer is too small to hold the
  1361. * packet, then we get told the message is too long to be sent.
  1362. *
  1363. * When dealing with SIP NOTIFY, especially with RLS, it is possible to exceed
  1364. * PJSIP_MAX_PKT_LEN. Rather than accepting the limitation imposed on us by default,
  1365. * we instead take the strategy of pre-allocating the buffer, testing for ourselves
  1366. * if the message will fit, and resizing the buffer as required.
  1367. *
  1368. * RFC 3261 says that a SIP UDP request can be up to 65535 bytes long. We're capping
  1369. * it at 64000 for a couple of reasons:
  1370. * 1) Allocating more than 64K at a time is hard to justify
  1371. * 2) If the message goes through proxies, those proxies will want to add Via and
  1372. * Record-Route headers, making the message even larger. Giving some space for
  1373. * those headers is a nice thing to do.
  1374. *
  1375. * RFC 3261 does not place an upper limit on the size of TCP requests, but we are
  1376. * going to impose the same 64K limit as a memory savings.
  1377. *
  1378. * \param tdata The tdata onto which to allocate a buffer
  1379. * \retval 0 Success
  1380. * \retval -1 The message is too large
  1381. */
  1382. static int allocate_tdata_buffer(pjsip_tx_data *tdata)
  1383. {
  1384. int buf_size;
  1385. int size = -1;
  1386. char *buf;
  1387. for (buf_size = PJSIP_MAX_PKT_LEN; size == -1 && buf_size < 64000; buf_size *= 2) {
  1388. buf = pj_pool_alloc(tdata->pool, buf_size);
  1389. size = pjsip_msg_print(tdata->msg, buf, buf_size);
  1390. }
  1391. if (size == -1) {
  1392. return -1;
  1393. }
  1394. tdata->buf.start = buf;
  1395. tdata->buf.cur = tdata->buf.start;
  1396. tdata->buf.end = tdata->buf.start + buf_size;
  1397. return 0;
  1398. }
  1399. static int sip_subscription_send_request(struct sip_subscription_tree *sub_tree, pjsip_tx_data *tdata)
  1400. {
  1401. #ifdef TEST_FRAMEWORK
  1402. struct ast_sip_endpoint *endpoint = sub_tree->endpoint;
  1403. #endif
  1404. int res;
  1405. if (allocate_tdata_buffer(tdata)) {
  1406. ast_log(LOG_ERROR, "SIP request %s is too large to send.\n", tdata->info);
  1407. return -1;
  1408. }
  1409. res = pjsip_evsub_send_request(sub_tree->evsub, tdata) == PJ_SUCCESS ? 0 : -1;
  1410. subscription_persistence_update(sub_tree, NULL);
  1411. ast_test_suite_event_notify("SUBSCRIPTION_STATE_SET",
  1412. "StateText: %s\r\n"
  1413. "Endpoint: %s\r\n",
  1414. pjsip_evsub_get_state_name(sub_tree->evsub),
  1415. ast_sorcery_object_get_id(endpoint));
  1416. return res;
  1417. }
  1418. /*!
  1419. * \brief Add a resource XML element to an RLMI body
  1420. *
  1421. * Each resource element represents a subscribed resource in the list. This function currently
  1422. * will unconditionally add an instance element to each created resource element. Instance
  1423. * elements refer to later parts in the multipart body.
  1424. *
  1425. * \param pool PJLIB allocation pool
  1426. * \param cid Content-ID header of the resource
  1427. * \param resource_name Name of the resource
  1428. * \param resource_uri URI of the resource
  1429. * \param state State of the subscribed resource
  1430. */
  1431. static void add_rlmi_resource(pj_pool_t *pool, pj_xml_node *rlmi, const pjsip_generic_string_hdr *cid,
  1432. const char *resource_name, const pjsip_sip_uri *resource_uri, pjsip_evsub_state state)
  1433. {
  1434. static pj_str_t cid_name = { "cid", 3 };
  1435. pj_xml_node *resource;
  1436. pj_xml_node *name;
  1437. pj_xml_node *instance;
  1438. pj_xml_attr *cid_attr;
  1439. char id[6];
  1440. char uri[PJSIP_MAX_URL_SIZE];
  1441. /* This creates a string representing the Content-ID without the enclosing < > */
  1442. const pj_str_t cid_stripped = {
  1443. .ptr = cid->hvalue.ptr + 1,
  1444. .slen = cid->hvalue.slen - 2,
  1445. };
  1446. resource = ast_sip_presence_xml_create_node(pool, rlmi, "resource");
  1447. name = ast_sip_presence_xml_create_node(pool, resource, "name");
  1448. instance = ast_sip_presence_xml_create_node(pool, resource, "instance");
  1449. pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, resource_uri, uri, sizeof(uri));
  1450. ast_sip_presence_xml_create_attr(pool, resource, "uri", uri);
  1451. pj_strdup2(pool, &name->content, resource_name);
  1452. ast_generate_random_string(id, sizeof(id));
  1453. ast_sip_presence_xml_create_attr(pool, instance, "id", id);
  1454. ast_sip_presence_xml_create_attr(pool, instance, "state",
  1455. state == PJSIP_EVSUB_STATE_TERMINATED ? "terminated" : "active");
  1456. /* Use the PJLIB-util XML library directly here since we are using a
  1457. * pj_str_t
  1458. */
  1459. cid_attr = pj_xml_attr_new(pool, &cid_name, &cid_stripped);
  1460. pj_xml_add_attr(instance, cid_attr);
  1461. }
  1462. /*!
  1463. * \brief A multipart body part and meta-information
  1464. *
  1465. * When creating a multipart body part, the end result (the
  1466. * pjsip_multipart_part) is hard to inspect without undoing
  1467. * a lot of what was done to create it. Therefore, we use this
  1468. * structure to store meta-information about the body part.
  1469. *
  1470. * The main consumer of this is the creator of the RLMI body
  1471. * part of a multipart resource list body.
  1472. */
  1473. struct body_part {
  1474. /*! Content-ID header for the body part */
  1475. pjsip_generic_string_hdr *cid;
  1476. /*! Subscribed resource represented in the body part */
  1477. const char *resource;
  1478. /*! URI for the subscribed body part */
  1479. pjsip_sip_uri *uri;
  1480. /*! Subscription state of the resource represented in the body part */
  1481. pjsip_evsub_state state;
  1482. /*! The actual body part that will be present in the multipart body */
  1483. pjsip_multipart_part *part;
  1484. };
  1485. /*!
  1486. * \brief Type declaration for container of body part structures
  1487. */
  1488. AST_VECTOR(body_part_list, struct body_part *);
  1489. /*!
  1490. * \brief Create a Content-ID header
  1491. *
  1492. * Content-ID headers are required by RFC2387 for multipart/related
  1493. * bodies. They serve as identifiers for each part of the multipart body.
  1494. *
  1495. * \param pool PJLIB allocation pool
  1496. * \param sub Subscription to a resource
  1497. */
  1498. static pjsip_generic_string_hdr *generate_content_id_hdr(pj_pool_t *pool,
  1499. const struct ast_sip_subscription *sub)
  1500. {
  1501. static const pj_str_t cid_name = { "Content-ID", 10 };
  1502. pjsip_generic_string_hdr *cid;
  1503. char id[6];
  1504. size_t alloc_size;
  1505. pj_str_t cid_value;
  1506. /* '<' + '@' + '>' = 3. pj_str_t does not require a null-terminator */
  1507. alloc_size = sizeof(id) + pj_strlen(&sub->uri->host) + 3;
  1508. cid_value.ptr = pj_pool_alloc(pool, alloc_size);
  1509. cid_value.slen = sprintf(cid_value.ptr, "<%s@%.*s>",
  1510. ast_generate_random_string(id, sizeof(id)),
  1511. (int) pj_strlen(&sub->uri->host), pj_strbuf(&sub->uri->host));
  1512. cid = pjsip_generic_string_hdr_create(pool, &cid_name, &cid_value);
  1513. return cid;
  1514. }
  1515. static int rlmi_print_body(struct pjsip_msg_body *msg_body, char *buf, pj_size_t size)
  1516. {
  1517. int num_printed;
  1518. pj_xml_node *rlmi = msg_body->data;
  1519. num_printed = pj_xml_print(rlmi, buf, size, PJ_TRUE);
  1520. if (num_printed == AST_PJSIP_XML_PROLOG_LEN) {
  1521. return -1;
  1522. }
  1523. return num_printed;
  1524. }
  1525. static void *rlmi_clone_data(pj_pool_t *pool, const void *data, unsigned len)
  1526. {
  1527. const pj_xml_node *rlmi = data;
  1528. return pj_xml_clone(pool, rlmi);
  1529. }
  1530. /*!
  1531. * \brief Create an RLMI body part for a multipart resource list body
  1532. *
  1533. * RLMI (Resource list meta information) is a special body type that lists
  1534. * the subscribed resources and tells subscribers the number of subscribed
  1535. * resources and what other body parts are in the multipart body. The
  1536. * RLMI body also has a version number that a subscriber can use to ensure
  1537. * that the locally-stored state corresponds to server state.
  1538. *
  1539. * \param pool The allocation pool
  1540. * \param sub The subscription representing the subscribed resource list
  1541. * \param body_parts A container of body parts that RLMI will refer to
  1542. * \param full_state Indicates whether this is a full or partial state notification
  1543. * \return The multipart part representing the RLMI body
  1544. */
  1545. static pjsip_multipart_part *build_rlmi_body(pj_pool_t *pool, struct ast_sip_subscription *sub,
  1546. struct body_part_list *body_parts, unsigned int full_state)
  1547. {
  1548. static const pj_str_t rlmi_type = { "application", 11 };
  1549. static const pj_str_t rlmi_subtype = { "rlmi+xml", 8 };
  1550. pj_xml_node *rlmi;
  1551. pj_xml_node *name;
  1552. pjsip_multipart_part *rlmi_part;
  1553. char version_str[32];
  1554. char uri[PJSIP_MAX_URL_SIZE];
  1555. pjsip_generic_string_hdr *cid;
  1556. int i;
  1557. rlmi = ast_sip_presence_xml_create_node(pool, NULL, "list");
  1558. ast_sip_presence_xml_create_attr(pool, rlmi, "xmlns", "urn:ietf:params:xml:ns:rlmi");
  1559. ast_sip_subscription_get_local_uri(sub, uri, sizeof(uri));
  1560. ast_sip_presence_xml_create_attr(pool, rlmi, "uri", uri);
  1561. snprintf(version_str, sizeof(version_str), "%u", sub->version++);
  1562. ast_sip_presence_xml_create_attr(pool, rlmi, "version", version_str);
  1563. ast_sip_presence_xml_create_attr(pool, rlmi, "fullState", full_state ? "true" : "false");
  1564. name = ast_sip_presence_xml_create_node(pool, rlmi, "name");
  1565. pj_strdup2(pool, &name->content, ast_sip_subscription_get_resource_name(sub));
  1566. for (i = 0; i < AST_VECTOR_SIZE(body_parts); ++i) {
  1567. const struct body_part *part = AST_VECTOR_GET(body_parts, i);
  1568. add_rlmi_resource(pool, rlmi, part->cid, part->resource, part->uri, part->state);
  1569. }
  1570. rlmi_part = pjsip_multipart_create_part(pool);
  1571. rlmi_part->body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
  1572. pj_strdup(pool, &rlmi_part->body->content_type.type, &rlmi_type);
  1573. pj_strdup(pool, &rlmi_part->body->content_type.subtype, &rlmi_subtype);
  1574. pj_list_init(&rlmi_part->body->content_type.param);
  1575. rlmi_part->body->data = pj_xml_clone(pool, rlmi);
  1576. rlmi_part->body->clone_data = rlmi_clone_data;
  1577. rlmi_part->body->print_body = rlmi_print_body;
  1578. cid = generate_content_id_hdr(pool, sub);
  1579. pj_list_insert_before(&rlmi_part->hdr, cid);
  1580. return rlmi_part;
  1581. }
  1582. static pjsip_msg_body *generate_notify_body(pj_pool_t *pool, struct ast_sip_subscription *root,
  1583. unsigned int force_full_state);
  1584. /*!
  1585. * \brief Destroy a list of body parts
  1586. *
  1587. * \param parts The container of parts to destroy
  1588. */
  1589. static void free_body_parts(struct body_part_list *parts)
  1590. {
  1591. int i;
  1592. for (i = 0; i < AST_VECTOR_SIZE(parts); ++i) {
  1593. struct body_part *part = AST_VECTOR_GET(parts, i);
  1594. ast_free(part);
  1595. }
  1596. AST_VECTOR_FREE(parts);
  1597. }
  1598. /*!
  1599. * \brief Allocate and initialize a body part structure
  1600. *
  1601. * \param pool PJLIB allocation pool
  1602. * \param sub Subscription representing a subscribed resource
  1603. */
  1604. static struct body_part *allocate_body_part(pj_pool_t *pool, const struct ast_sip_subscription *sub)
  1605. {
  1606. struct body_part *bp;
  1607. bp = ast_calloc(1, sizeof(*bp));
  1608. if (!bp) {
  1609. return NULL;
  1610. }
  1611. bp->cid = generate_content_id_hdr(pool, sub);
  1612. bp->resource = sub->resource;
  1613. bp->state = sub->subscription_state;
  1614. bp->uri = sub->uri;
  1615. return bp;
  1616. }
  1617. /*!
  1618. * \brief Create a multipart body part for a subscribed resource
  1619. *
  1620. * \param pool PJLIB allocation pool
  1621. * \param sub The subscription representing a subscribed resource
  1622. * \param parts A vector of parts to append the created part to.
  1623. * \param use_full_state Unused locally, but may be passed to other functions
  1624. */
  1625. static void build_body_part(pj_pool_t *pool, struct ast_sip_subscription *sub,
  1626. struct body_part_list *parts, unsigned int use_full_state)
  1627. {
  1628. struct body_part *bp;
  1629. pjsip_msg_body *body;
  1630. bp = allocate_body_part(pool, sub);
  1631. if (!bp) {
  1632. return;
  1633. }
  1634. body = generate_notify_body(pool, sub, use_full_state);
  1635. if (!body) {
  1636. /* Partial state was requested and the resource has not changed state */
  1637. ast_free(bp);
  1638. return;
  1639. }
  1640. bp->part = pjsip_multipart_create_part(pool);
  1641. bp->part->body = body;
  1642. pj_list_insert_before(&bp->part->hdr, bp->cid);
  1643. AST_VECTOR_APPEND(parts, bp);
  1644. }
  1645. /*!
  1646. * \brief Create and initialize the PJSIP multipart body structure for a resource list subscription
  1647. *
  1648. * \param pool
  1649. * \return The multipart message body
  1650. */
  1651. static pjsip_msg_body *create_multipart_body(pj_pool_t *pool)
  1652. {
  1653. pjsip_media_type media_type;
  1654. pjsip_param *media_type_param;
  1655. char boundary[6];
  1656. pj_str_t pj_boundary;
  1657. pjsip_media_type_init2(&media_type, "multipart", "related");
  1658. media_type_param = pj_pool_alloc(pool, sizeof(*media_type_param));
  1659. pj_list_init(media_type_param);
  1660. pj_strdup2(pool, &media_type_param->name, "type");
  1661. pj_strdup2(pool, &media_type_param->value, "\"application/rlmi+xml\"");
  1662. pj_list_insert_before(&media_type.param, media_type_param);
  1663. pj_cstr(&pj_boundary, ast_generate_random_string(boundary, sizeof(boundary)));
  1664. return pjsip_multipart_create(pool, &media_type, &pj_boundary);
  1665. }
  1666. /*!
  1667. * \brief Create a resource list body for NOTIFY requests
  1668. *
  1669. * Resource list bodies are multipart/related bodies. The first part of the multipart body
  1670. * is an RLMI body that describes the rest of the parts to come. The other parts of the body
  1671. * convey state of individual subscribed resources.
  1672. *
  1673. * \param pool PJLIB allocation pool
  1674. * \param sub Subscription details from which to generate body
  1675. * \param force_full_state If true, ignore resource list settings and send a full state notification
  1676. * \return The generated multipart/related body
  1677. */
  1678. static pjsip_msg_body *generate_list_body(pj_pool_t *pool, struct ast_sip_subscription *sub,
  1679. unsigned int force_full_state)
  1680. {
  1681. int i;
  1682. pjsip_multipart_part *rlmi_part;
  1683. pjsip_msg_body *multipart;
  1684. struct body_part_list body_parts;
  1685. unsigned int use_full_state = force_full_state ? 1 : sub->full_state;
  1686. if (AST_VECTOR_INIT(&body_parts, AST_VECTOR_SIZE(&sub->children))) {
  1687. return NULL;
  1688. }
  1689. for (i = 0; i < AST_VECTOR_SIZE(&sub->children); ++i) {
  1690. build_body_part(pool, AST_VECTOR_GET(&sub->children, i), &body_parts, use_full_state);
  1691. }
  1692. /* This can happen if issuing partial state and no children of the list have changed state */
  1693. if (AST_VECTOR_SIZE(&body_parts) == 0) {
  1694. return NULL;
  1695. }
  1696. multipart = create_multipart_body(pool);
  1697. rlmi_part = build_rlmi_body(pool, sub, &body_parts, use_full_state);
  1698. if (!rlmi_part) {
  1699. return NULL;
  1700. }
  1701. pjsip_multipart_add_part(pool, multipart, rlmi_part);
  1702. for (i = 0; i < AST_VECTOR_SIZE(&body_parts); ++i) {
  1703. pjsip_multipart_add_part(pool, multipart, AST_VECTOR_GET(&body_parts, i)->part);
  1704. }
  1705. free_body_parts(&body_parts);
  1706. return multipart;
  1707. }
  1708. /*!
  1709. * \brief Create the body for a NOTIFY request.
  1710. *
  1711. * \param pool The pool used for allocations
  1712. * \param root The root of the subscription tree
  1713. * \param force_full_state If true, ignore resource list settings and send a full state notification
  1714. */
  1715. static pjsip_msg_body *generate_notify_body(pj_pool_t *pool, struct ast_sip_subscription *root,
  1716. unsigned int force_full_state)
  1717. {
  1718. pjsip_msg_body *body;
  1719. if (AST_VECTOR_SIZE(&root->children) == 0) {
  1720. if (force_full_state || root->body_changed) {
  1721. /* Not a list. We've already generated the body and saved it on the subscription.
  1722. * Use that directly.
  1723. */
  1724. pj_str_t type;
  1725. pj_str_t subtype;
  1726. pj_str_t text;
  1727. pj_cstr(&type, ast_sip_subscription_get_body_type(root));
  1728. pj_cstr(&subtype, ast_sip_subscription_get_body_subtype(root));
  1729. pj_cstr(&text, ast_str_buffer(root->body_text));
  1730. body = pjsip_msg_body_create(pool, &type, &subtype, &text);
  1731. root->body_changed = 0;
  1732. } else {
  1733. body = NULL;
  1734. }
  1735. } else {
  1736. body = generate_list_body(pool, root, force_full_state);
  1737. }
  1738. return body;
  1739. }
  1740. /*!
  1741. * \brief Shortcut method to create a Require: eventlist header
  1742. */
  1743. static pjsip_require_hdr *create_require_eventlist(pj_pool_t *pool)
  1744. {
  1745. pjsip_require_hdr *require;
  1746. require = pjsip_require_hdr_create(pool);
  1747. pj_strdup2(pool, &require->values[0], "eventlist");
  1748. require->count = 1;
  1749. return require;
  1750. }
  1751. /*!
  1752. * \brief Send a NOTIFY request to a subscriber
  1753. *
  1754. * \param sub_tree The subscription tree representing the subscription
  1755. * \param force_full_state If true, ignore resource list settings and send full resource list state.
  1756. * \retval 0 Success
  1757. * \retval non-zero Failure
  1758. */
  1759. static int send_notify(struct sip_subscription_tree *sub_tree, unsigned int force_full_state)
  1760. {
  1761. pjsip_evsub *evsub = sub_tree->evsub;
  1762. pjsip_tx_data *tdata;
  1763. if (pjsip_evsub_notify(evsub, sub_tree->root->subscription_state,
  1764. NULL, NULL, &tdata) != PJ_SUCCESS) {
  1765. return -1;
  1766. }
  1767. tdata->msg->body = generate_notify_body(tdata->pool, sub_tree->root, force_full_state);
  1768. if (!tdata->msg->body) {
  1769. pjsip_tx_data_dec_ref(tdata);
  1770. return -1;
  1771. }
  1772. if (sub_tree->is_list) {
  1773. pjsip_require_hdr *require = create_require_eventlist(tdata->pool);
  1774. pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) require);
  1775. }
  1776. if (sip_subscription_send_request(sub_tree, tdata)) {
  1777. return -1;
  1778. }
  1779. sub_tree->send_scheduled_notify = 0;
  1780. return 0;
  1781. }
  1782. static int serialized_send_notify(void *userdata)
  1783. {
  1784. struct sip_subscription_tree *sub_tree = userdata;
  1785. /* It's possible that between when the notification was scheduled
  1786. * and now, that a new SUBSCRIBE arrived, requiring full state to be
  1787. * sent out in an immediate NOTIFY. If that has happened, we need to
  1788. * bail out here instead of sending the batched NOTIFY.
  1789. */
  1790. if (!sub_tree->send_scheduled_notify) {
  1791. ao2_cleanup(sub_tree);
  1792. return 0;
  1793. }
  1794. send_notify(sub_tree, 0);
  1795. ast_test_suite_event_notify("SUBSCRIPTION_STATE_CHANGED",
  1796. "Resource: %s",
  1797. sub_tree->root->resource);
  1798. sub_tree->notify_sched_id = -1;
  1799. ao2_cleanup(sub_tree);
  1800. return 0;
  1801. }
  1802. static int sched_cb(const void *data)
  1803. {
  1804. struct sip_subscription_tree *sub_tree = (struct sip_subscription_tree *) data;
  1805. /* We don't need to bump the refcount of sub_tree since we bumped it when scheduling this task */
  1806. ast_sip_push_task(sub_tree->serializer, serialized_send_notify, sub_tree);
  1807. return 0;
  1808. }
  1809. static int schedule_notification(struct sip_subscription_tree *sub_tree)
  1810. {
  1811. /* There's already a notification scheduled */
  1812. if (sub_tree->notify_sched_id > -1) {
  1813. return 0;
  1814. }
  1815. sub_tree->notify_sched_id = ast_sched_add(sched, sub_tree->notification_batch_interval, sched_cb, ao2_bump(sub_tree));
  1816. if (sub_tree->notify_sched_id < 0) {
  1817. return -1;
  1818. }
  1819. sub_tree->send_scheduled_notify = 1;
  1820. return 0;
  1821. }
  1822. int ast_sip_subscription_notify(struct ast_sip_subscription *sub, struct ast_sip_body_data *notify_data,
  1823. int terminate)
  1824. {
  1825. if (ast_sip_pubsub_generate_body_content(ast_sip_subscription_get_body_type(sub),
  1826. ast_sip_subscription_get_body_subtype(sub), notify_data, &sub->body_text)) {
  1827. return -1;
  1828. }
  1829. sub->body_changed = 1;
  1830. if (terminate) {
  1831. sub->subscription_state = PJSIP_EVSUB_STATE_TERMINATED;
  1832. }
  1833. if (sub->tree->notification_batch_interval) {
  1834. return schedule_notification(sub->tree);
  1835. } else {
  1836. int res;
  1837. /* See the note in pubsub_on_rx_refresh() for why sub->tree is refbumped here */
  1838. ao2_ref(sub->tree, +1);
  1839. res = send_notify(sub->tree, 0);
  1840. ast_test_suite_event_notify(terminate ? "SUBSCRIPTION_TERMINATED" : "SUBSCRIPTION_STATE_CHANGED",
  1841. "Resource: %s",
  1842. sub->tree->root->resource);
  1843. ao2_ref(sub->tree, -1);
  1844. return res;
  1845. }
  1846. }
  1847. void ast_sip_subscription_get_local_uri(struct ast_sip_subscription *sub, char *buf, size_t size)
  1848. {
  1849. pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, sub->uri, buf, size);
  1850. }
  1851. void ast_sip_subscription_get_remote_uri(struct ast_sip_subscription *sub, char *buf, size_t size)
  1852. {
  1853. pjsip_dialog *dlg = sub->tree->dlg;
  1854. ast_copy_pj_str(buf, &dlg->remote.info_str, size);
  1855. }
  1856. const char *ast_sip_subscription_get_resource_name(struct ast_sip_subscription *sub)
  1857. {
  1858. return sub->resource;
  1859. }
  1860. static int sip_subscription_accept(struct sip_subscription_tree *sub_tree, pjsip_rx_data *rdata, int response)
  1861. {
  1862. pjsip_hdr res_hdr;
  1863. /* If this is a persistence recreation the subscription has already been accepted */
  1864. if (ast_sip_mod_data_get(rdata->endpt_info.mod_data, pubsub_module.id, MOD_DATA_PERSISTENCE)) {
  1865. return 0;
  1866. }
  1867. pj_list_init(&res_hdr);
  1868. if (sub_tree->is_list) {
  1869. /* If subscribing to a list, our response has to have a Require: eventlist header in it */
  1870. pj_list_insert_before(&res_hdr, create_require_eventlist(rdata->tp_info.pool));
  1871. }
  1872. return pjsip_evsub_accept(sub_tree->evsub, rdata, response, &res_hdr) == PJ_SUCCESS ? 0 : -1;
  1873. }
  1874. static void subscription_datastore_destroy(void *obj)
  1875. {
  1876. struct ast_datastore *datastore = obj;
  1877. /* Using the destroy function (if present) destroy the data */
  1878. if (datastore->info->destroy != NULL && datastore->data != NULL) {
  1879. datastore->info->destroy(datastore->data);
  1880. datastore->data = NULL;
  1881. }
  1882. ast_free((void *) datastore->uid);
  1883. datastore->uid = NULL;
  1884. }
  1885. struct ast_datastore *ast_sip_subscription_alloc_datastore(const struct ast_datastore_info *info, const char *uid)
  1886. {
  1887. RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
  1888. char uuid_buf[AST_UUID_STR_LEN];
  1889. const char *uid_ptr = uid;
  1890. if (!info) {
  1891. return NULL;
  1892. }
  1893. datastore = ao2_alloc(sizeof(*datastore), subscription_datastore_destroy);
  1894. if (!datastore) {
  1895. return NULL;
  1896. }
  1897. datastore->info = info;
  1898. if (ast_strlen_zero(uid)) {
  1899. /* They didn't provide an ID so we'll provide one ourself */
  1900. uid_ptr = ast_uuid_generate_str(uuid_buf, sizeof(uuid_buf));
  1901. }
  1902. datastore->uid = ast_strdup(uid_ptr);
  1903. if (!datastore->uid) {
  1904. return NULL;
  1905. }
  1906. ao2_ref(datastore, +1);
  1907. return datastore;
  1908. }
  1909. int ast_sip_subscription_add_datastore(struct ast_sip_subscription *subscription, struct ast_datastore *datastore)
  1910. {
  1911. ast_assert(datastore != NULL);
  1912. ast_assert(datastore->info != NULL);
  1913. ast_assert(!ast_strlen_zero(datastore->uid));
  1914. if (!ao2_link(subscription->datastores, datastore)) {
  1915. return -1;
  1916. }
  1917. return 0;
  1918. }
  1919. struct ast_datastore *ast_sip_subscription_get_datastore(struct ast_sip_subscription *subscription, const char *name)
  1920. {
  1921. return ao2_find(subscription->datastores, name, OBJ_KEY);
  1922. }
  1923. void ast_sip_subscription_remove_datastore(struct ast_sip_subscription *subscription, const char *name)
  1924. {
  1925. ao2_find(subscription->datastores, name, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
  1926. }
  1927. int ast_sip_publication_add_datastore(struct ast_sip_publication *publication, struct ast_datastore *datastore)
  1928. {
  1929. ast_assert(datastore != NULL);
  1930. ast_assert(datastore->info != NULL);
  1931. ast_assert(!ast_strlen_zero(datastore->uid));
  1932. if (!ao2_link(publication->datastores, datastore)) {
  1933. return -1;
  1934. }
  1935. return 0;
  1936. }
  1937. struct ast_datastore *ast_sip_publication_get_datastore(struct ast_sip_publication *publication, const char *name)
  1938. {
  1939. return ao2_find(publication->datastores, name, OBJ_KEY);
  1940. }
  1941. void ast_sip_publication_remove_datastore(struct ast_sip_publication *publication, const char *name)
  1942. {
  1943. ao2_callback(publication->datastores, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA, NULL, (void *) name);
  1944. }
  1945. AST_RWLIST_HEAD_STATIC(publish_handlers, ast_sip_publish_handler);
  1946. static int publication_hash_fn(const void *obj, const int flags)
  1947. {
  1948. const struct ast_sip_publication *publication = obj;
  1949. const int *entity_tag = obj;
  1950. return flags & OBJ_KEY ? *entity_tag : publication->entity_tag;
  1951. }
  1952. static int publication_cmp_fn(void *obj, void *arg, int flags)
  1953. {
  1954. const struct ast_sip_publication *publication1 = obj;
  1955. const struct ast_sip_publication *publication2 = arg;
  1956. const int *entity_tag = arg;
  1957. return (publication1->entity_tag == (flags & OBJ_KEY ? *entity_tag : publication2->entity_tag) ?
  1958. CMP_MATCH | CMP_STOP : 0);
  1959. }
  1960. static void publish_add_handler(struct ast_sip_publish_handler *handler)
  1961. {
  1962. SCOPED_LOCK(lock, &publish_handlers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
  1963. AST_RWLIST_INSERT_TAIL(&publish_handlers, handler, next);
  1964. }
  1965. int ast_sip_register_publish_handler(struct ast_sip_publish_handler *handler)
  1966. {
  1967. if (ast_strlen_zero(handler->event_name)) {
  1968. ast_log(LOG_ERROR, "No event package specified for publish handler. Cannot register\n");
  1969. return -1;
  1970. }
  1971. if (!(handler->publications = ao2_container_alloc(PUBLICATIONS_BUCKETS,
  1972. publication_hash_fn, publication_cmp_fn))) {
  1973. ast_log(LOG_ERROR, "Could not allocate publications container for event '%s'\n",
  1974. handler->event_name);
  1975. return -1;
  1976. }
  1977. publish_add_handler(handler);
  1978. ast_module_ref(ast_module_info->self);
  1979. return 0;
  1980. }
  1981. void ast_sip_unregister_publish_handler(struct ast_sip_publish_handler *handler)
  1982. {
  1983. struct ast_sip_publish_handler *iter;
  1984. SCOPED_LOCK(lock, &publish_handlers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
  1985. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&publish_handlers, iter, next) {
  1986. if (handler == iter) {
  1987. AST_RWLIST_REMOVE_CURRENT(next);
  1988. ao2_cleanup(handler->publications);
  1989. ast_module_unref(ast_module_info->self);
  1990. break;
  1991. }
  1992. }
  1993. AST_RWLIST_TRAVERSE_SAFE_END;
  1994. }
  1995. AST_RWLIST_HEAD_STATIC(subscription_handlers, ast_sip_subscription_handler);
  1996. static void sub_add_handler(struct ast_sip_subscription_handler *handler)
  1997. {
  1998. SCOPED_LOCK(lock, &subscription_handlers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
  1999. AST_RWLIST_INSERT_TAIL(&subscription_handlers, handler, next);
  2000. ast_module_ref(ast_module_info->self);
  2001. }
  2002. static struct ast_sip_subscription_handler *find_sub_handler_for_event_name(const char *event_name)
  2003. {
  2004. struct ast_sip_subscription_handler *iter;
  2005. SCOPED_LOCK(lock, &subscription_handlers, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
  2006. AST_RWLIST_TRAVERSE(&subscription_handlers, iter, next) {
  2007. if (!strcmp(iter->event_name, event_name)) {
  2008. break;
  2009. }
  2010. }
  2011. return iter;
  2012. }
  2013. int ast_sip_register_subscription_handler(struct ast_sip_subscription_handler *handler)
  2014. {
  2015. pj_str_t event;
  2016. pj_str_t accept[AST_SIP_MAX_ACCEPT] = { {0, }, };
  2017. struct ast_sip_subscription_handler *existing;
  2018. int i = 0;
  2019. if (ast_strlen_zero(handler->event_name)) {
  2020. ast_log(LOG_ERROR, "No event package specified for subscription handler. Cannot register\n");
  2021. return -1;
  2022. }
  2023. existing = find_sub_handler_for_event_name(handler->event_name);
  2024. if (existing) {
  2025. ast_log(LOG_ERROR, "Unable to register subscription handler for event %s."
  2026. "A handler is already registered\n", handler->event_name);
  2027. return -1;
  2028. }
  2029. for (i = 0; i < AST_SIP_MAX_ACCEPT && !ast_strlen_zero(handler->accept[i]); ++i) {
  2030. pj_cstr(&accept[i], handler->accept[i]);
  2031. }
  2032. pj_cstr(&event, handler->event_name);
  2033. pjsip_evsub_register_pkg(&pubsub_module, &event, DEFAULT_EXPIRES, i, accept);
  2034. sub_add_handler(handler);
  2035. return 0;
  2036. }
  2037. void ast_sip_unregister_subscription_handler(struct ast_sip_subscription_handler *handler)
  2038. {
  2039. struct ast_sip_subscription_handler *iter;
  2040. SCOPED_LOCK(lock, &subscription_handlers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
  2041. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&subscription_handlers, iter, next) {
  2042. if (handler == iter) {
  2043. AST_RWLIST_REMOVE_CURRENT(next);
  2044. ast_module_unref(ast_module_info->self);
  2045. break;
  2046. }
  2047. }
  2048. AST_RWLIST_TRAVERSE_SAFE_END;
  2049. }
  2050. static struct ast_sip_pubsub_body_generator *find_body_generator_type_subtype(const char *content_type,
  2051. const char *content_subtype)
  2052. {
  2053. struct ast_sip_pubsub_body_generator *iter;
  2054. SCOPED_LOCK(lock, &body_generators, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
  2055. AST_LIST_TRAVERSE(&body_generators, iter, list) {
  2056. if (!strcmp(iter->type, content_type) &&
  2057. !strcmp(iter->subtype, content_subtype)) {
  2058. break;
  2059. }
  2060. };
  2061. return iter;
  2062. }
  2063. static struct ast_sip_pubsub_body_generator *find_body_generator_accept(const char *accept)
  2064. {
  2065. char *accept_copy = ast_strdupa(accept);
  2066. char *subtype = accept_copy;
  2067. char *type = strsep(&subtype, "/");
  2068. if (ast_strlen_zero(type) || ast_strlen_zero(subtype)) {
  2069. return NULL;
  2070. }
  2071. return find_body_generator_type_subtype(type, subtype);
  2072. }
  2073. static struct ast_sip_pubsub_body_generator *find_body_generator(char accept[AST_SIP_MAX_ACCEPT][64],
  2074. size_t num_accept, const char *body_type)
  2075. {
  2076. int i;
  2077. struct ast_sip_pubsub_body_generator *generator = NULL;
  2078. for (i = 0; i < num_accept; ++i) {
  2079. generator = find_body_generator_accept(accept[i]);
  2080. if (generator) {
  2081. ast_debug(3, "Body generator %p found for accept type %s\n", generator, accept[i]);
  2082. if (strcmp(generator->body_type, body_type)) {
  2083. ast_log(LOG_WARNING, "Body generator '%s/%s'(%p) does not accept the type of data this event generates\n",
  2084. generator->type, generator->subtype, generator);
  2085. generator = NULL;
  2086. continue;
  2087. }
  2088. break;
  2089. } else {
  2090. ast_debug(3, "No body generator found for accept type %s\n", accept[i]);
  2091. }
  2092. }
  2093. return generator;
  2094. }
  2095. static int generate_initial_notify(struct ast_sip_subscription *sub)
  2096. {
  2097. void *notify_data;
  2098. int res;
  2099. struct ast_sip_body_data data = {
  2100. .body_type = sub->handler->body_type,
  2101. };
  2102. if (AST_VECTOR_SIZE(&sub->children) > 0) {
  2103. int i;
  2104. for (i = 0; i < AST_VECTOR_SIZE(&sub->children); ++i) {
  2105. if (generate_initial_notify(AST_VECTOR_GET(&sub->children, i))) {
  2106. return -1;
  2107. }
  2108. }
  2109. return 0;
  2110. }
  2111. if (sub->handler->notifier->subscription_established(sub)) {
  2112. return -1;
  2113. }
  2114. notify_data = sub->handler->notifier->get_notify_data(sub);
  2115. if (!notify_data) {
  2116. return -1;
  2117. }
  2118. data.body_data = notify_data;
  2119. res = ast_sip_pubsub_generate_body_content(ast_sip_subscription_get_body_type(sub),
  2120. ast_sip_subscription_get_body_subtype(sub), &data, &sub->body_text);
  2121. ao2_cleanup(notify_data);
  2122. return res;
  2123. }
  2124. static pj_bool_t pubsub_on_rx_subscribe_request(pjsip_rx_data *rdata)
  2125. {
  2126. pjsip_expires_hdr *expires_header;
  2127. struct ast_sip_subscription_handler *handler;
  2128. RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
  2129. struct sip_subscription_tree *sub_tree;
  2130. struct ast_sip_pubsub_body_generator *generator;
  2131. char *resource;
  2132. pjsip_uri *request_uri;
  2133. pjsip_sip_uri *request_uri_sip;
  2134. size_t resource_size;
  2135. int resp;
  2136. struct resource_tree tree;
  2137. endpoint = ast_pjsip_rdata_get_endpoint(rdata);
  2138. ast_assert(endpoint != NULL);
  2139. if (!endpoint->subscription.allow) {
  2140. ast_log(LOG_WARNING, "Subscriptions not permitted for endpoint %s.\n", ast_sorcery_object_get_id(endpoint));
  2141. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 603, NULL, NULL, NULL);
  2142. return PJ_TRUE;
  2143. }
  2144. request_uri = rdata->msg_info.msg->line.req.uri;
  2145. if (!PJSIP_URI_SCHEME_IS_SIP(request_uri) && !PJSIP_URI_SCHEME_IS_SIPS(request_uri)) {
  2146. char uri_str[PJSIP_MAX_URL_SIZE];
  2147. pjsip_uri_print(PJSIP_URI_IN_REQ_URI, request_uri, uri_str, sizeof(uri_str));
  2148. ast_log(LOG_WARNING, "Request URI '%s' is not a sip: or sips: URI.\n", uri_str);
  2149. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 416, NULL, NULL, NULL);
  2150. return PJ_TRUE;
  2151. }
  2152. request_uri_sip = pjsip_uri_get_uri(request_uri);
  2153. resource_size = pj_strlen(&request_uri_sip->user) + 1;
  2154. resource = alloca(resource_size);
  2155. ast_copy_pj_str(resource, &request_uri_sip->user, resource_size);
  2156. expires_header = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, rdata->msg_info.msg->hdr.next);
  2157. if (expires_header) {
  2158. if (expires_header->ivalue == 0) {
  2159. ast_log(LOG_WARNING, "Subscription request from endpoint %s rejected. Expiration of 0 is invalid\n",
  2160. ast_sorcery_object_get_id(endpoint));
  2161. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 400, NULL, NULL, NULL);
  2162. return PJ_TRUE;
  2163. }
  2164. if (expires_header->ivalue < endpoint->subscription.minexpiry) {
  2165. ast_log(LOG_WARNING, "Subscription expiration %d is too brief for endpoint %s. Minimum is %u\n",
  2166. expires_header->ivalue, ast_sorcery_object_get_id(endpoint), endpoint->subscription.minexpiry);
  2167. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 423, NULL, NULL, NULL);
  2168. return PJ_TRUE;
  2169. }
  2170. }
  2171. handler = subscription_get_handler_from_rdata(rdata);
  2172. if (!handler) {
  2173. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL);
  2174. return PJ_TRUE;
  2175. }
  2176. generator = subscription_get_generator_from_rdata(rdata, handler);
  2177. if (!generator) {
  2178. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL);
  2179. return PJ_TRUE;
  2180. }
  2181. memset(&tree, 0, sizeof(tree));
  2182. resp = build_resource_tree(endpoint, handler, resource, &tree,
  2183. ast_sip_pubsub_has_eventlist_support(rdata));
  2184. if (!PJSIP_IS_STATUS_IN_CLASS(resp, 200)) {
  2185. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, resp, NULL, NULL, NULL);
  2186. resource_tree_destroy(&tree);
  2187. return PJ_TRUE;
  2188. }
  2189. sub_tree = create_subscription_tree(handler, endpoint, rdata, resource, generator, &tree);
  2190. if (!sub_tree) {
  2191. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
  2192. } else {
  2193. sub_tree->persistence = subscription_persistence_create(sub_tree);
  2194. subscription_persistence_update(sub_tree, rdata);
  2195. sip_subscription_accept(sub_tree, rdata, resp);
  2196. if (generate_initial_notify(sub_tree->root)) {
  2197. pjsip_evsub_terminate(sub_tree->evsub, PJ_TRUE);
  2198. }
  2199. send_notify(sub_tree, 1);
  2200. ast_test_suite_event_notify("SUBSCRIPTION_ESTABLISHED",
  2201. "Resource: %s",
  2202. sub_tree->root->resource);
  2203. }
  2204. resource_tree_destroy(&tree);
  2205. return PJ_TRUE;
  2206. }
  2207. static struct ast_sip_publish_handler *find_pub_handler(const char *event)
  2208. {
  2209. struct ast_sip_publish_handler *iter = NULL;
  2210. SCOPED_LOCK(lock, &publish_handlers, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
  2211. AST_RWLIST_TRAVERSE(&publish_handlers, iter, next) {
  2212. if (strcmp(event, iter->event_name)) {
  2213. ast_debug(3, "Event %s does not match %s\n", event, iter->event_name);
  2214. continue;
  2215. }
  2216. ast_debug(3, "Event name match: %s = %s\n", event, iter->event_name);
  2217. break;
  2218. }
  2219. return iter;
  2220. }
  2221. static enum sip_publish_type determine_sip_publish_type(pjsip_rx_data *rdata,
  2222. pjsip_generic_string_hdr *etag_hdr, int *expires, int *entity_id)
  2223. {
  2224. pjsip_expires_hdr *expires_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL);
  2225. if (etag_hdr) {
  2226. char etag[pj_strlen(&etag_hdr->hvalue) + 1];
  2227. ast_copy_pj_str(etag, &etag_hdr->hvalue, sizeof(etag));
  2228. if (sscanf(etag, "%30d", entity_id) != 1) {
  2229. return SIP_PUBLISH_UNKNOWN;
  2230. }
  2231. }
  2232. *expires = expires_hdr ? expires_hdr->ivalue : DEFAULT_PUBLISH_EXPIRES;
  2233. if (!(*expires)) {
  2234. return SIP_PUBLISH_REMOVE;
  2235. } else if (!etag_hdr && rdata->msg_info.msg->body) {
  2236. return SIP_PUBLISH_INITIAL;
  2237. } else if (etag_hdr && !rdata->msg_info.msg->body) {
  2238. return SIP_PUBLISH_REFRESH;
  2239. } else if (etag_hdr && rdata->msg_info.msg->body) {
  2240. return SIP_PUBLISH_MODIFY;
  2241. }
  2242. return SIP_PUBLISH_UNKNOWN;
  2243. }
  2244. /*! \brief Internal destructor for publications */
  2245. static void publication_destroy_fn(void *obj)
  2246. {
  2247. struct ast_sip_publication *publication = obj;
  2248. ast_debug(3, "Destroying SIP publication\n");
  2249. ao2_cleanup(publication->datastores);
  2250. ao2_cleanup(publication->endpoint);
  2251. }
  2252. static struct ast_sip_publication *sip_create_publication(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata,
  2253. const char *resource, const char *event_configuration_name)
  2254. {
  2255. struct ast_sip_publication *publication;
  2256. pjsip_expires_hdr *expires_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL);
  2257. size_t resource_len = strlen(resource) + 1, event_configuration_name_len = strlen(event_configuration_name) + 1;
  2258. char *dst;
  2259. ast_assert(endpoint != NULL);
  2260. if (!(publication = ao2_alloc(sizeof(*publication) + resource_len + event_configuration_name_len, publication_destroy_fn))) {
  2261. return NULL;
  2262. }
  2263. if (!(publication->datastores = ao2_container_alloc(DATASTORE_BUCKETS, datastore_hash, datastore_cmp))) {
  2264. ao2_ref(publication, -1);
  2265. return NULL;
  2266. }
  2267. publication->entity_tag = ast_atomic_fetchadd_int(&esc_etag_counter, +1);
  2268. ao2_ref(endpoint, +1);
  2269. publication->endpoint = endpoint;
  2270. publication->expires = expires_hdr ? expires_hdr->ivalue : DEFAULT_PUBLISH_EXPIRES;
  2271. publication->sched_id = -1;
  2272. dst = publication->data;
  2273. publication->resource = strcpy(dst, resource);
  2274. dst += resource_len;
  2275. publication->event_configuration_name = strcpy(dst, event_configuration_name);
  2276. return publication;
  2277. }
  2278. static int sip_publication_respond(struct ast_sip_publication *pub, int status_code,
  2279. pjsip_rx_data *rdata)
  2280. {
  2281. pj_status_t status;
  2282. pjsip_tx_data *tdata;
  2283. pjsip_transaction *tsx;
  2284. if (pjsip_endpt_create_response(ast_sip_get_pjsip_endpoint(), rdata, status_code, NULL, &tdata) != PJ_SUCCESS) {
  2285. return -1;
  2286. }
  2287. if (PJSIP_IS_STATUS_IN_CLASS(status_code, 200)) {
  2288. RAII_VAR(char *, entity_tag, NULL, ast_free_ptr);
  2289. RAII_VAR(char *, expires, NULL, ast_free_ptr);
  2290. if ((ast_asprintf(&entity_tag, "%d", pub->entity_tag) < 0) ||
  2291. (ast_asprintf(&expires, "%d", pub->expires) < 0)) {
  2292. pjsip_tx_data_dec_ref(tdata);
  2293. return -1;
  2294. }
  2295. ast_sip_add_header(tdata, "SIP-ETag", entity_tag);
  2296. ast_sip_add_header(tdata, "Expires", expires);
  2297. }
  2298. if ((status = pjsip_tsx_create_uas(&pubsub_module, rdata, &tsx)) != PJ_SUCCESS) {
  2299. return -1;
  2300. }
  2301. pjsip_tsx_recv_msg(tsx, rdata);
  2302. if (pjsip_tsx_send_msg(tsx, tdata) != PJ_SUCCESS) {
  2303. return -1;
  2304. }
  2305. return 0;
  2306. }
  2307. static struct ast_sip_publication *publish_request_initial(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata,
  2308. struct ast_sip_publish_handler *handler)
  2309. {
  2310. struct ast_sip_publication *publication;
  2311. char *resource_name;
  2312. size_t resource_size;
  2313. RAII_VAR(struct ast_sip_publication_resource *, resource, NULL, ao2_cleanup);
  2314. struct ast_variable *event_configuration_name = NULL;
  2315. pjsip_uri *request_uri;
  2316. pjsip_sip_uri *request_uri_sip;
  2317. int resp;
  2318. request_uri = rdata->msg_info.msg->line.req.uri;
  2319. if (!PJSIP_URI_SCHEME_IS_SIP(request_uri) && !PJSIP_URI_SCHEME_IS_SIPS(request_uri)) {
  2320. char uri_str[PJSIP_MAX_URL_SIZE];
  2321. pjsip_uri_print(PJSIP_URI_IN_REQ_URI, request_uri, uri_str, sizeof(uri_str));
  2322. ast_log(LOG_WARNING, "Request URI '%s' is not a sip: or sips: URI.\n", uri_str);
  2323. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 416, NULL, NULL, NULL);
  2324. return NULL;
  2325. }
  2326. request_uri_sip = pjsip_uri_get_uri(request_uri);
  2327. resource_size = pj_strlen(&request_uri_sip->user) + 1;
  2328. resource_name = alloca(resource_size);
  2329. ast_copy_pj_str(resource_name, &request_uri_sip->user, resource_size);
  2330. resource = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "inbound-publication", resource_name);
  2331. if (!resource) {
  2332. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 404, NULL, NULL, NULL);
  2333. return NULL;
  2334. }
  2335. if (!ast_strlen_zero(resource->endpoint) && strcmp(resource->endpoint, ast_sorcery_object_get_id(endpoint))) {
  2336. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 403, NULL, NULL, NULL);
  2337. return NULL;
  2338. }
  2339. for (event_configuration_name = resource->events; event_configuration_name; event_configuration_name = event_configuration_name->next) {
  2340. if (!strcmp(event_configuration_name->name, handler->event_name)) {
  2341. break;
  2342. }
  2343. }
  2344. if (!event_configuration_name) {
  2345. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 404, NULL, NULL, NULL);
  2346. return NULL;
  2347. }
  2348. resp = handler->new_publication(endpoint, resource_name, event_configuration_name->value);
  2349. if (!PJSIP_IS_STATUS_IN_CLASS(resp, 200)) {
  2350. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, resp, NULL, NULL, NULL);
  2351. return NULL;
  2352. }
  2353. publication = sip_create_publication(endpoint, rdata, S_OR(resource_name, ""), event_configuration_name->value);
  2354. if (!publication) {
  2355. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 503, NULL, NULL, NULL);
  2356. return NULL;
  2357. }
  2358. publication->handler = handler;
  2359. if (publication->handler->publication_state_change(publication, rdata->msg_info.msg->body,
  2360. AST_SIP_PUBLISH_STATE_INITIALIZED)) {
  2361. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
  2362. ao2_cleanup(publication);
  2363. return NULL;
  2364. }
  2365. sip_publication_respond(publication, resp, rdata);
  2366. return publication;
  2367. }
  2368. static int publish_expire_callback(void *data)
  2369. {
  2370. RAII_VAR(struct ast_sip_publication *, publication, data, ao2_cleanup);
  2371. if (publication->handler->publish_expire) {
  2372. publication->handler->publish_expire(publication);
  2373. }
  2374. return 0;
  2375. }
  2376. static int publish_expire(const void *data)
  2377. {
  2378. struct ast_sip_publication *publication = (struct ast_sip_publication*)data;
  2379. ao2_unlink(publication->handler->publications, publication);
  2380. publication->sched_id = -1;
  2381. if (ast_sip_push_task(NULL, publish_expire_callback, publication)) {
  2382. ao2_cleanup(publication);
  2383. }
  2384. return 0;
  2385. }
  2386. static pj_bool_t pubsub_on_rx_publish_request(pjsip_rx_data *rdata)
  2387. {
  2388. pjsip_event_hdr *event_header;
  2389. struct ast_sip_publish_handler *handler;
  2390. RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
  2391. char event[32];
  2392. static const pj_str_t str_sip_if_match = { "SIP-If-Match", 12 };
  2393. pjsip_generic_string_hdr *etag_hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_sip_if_match, NULL);
  2394. enum sip_publish_type publish_type;
  2395. RAII_VAR(struct ast_sip_publication *, publication, NULL, ao2_cleanup);
  2396. int expires = 0, entity_id, response = 0;
  2397. endpoint = ast_pjsip_rdata_get_endpoint(rdata);
  2398. ast_assert(endpoint != NULL);
  2399. event_header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_event_name, rdata->msg_info.msg->hdr.next);
  2400. if (!event_header) {
  2401. ast_log(LOG_WARNING, "Incoming PUBLISH request with no Event header\n");
  2402. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL);
  2403. return PJ_TRUE;
  2404. }
  2405. ast_copy_pj_str(event, &event_header->event_type, sizeof(event));
  2406. handler = find_pub_handler(event);
  2407. if (!handler) {
  2408. ast_log(LOG_WARNING, "No registered publish handler for event %s\n", event);
  2409. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL);
  2410. return PJ_TRUE;
  2411. }
  2412. publish_type = determine_sip_publish_type(rdata, etag_hdr, &expires, &entity_id);
  2413. /* If this is not an initial publish ensure that a publication is present */
  2414. if ((publish_type != SIP_PUBLISH_INITIAL) && (publish_type != SIP_PUBLISH_UNKNOWN)) {
  2415. if (!(publication = ao2_find(handler->publications, &entity_id, OBJ_KEY | OBJ_UNLINK))) {
  2416. static const pj_str_t str_conditional_request_failed = { "Conditional Request Failed", 26 };
  2417. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 412, &str_conditional_request_failed,
  2418. NULL, NULL);
  2419. return PJ_TRUE;
  2420. }
  2421. /* Per the RFC every response has to have a new entity tag */
  2422. publication->entity_tag = ast_atomic_fetchadd_int(&esc_etag_counter, +1);
  2423. /* Update the expires here so that the created responses will contain the correct value */
  2424. publication->expires = expires;
  2425. }
  2426. switch (publish_type) {
  2427. case SIP_PUBLISH_INITIAL:
  2428. publication = publish_request_initial(endpoint, rdata, handler);
  2429. break;
  2430. case SIP_PUBLISH_REFRESH:
  2431. case SIP_PUBLISH_MODIFY:
  2432. if (handler->publication_state_change(publication, rdata->msg_info.msg->body,
  2433. AST_SIP_PUBLISH_STATE_ACTIVE)) {
  2434. /* If an error occurs we want to terminate the publication */
  2435. expires = 0;
  2436. }
  2437. response = 200;
  2438. break;
  2439. case SIP_PUBLISH_REMOVE:
  2440. handler->publication_state_change(publication, rdata->msg_info.msg->body,
  2441. AST_SIP_PUBLISH_STATE_TERMINATED);
  2442. response = 200;
  2443. break;
  2444. case SIP_PUBLISH_UNKNOWN:
  2445. default:
  2446. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 400, NULL, NULL, NULL);
  2447. break;
  2448. }
  2449. if (publication) {
  2450. if (expires) {
  2451. ao2_link(handler->publications, publication);
  2452. AST_SCHED_REPLACE_UNREF(publication->sched_id, sched, expires * 1000, publish_expire, publication,
  2453. ao2_ref(publication, -1), ao2_ref(publication, -1), ao2_ref(publication, +1));
  2454. } else {
  2455. AST_SCHED_DEL_UNREF(sched, publication->sched_id, ao2_ref(publication, -1));
  2456. }
  2457. }
  2458. if (response) {
  2459. sip_publication_respond(publication, response, rdata);
  2460. }
  2461. return PJ_TRUE;
  2462. }
  2463. struct ast_sip_endpoint *ast_sip_publication_get_endpoint(struct ast_sip_publication *pub)
  2464. {
  2465. return pub->endpoint;
  2466. }
  2467. const char *ast_sip_publication_get_resource(const struct ast_sip_publication *pub)
  2468. {
  2469. return pub->resource;
  2470. }
  2471. const char *ast_sip_publication_get_event_configuration(const struct ast_sip_publication *pub)
  2472. {
  2473. return pub->event_configuration_name;
  2474. }
  2475. int ast_sip_pubsub_register_body_generator(struct ast_sip_pubsub_body_generator *generator)
  2476. {
  2477. struct ast_sip_pubsub_body_generator *existing;
  2478. pj_str_t accept;
  2479. pj_size_t accept_len;
  2480. existing = find_body_generator_type_subtype(generator->type, generator->subtype);
  2481. if (existing) {
  2482. ast_log(LOG_WARNING, "Cannot register body generator of %s/%s."
  2483. "One is already registered.\n", generator->type, generator->subtype);
  2484. return -1;
  2485. }
  2486. AST_RWLIST_WRLOCK(&body_generators);
  2487. AST_LIST_INSERT_HEAD(&body_generators, generator, list);
  2488. AST_RWLIST_UNLOCK(&body_generators);
  2489. /* Lengths of type and subtype plus space for a slash. pj_str_t is not
  2490. * null-terminated, so there is no need to allocate for the extra null
  2491. * byte
  2492. */
  2493. accept_len = strlen(generator->type) + strlen(generator->subtype) + 1;
  2494. accept.ptr = alloca(accept_len);
  2495. accept.slen = accept_len;
  2496. /* Safe use of sprintf */
  2497. sprintf(accept.ptr, "%s/%s", generator->type, generator->subtype);
  2498. pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(), &pubsub_module,
  2499. PJSIP_H_ACCEPT, NULL, 1, &accept);
  2500. return 0;
  2501. }
  2502. void ast_sip_pubsub_unregister_body_generator(struct ast_sip_pubsub_body_generator *generator)
  2503. {
  2504. struct ast_sip_pubsub_body_generator *iter;
  2505. SCOPED_LOCK(lock, &body_generators, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
  2506. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&body_generators, iter, list) {
  2507. if (iter == generator) {
  2508. AST_LIST_REMOVE_CURRENT(list);
  2509. break;
  2510. }
  2511. }
  2512. AST_RWLIST_TRAVERSE_SAFE_END;
  2513. }
  2514. int ast_sip_pubsub_register_body_supplement(struct ast_sip_pubsub_body_supplement *supplement)
  2515. {
  2516. AST_RWLIST_WRLOCK(&body_supplements);
  2517. AST_RWLIST_INSERT_TAIL(&body_supplements, supplement, list);
  2518. AST_RWLIST_UNLOCK(&body_supplements);
  2519. return 0;
  2520. }
  2521. void ast_sip_pubsub_unregister_body_supplement(struct ast_sip_pubsub_body_supplement *supplement)
  2522. {
  2523. struct ast_sip_pubsub_body_supplement *iter;
  2524. SCOPED_LOCK(lock, &body_supplements, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
  2525. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&body_supplements, iter, list) {
  2526. if (iter == supplement) {
  2527. AST_LIST_REMOVE_CURRENT(list);
  2528. break;
  2529. }
  2530. }
  2531. AST_RWLIST_TRAVERSE_SAFE_END;
  2532. }
  2533. const char *ast_sip_subscription_get_body_type(struct ast_sip_subscription *sub)
  2534. {
  2535. return sub->body_generator->type;
  2536. }
  2537. const char *ast_sip_subscription_get_body_subtype(struct ast_sip_subscription *sub)
  2538. {
  2539. return sub->body_generator->subtype;
  2540. }
  2541. int ast_sip_pubsub_generate_body_content(const char *type, const char *subtype,
  2542. struct ast_sip_body_data *data, struct ast_str **str)
  2543. {
  2544. struct ast_sip_pubsub_body_supplement *supplement;
  2545. struct ast_sip_pubsub_body_generator *generator;
  2546. int res = 0;
  2547. void *body;
  2548. generator = find_body_generator_type_subtype(type, subtype);
  2549. if (!generator) {
  2550. ast_log(LOG_WARNING, "Unable to find a body generator for %s/%s\n",
  2551. type, subtype);
  2552. return -1;
  2553. }
  2554. if (strcmp(data->body_type, generator->body_type)) {
  2555. ast_log(LOG_WARNING, "Body generator does not accept the type of data provided\n");
  2556. return -1;
  2557. }
  2558. body = generator->allocate_body(data->body_data);
  2559. if (!body) {
  2560. ast_log(LOG_WARNING, "Unable to allocate a NOTIFY body of type %s/%s\n",
  2561. type, subtype);
  2562. return -1;
  2563. }
  2564. if (generator->generate_body_content(body, data->body_data)) {
  2565. res = -1;
  2566. goto end;
  2567. }
  2568. AST_RWLIST_RDLOCK(&body_supplements);
  2569. AST_RWLIST_TRAVERSE(&body_supplements, supplement, list) {
  2570. if (!strcmp(generator->type, supplement->type) &&
  2571. !strcmp(generator->subtype, supplement->subtype)) {
  2572. res = supplement->supplement_body(body, data->body_data);
  2573. if (res) {
  2574. break;
  2575. }
  2576. }
  2577. }
  2578. AST_RWLIST_UNLOCK(&body_supplements);
  2579. if (!res) {
  2580. generator->to_string(body, str);
  2581. }
  2582. end:
  2583. if (generator->destroy_body) {
  2584. generator->destroy_body(body);
  2585. }
  2586. return res;
  2587. }
  2588. static pj_bool_t pubsub_on_rx_request(pjsip_rx_data *rdata)
  2589. {
  2590. if (!pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, pjsip_get_subscribe_method())) {
  2591. return pubsub_on_rx_subscribe_request(rdata);
  2592. } else if (!pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_publish_method)) {
  2593. return pubsub_on_rx_publish_request(rdata);
  2594. }
  2595. return PJ_FALSE;
  2596. }
  2597. static void pubsub_on_evsub_state(pjsip_evsub *evsub, pjsip_event *event)
  2598. {
  2599. struct sip_subscription_tree *sub_tree;
  2600. if (pjsip_evsub_get_state(evsub) != PJSIP_EVSUB_STATE_TERMINATED) {
  2601. return;
  2602. }
  2603. sub_tree = pjsip_evsub_get_mod_data(evsub, pubsub_module.id);
  2604. if (!sub_tree) {
  2605. return;
  2606. }
  2607. ao2_cleanup(sub_tree);
  2608. pjsip_evsub_set_mod_data(evsub, pubsub_module.id, NULL);
  2609. }
  2610. static void set_state_terminated(struct ast_sip_subscription *sub)
  2611. {
  2612. int i;
  2613. sub->subscription_state = PJSIP_EVSUB_STATE_TERMINATED;
  2614. for (i = 0; i < AST_VECTOR_SIZE(&sub->children); ++i) {
  2615. set_state_terminated(AST_VECTOR_GET(&sub->children, i));
  2616. }
  2617. }
  2618. static void pubsub_on_rx_refresh(pjsip_evsub *evsub, pjsip_rx_data *rdata,
  2619. int *p_st_code, pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body)
  2620. {
  2621. struct sip_subscription_tree *sub_tree;
  2622. sub_tree = pjsip_evsub_get_mod_data(evsub, pubsub_module.id);
  2623. if (!sub_tree) {
  2624. return;
  2625. }
  2626. /* If sending a NOTIFY to terminate a subscription, then pubsub_on_evsub_state()
  2627. * will be called when we send the NOTIFY, and that will result in dropping the
  2628. * refcount of sub_tree by one, and possibly destroying the sub_tree. We need to
  2629. * hold a reference to the sub_tree until this function returns so that we don't
  2630. * try to read from or write to freed memory by accident
  2631. */
  2632. ao2_ref(sub_tree, +1);
  2633. if (pjsip_evsub_get_state(evsub) == PJSIP_EVSUB_STATE_TERMINATED) {
  2634. set_state_terminated(sub_tree->root);
  2635. }
  2636. if (send_notify(sub_tree, 1)) {
  2637. *p_st_code = 500;
  2638. }
  2639. ast_test_suite_event_notify(sub_tree->root->subscription_state == PJSIP_EVSUB_STATE_TERMINATED ?
  2640. "SUBSCRIPTION_TERMINATED" : "SUBSCRIPTION_REFRESHED",
  2641. "Resource: %s", sub_tree->root->resource);
  2642. if (sub_tree->is_list) {
  2643. pj_list_insert_before(res_hdr, create_require_eventlist(rdata->tp_info.pool));
  2644. }
  2645. ao2_ref(sub_tree, -1);
  2646. }
  2647. static void pubsub_on_rx_notify(pjsip_evsub *evsub, pjsip_rx_data *rdata, int *p_st_code,
  2648. pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body)
  2649. {
  2650. struct ast_sip_subscription *sub = pjsip_evsub_get_mod_data(evsub, pubsub_module.id);
  2651. if (!sub) {
  2652. return;
  2653. }
  2654. sub->handler->subscriber->state_change(sub, rdata->msg_info.msg->body,
  2655. pjsip_evsub_get_state(evsub));
  2656. }
  2657. static int serialized_pubsub_on_client_refresh(void *userdata)
  2658. {
  2659. struct sip_subscription_tree *sub_tree = userdata;
  2660. pjsip_tx_data *tdata;
  2661. if (pjsip_evsub_initiate(sub_tree->evsub, NULL, -1, &tdata) == PJ_SUCCESS) {
  2662. pjsip_evsub_send_request(sub_tree->evsub, tdata);
  2663. } else {
  2664. pjsip_evsub_terminate(sub_tree->evsub, PJ_TRUE);
  2665. return 0;
  2666. }
  2667. ao2_cleanup(sub_tree);
  2668. return 0;
  2669. }
  2670. static void pubsub_on_client_refresh(pjsip_evsub *evsub)
  2671. {
  2672. struct sip_subscription_tree *sub_tree = pjsip_evsub_get_mod_data(evsub, pubsub_module.id);
  2673. ao2_ref(sub_tree, +1);
  2674. ast_sip_push_task(sub_tree->serializer, serialized_pubsub_on_client_refresh, sub_tree);
  2675. }
  2676. static int serialized_pubsub_on_server_timeout(void *userdata)
  2677. {
  2678. struct sip_subscription_tree *sub_tree = userdata;
  2679. set_state_terminated(sub_tree->root);
  2680. send_notify(sub_tree, 1);
  2681. ast_test_suite_event_notify("SUBSCRIPTION_TERMINATED",
  2682. "Resource: %s",
  2683. sub_tree->root->resource);
  2684. ao2_cleanup(sub_tree);
  2685. return 0;
  2686. }
  2687. static void pubsub_on_server_timeout(pjsip_evsub *evsub)
  2688. {
  2689. struct sip_subscription_tree *sub_tree = pjsip_evsub_get_mod_data(evsub, pubsub_module.id);
  2690. if (!sub_tree) {
  2691. /* if a subscription has been terminated and the subscription
  2692. timeout/expires is less than the time it takes for all pending
  2693. transactions to end then the subscription timer will not have
  2694. been canceled yet and sub will be null, so do nothing since
  2695. the subscription has already been terminated. */
  2696. return;
  2697. }
  2698. ao2_ref(sub_tree, +1);
  2699. ast_sip_push_task(sub_tree->serializer, serialized_pubsub_on_server_timeout, sub_tree);
  2700. }
  2701. static int ami_subscription_detail(struct sip_subscription_tree *sub_tree,
  2702. struct ast_sip_ami *ami,
  2703. const char *event)
  2704. {
  2705. RAII_VAR(struct ast_str *, buf,
  2706. ast_sip_create_ami_event(event, ami), ast_free);
  2707. if (!buf) {
  2708. return -1;
  2709. }
  2710. sip_subscription_to_ami(sub_tree, &buf);
  2711. astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
  2712. return 0;
  2713. }
  2714. static int ami_subscription_detail_inbound(struct sip_subscription_tree *sub_tree, void *arg)
  2715. {
  2716. return sub_tree->role == AST_SIP_NOTIFIER ? ami_subscription_detail(
  2717. sub_tree, arg, "InboundSubscriptionDetail") : 0;
  2718. }
  2719. static int ami_subscription_detail_outbound(struct sip_subscription_tree *sub_tree, void *arg)
  2720. {
  2721. return sub_tree->role == AST_SIP_SUBSCRIBER ? ami_subscription_detail(
  2722. sub_tree, arg, "OutboundSubscriptionDetail") : 0;
  2723. }
  2724. static int ami_show_subscriptions_inbound(struct mansession *s, const struct message *m)
  2725. {
  2726. struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"), };
  2727. int num;
  2728. astman_send_listack(s, m, "Following are Events for "
  2729. "each inbound Subscription", "start");
  2730. num = for_each_subscription(ami_subscription_detail_inbound, &ami);
  2731. astman_append(s, "Event: InboundSubscriptionDetailComplete\r\n");
  2732. if (!ast_strlen_zero(ami.action_id)) {
  2733. astman_append(s, "ActionID: %s\r\n", ami.action_id);
  2734. }
  2735. astman_append(s, "EventList: Complete\r\n"
  2736. "ListItems: %d\r\n\r\n", num);
  2737. return 0;
  2738. }
  2739. static int ami_show_subscriptions_outbound(struct mansession *s, const struct message *m)
  2740. {
  2741. struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"), };
  2742. int num;
  2743. astman_send_listack(s, m, "Following are Events for "
  2744. "each outbound Subscription", "start");
  2745. num = for_each_subscription(ami_subscription_detail_outbound, &ami);
  2746. astman_append(s, "Event: OutboundSubscriptionDetailComplete\r\n");
  2747. if (!ast_strlen_zero(ami.action_id)) {
  2748. astman_append(s, "ActionID: %s\r\n", ami.action_id);
  2749. }
  2750. astman_append(s, "EventList: Complete\r\n"
  2751. "ListItems: %d\r\n\r\n", num);
  2752. return 0;
  2753. }
  2754. static int format_ami_resource_lists(void *obj, void *arg, int flags)
  2755. {
  2756. struct resource_list *list = obj;
  2757. struct ast_sip_ami *ami = arg;
  2758. struct ast_str *buf;
  2759. buf = ast_sip_create_ami_event("ResourceListDetail", ami);
  2760. if (!buf) {
  2761. return CMP_STOP;
  2762. }
  2763. if (ast_sip_sorcery_object_to_ami(list, &buf)) {
  2764. ast_free(buf);
  2765. return CMP_STOP;
  2766. }
  2767. astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
  2768. ast_free(buf);
  2769. return 0;
  2770. }
  2771. static int ami_show_resource_lists(struct mansession *s, const struct message *m)
  2772. {
  2773. struct ast_sip_ami ami = { .s = s, .m = m };
  2774. int num;
  2775. struct ao2_container *lists;
  2776. lists = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "resource_list",
  2777. AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
  2778. if (!lists || !(num = ao2_container_count(lists))) {
  2779. astman_send_error(s, m, "No resource lists found\n");
  2780. return 0;
  2781. }
  2782. astman_send_listack(s, m, "A listing of resource lists follows, "
  2783. "presented as ResourceListDetail events", "start");
  2784. ao2_callback(lists, OBJ_NODATA, format_ami_resource_lists, &ami);
  2785. astman_append(s,
  2786. "Event: ResourceListDetailComplete\r\n"
  2787. "EventList: Complete\r\n"
  2788. "ListItems: %d\r\n\r\n", num);
  2789. return 0;
  2790. }
  2791. #define AMI_SHOW_SUBSCRIPTIONS_INBOUND "PJSIPShowSubscriptionsInbound"
  2792. #define AMI_SHOW_SUBSCRIPTIONS_OUTBOUND "PJSIPShowSubscriptionsOutbound"
  2793. static int persistence_endpoint_str2struct(const struct aco_option *opt, struct ast_variable *var, void *obj)
  2794. {
  2795. struct subscription_persistence *persistence = obj;
  2796. persistence->endpoint = ast_strdup(var->value);
  2797. return 0;
  2798. }
  2799. static int persistence_endpoint_struct2str(const void *obj, const intptr_t *args, char **buf)
  2800. {
  2801. const struct subscription_persistence *persistence = obj;
  2802. *buf = ast_strdup(persistence->endpoint);
  2803. return 0;
  2804. }
  2805. static int persistence_tag_str2struct(const struct aco_option *opt, struct ast_variable *var, void *obj)
  2806. {
  2807. struct subscription_persistence *persistence = obj;
  2808. persistence->tag = ast_strdup(var->value);
  2809. return 0;
  2810. }
  2811. static int persistence_tag_struct2str(const void *obj, const intptr_t *args, char **buf)
  2812. {
  2813. const struct subscription_persistence *persistence = obj;
  2814. *buf = ast_strdup(persistence->tag);
  2815. return 0;
  2816. }
  2817. static int persistence_expires_str2struct(const struct aco_option *opt, struct ast_variable *var, void *obj)
  2818. {
  2819. struct subscription_persistence *persistence = obj;
  2820. return ast_get_timeval(var->value, &persistence->expires, ast_tv(0, 0), NULL);
  2821. }
  2822. static int persistence_expires_struct2str(const void *obj, const intptr_t *args, char **buf)
  2823. {
  2824. const struct subscription_persistence *persistence = obj;
  2825. return (ast_asprintf(buf, "%ld", persistence->expires.tv_sec) < 0) ? -1 : 0;
  2826. }
  2827. #define RESOURCE_LIST_INIT_SIZE 4
  2828. static void resource_list_destructor(void *obj)
  2829. {
  2830. struct resource_list *list = obj;
  2831. int i;
  2832. for (i = 0; i < AST_VECTOR_SIZE(&list->items); ++i) {
  2833. ast_free((char *) AST_VECTOR_GET(&list->items, i));
  2834. }
  2835. AST_VECTOR_FREE(&list->items);
  2836. }
  2837. static void *resource_list_alloc(const char *name)
  2838. {
  2839. struct resource_list *list;
  2840. list = ast_sorcery_generic_alloc(sizeof(*list), resource_list_destructor);
  2841. if (!list) {
  2842. return NULL;
  2843. }
  2844. if (AST_VECTOR_INIT(&list->items, RESOURCE_LIST_INIT_SIZE)) {
  2845. ao2_cleanup(list);
  2846. return NULL;
  2847. }
  2848. return list;
  2849. }
  2850. static int item_in_vector(const struct resource_list *list, const char *item)
  2851. {
  2852. int i;
  2853. for (i = 0; i < AST_VECTOR_SIZE(&list->items); ++i) {
  2854. if (!strcmp(item, AST_VECTOR_GET(&list->items, i))) {
  2855. return 1;
  2856. }
  2857. }
  2858. return 0;
  2859. }
  2860. static int list_item_handler(const struct aco_option *opt,
  2861. struct ast_variable *var, void *obj)
  2862. {
  2863. struct resource_list *list = obj;
  2864. char *items = ast_strdupa(var->value);
  2865. char *item;
  2866. while ((item = strsep(&items, ","))) {
  2867. if (item_in_vector(list, item)) {
  2868. ast_log(LOG_WARNING, "Ignoring duplicated list item '%s'\n", item);
  2869. continue;
  2870. }
  2871. if (AST_VECTOR_APPEND(&list->items, ast_strdup(item))) {
  2872. return -1;
  2873. }
  2874. }
  2875. return 0;
  2876. }
  2877. static int list_item_to_str(const void *obj, const intptr_t *args, char **buf)
  2878. {
  2879. const struct resource_list *list = obj;
  2880. int i;
  2881. struct ast_str *str = ast_str_create(32);
  2882. for (i = 0; i < AST_VECTOR_SIZE(&list->items); ++i) {
  2883. ast_str_append(&str, 0, "%s,", AST_VECTOR_GET(&list->items, i));
  2884. }
  2885. /* Chop off trailing comma */
  2886. ast_str_truncate(str, -1);
  2887. *buf = ast_strdup(ast_str_buffer(str));
  2888. ast_free(str);
  2889. return 0;
  2890. }
  2891. static int resource_list_apply_handler(const struct ast_sorcery *sorcery, void *obj)
  2892. {
  2893. struct resource_list *list = obj;
  2894. if (ast_strlen_zero(list->event)) {
  2895. ast_log(LOG_WARNING, "Resource list '%s' has no event set\n",
  2896. ast_sorcery_object_get_id(list));
  2897. return -1;
  2898. }
  2899. if (AST_VECTOR_SIZE(&list->items) == 0) {
  2900. ast_log(LOG_WARNING, "Resource list '%s' has no list items\n",
  2901. ast_sorcery_object_get_id(list));
  2902. return -1;
  2903. }
  2904. return 0;
  2905. }
  2906. static int apply_list_configuration(struct ast_sorcery *sorcery)
  2907. {
  2908. ast_sorcery_apply_default(sorcery, "resource_list", "config",
  2909. "pjsip.conf,criteria=type=resource_list");
  2910. if (ast_sorcery_object_register(sorcery, "resource_list", resource_list_alloc,
  2911. NULL, resource_list_apply_handler)) {
  2912. return -1;
  2913. }
  2914. ast_sorcery_object_field_register(sorcery, "resource_list", "type", "",
  2915. OPT_NOOP_T, 0, 0);
  2916. ast_sorcery_object_field_register(sorcery, "resource_list", "event", "",
  2917. OPT_CHAR_ARRAY_T, 1, CHARFLDSET(struct resource_list, event));
  2918. ast_sorcery_object_field_register(sorcery, "resource_list", "full_state", "no",
  2919. OPT_BOOL_T, 1, FLDSET(struct resource_list, full_state));
  2920. ast_sorcery_object_field_register(sorcery, "resource_list", "notification_batch_interval",
  2921. "0", OPT_UINT_T, 0, FLDSET(struct resource_list, notification_batch_interval));
  2922. ast_sorcery_object_field_register_custom(sorcery, "resource_list", "list_item",
  2923. "", list_item_handler, list_item_to_str, NULL, 0, 0);
  2924. ast_sorcery_reload_object(sorcery, "resource_list");
  2925. return 0;
  2926. }
  2927. #ifdef TEST_FRAMEWORK
  2928. /*!
  2929. * \brief "bad" resources
  2930. *
  2931. * These are resources that the test handler will reject subscriptions to.
  2932. */
  2933. const char *bad_resources[] = {
  2934. "coconut",
  2935. "cilantro",
  2936. "olive",
  2937. "cheese",
  2938. };
  2939. /*!
  2940. * \brief new_subscribe callback for unit tests
  2941. *
  2942. * Will give a 200 OK response to any resource except the "bad" ones.
  2943. */
  2944. static int test_new_subscribe(struct ast_sip_endpoint *endpoint, const char *resource)
  2945. {
  2946. int i;
  2947. for (i = 0; i < ARRAY_LEN(bad_resources); ++i) {
  2948. if (!strcmp(resource, bad_resources[i])) {
  2949. return 400;
  2950. }
  2951. }
  2952. return 200;
  2953. }
  2954. /*!
  2955. * \brief Subscription notifier for unit tests.
  2956. *
  2957. * Since unit tests are only concerned with building a resource tree,
  2958. * only the new_subscribe callback needs to be defined.
  2959. */
  2960. struct ast_sip_notifier test_notifier = {
  2961. .new_subscribe = test_new_subscribe,
  2962. };
  2963. /*!
  2964. * \brief Subscription handler for unit tests.
  2965. */
  2966. struct ast_sip_subscription_handler test_handler = {
  2967. .event_name = "test",
  2968. .notifier = &test_notifier,
  2969. };
  2970. /*!
  2971. * \brief Set properties on an allocated resource list
  2972. *
  2973. * \param list The list to set details on.
  2974. * \param event The list's event.
  2975. * \param resources Array of resources to add to the list.
  2976. * \param num_resources Number of resources in the array.
  2977. * \retval 0 Success
  2978. * \retval non-zero Failure
  2979. */
  2980. static int populate_list(struct resource_list *list, const char *event, const char **resources, size_t num_resources)
  2981. {
  2982. int i;
  2983. ast_copy_string(list->event, event, sizeof(list->event));
  2984. for (i = 0; i < num_resources; ++i) {
  2985. if (AST_VECTOR_APPEND(&list->items, ast_strdup(resources[i]))) {
  2986. return -1;
  2987. }
  2988. }
  2989. return 0;
  2990. }
  2991. /*!
  2992. * \brief RAII callback to destroy a resource list
  2993. */
  2994. static void cleanup_resource_list(struct resource_list *list)
  2995. {
  2996. if (!list) {
  2997. return;
  2998. }
  2999. ast_sorcery_delete(ast_sip_get_sorcery(), list);
  3000. ao2_cleanup(list);
  3001. }
  3002. /*!
  3003. * \brief allocate a resource list, store it in sorcery, and set its details
  3004. *
  3005. * \param test The unit test. Used for logging status messages.
  3006. * \param list_name The name of the list to create.
  3007. * \param event The event the list services
  3008. * \param resources Array of resources to apply to the list
  3009. * \param num_resources The number of resources in the array
  3010. * \retval NULL Failed to allocate or populate list
  3011. * \retval non-NULL The created list
  3012. */
  3013. static struct resource_list *create_resource_list(struct ast_test *test,
  3014. const char *list_name, const char *event, const char **resources, size_t num_resources)
  3015. {
  3016. struct resource_list *list;
  3017. list = ast_sorcery_alloc(ast_sip_get_sorcery(), "resource_list", list_name);
  3018. if (!list) {
  3019. ast_test_status_update(test, "Could not allocate resource list in sorcery\n");
  3020. return NULL;
  3021. }
  3022. if (ast_sorcery_create(ast_sip_get_sorcery(), list)) {
  3023. ast_test_status_update(test, "Could not store the resource list in sorcery\n");
  3024. ao2_cleanup(list);
  3025. return NULL;
  3026. }
  3027. if (populate_list(list, event, resources, num_resources)) {
  3028. ast_test_status_update(test, "Could not add resources to the resource list\n");
  3029. cleanup_resource_list(list);
  3030. return NULL;
  3031. }
  3032. return list;
  3033. }
  3034. /*!
  3035. * \brief Check the integrity of a tree node against a set of resources.
  3036. *
  3037. * The tree node's resources must be in the same order as the resources in
  3038. * the supplied resources array. Because of this constraint, tests can misrepresent
  3039. * the size of the resources array as being smaller than it really is if resources
  3040. * at the end of the array should not be present in the tree node.
  3041. *
  3042. * \param test The unit test. Used for printing status messages.
  3043. * \param node The constructed tree node whose integrity is under question.
  3044. * \param resources Array of expected resource values
  3045. * \param num_resources The number of resources to check in the array.
  3046. */
  3047. static int check_node(struct ast_test *test, struct tree_node *node,
  3048. const char **resources, size_t num_resources)
  3049. {
  3050. int i;
  3051. if (AST_VECTOR_SIZE(&node->children) != num_resources) {
  3052. ast_test_status_update(test, "Unexpected number of resources in tree. Expected %zu, got %zu\n",
  3053. num_resources, AST_VECTOR_SIZE(&node->children));
  3054. return -1;
  3055. }
  3056. for (i = 0; i < num_resources; ++i) {
  3057. if (strcmp(resources[i], AST_VECTOR_GET(&node->children, i)->resource)) {
  3058. ast_test_status_update(test, "Mismatched resources. Expected '%s' but got '%s'\n",
  3059. resources[i], AST_VECTOR_GET(&node->children, i)->resource);
  3060. return -1;
  3061. }
  3062. }
  3063. return 0;
  3064. }
  3065. /*!
  3066. * \brief RAII_VAR callback to destroy an allocated resource tree
  3067. */
  3068. static void test_resource_tree_destroy(struct resource_tree *tree)
  3069. {
  3070. resource_tree_destroy(tree);
  3071. ast_free(tree);
  3072. }
  3073. static int ineligible_configuration(void)
  3074. {
  3075. struct ast_config *config;
  3076. struct ast_flags flags = {0,};
  3077. const char *value;
  3078. config = ast_config_load("sorcery.conf", flags);
  3079. if (!config) {
  3080. return 1;
  3081. }
  3082. value = ast_variable_retrieve(config, "res_pjsip_pubsub", "resource_list");
  3083. if (ast_strlen_zero(value)) {
  3084. ast_config_destroy(config);
  3085. return 1;
  3086. }
  3087. if (strcasecmp(value, "memory") && strcasecmp(value, "astdb")) {
  3088. ast_config_destroy(config);
  3089. return 1;
  3090. }
  3091. return 0;
  3092. }
  3093. AST_TEST_DEFINE(resource_tree)
  3094. {
  3095. RAII_VAR(struct resource_list *, list, NULL, cleanup_resource_list);
  3096. RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy);
  3097. const char *resources[] = {
  3098. "huey",
  3099. "dewey",
  3100. "louie",
  3101. };
  3102. int resp;
  3103. switch (cmd) {
  3104. case TEST_INIT:
  3105. info->name = "resource_tree";
  3106. info->category = "/res/res_pjsip_pubsub/";
  3107. info->summary = "Basic resource tree integrity check";
  3108. info->description =
  3109. "Create a resource list and ensure that our attempt to build a tree works as expected.";
  3110. return AST_TEST_NOT_RUN;
  3111. case TEST_EXECUTE:
  3112. break;
  3113. }
  3114. if (ineligible_configuration()) {
  3115. ast_test_status_update(test, "Ineligible configuration for this test. Please add a "
  3116. "'res_pjsip_pubsub' section to sorcery.conf, and set 'resource_list=memory'\n");
  3117. return AST_TEST_NOT_RUN;
  3118. }
  3119. list = create_resource_list(test, "foo", "test", resources, ARRAY_LEN(resources));
  3120. if (!list) {
  3121. return AST_TEST_FAIL;
  3122. }
  3123. tree = ast_calloc(1, sizeof(*tree));
  3124. resp = build_resource_tree(NULL, &test_handler, "foo", tree, 1);
  3125. if (resp != 200) {
  3126. ast_test_status_update(test, "Unexpected response %d when building resource tree\n", resp);
  3127. return AST_TEST_FAIL;
  3128. }
  3129. if (!tree->root) {
  3130. ast_test_status_update(test, "Resource tree has no root\n");
  3131. return AST_TEST_FAIL;
  3132. }
  3133. if (check_node(test, tree->root, resources, ARRAY_LEN(resources))) {
  3134. return AST_TEST_FAIL;
  3135. }
  3136. return AST_TEST_PASS;
  3137. }
  3138. AST_TEST_DEFINE(complex_resource_tree)
  3139. {
  3140. RAII_VAR(struct resource_list *, list_1, NULL, cleanup_resource_list);
  3141. RAII_VAR(struct resource_list *, list_2, NULL, cleanup_resource_list);
  3142. RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy);
  3143. const char *resources_1[] = {
  3144. "huey",
  3145. "dewey",
  3146. "louie",
  3147. "dwarves",
  3148. };
  3149. const char *resources_2[] = {
  3150. "happy",
  3151. "grumpy",
  3152. "doc",
  3153. "bashful",
  3154. "dopey",
  3155. "sneezy",
  3156. "sleepy",
  3157. };
  3158. int resp;
  3159. struct tree_node *node;
  3160. switch (cmd) {
  3161. case TEST_INIT:
  3162. info->name = "complex_resource_tree";
  3163. info->category = "/res/res_pjsip_pubsub/";
  3164. info->summary = "Complex resource tree integrity check";
  3165. info->description =
  3166. "Create a complex resource list and ensure that our attempt to build a tree works as expected.";
  3167. return AST_TEST_NOT_RUN;
  3168. case TEST_EXECUTE:
  3169. break;
  3170. }
  3171. if (ineligible_configuration()) {
  3172. ast_test_status_update(test, "Ineligible configuration for this test. Please add a "
  3173. "'res_pjsip_pubsub' section to sorcery.conf, and set 'resource_list=memory'\n");
  3174. return AST_TEST_NOT_RUN;
  3175. }
  3176. list_1 = create_resource_list(test, "foo", "test", resources_1, ARRAY_LEN(resources_1));
  3177. if (!list_1) {
  3178. return AST_TEST_FAIL;
  3179. }
  3180. list_2 = create_resource_list(test, "dwarves", "test", resources_2, ARRAY_LEN(resources_2));
  3181. if (!list_2) {
  3182. return AST_TEST_FAIL;
  3183. }
  3184. tree = ast_calloc(1, sizeof(*tree));
  3185. resp = build_resource_tree(NULL, &test_handler, "foo", tree, 1);
  3186. if (resp != 200) {
  3187. ast_test_status_update(test, "Unexpected response %d when building resource tree\n", resp);
  3188. return AST_TEST_FAIL;
  3189. }
  3190. if (!tree->root) {
  3191. ast_test_status_update(test, "Resource tree has no root\n");
  3192. return AST_TEST_FAIL;
  3193. }
  3194. node = tree->root;
  3195. if (check_node(test, node, resources_1, ARRAY_LEN(resources_1))) {
  3196. return AST_TEST_FAIL;
  3197. }
  3198. /* The embedded list is at index 3 in the root node's children */
  3199. node = AST_VECTOR_GET(&node->children, 3);
  3200. if (check_node(test, node, resources_2, ARRAY_LEN(resources_2))) {
  3201. return AST_TEST_FAIL;
  3202. }
  3203. return AST_TEST_PASS;
  3204. }
  3205. AST_TEST_DEFINE(bad_resource)
  3206. {
  3207. RAII_VAR(struct resource_list *, list, NULL, cleanup_resource_list);
  3208. RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy);
  3209. const char *resources[] = {
  3210. "huey",
  3211. "dewey",
  3212. "louie",
  3213. "coconut", /* A "bad" resource */
  3214. };
  3215. int resp;
  3216. switch (cmd) {
  3217. case TEST_INIT:
  3218. info->name = "bad_resource";
  3219. info->category = "/res/res_pjsip_pubsub/";
  3220. info->summary = "Ensure bad resources do not end up in the tree";
  3221. info->description =
  3222. "Create a resource list with a single bad resource. Ensure the bad resource does not end up in the tree.";
  3223. return AST_TEST_NOT_RUN;
  3224. case TEST_EXECUTE:
  3225. break;
  3226. }
  3227. if (ineligible_configuration()) {
  3228. ast_test_status_update(test, "Ineligible configuration for this test. Please add a "
  3229. "'res_pjsip_pubsub' section to sorcery.conf, and set 'resource_list=memory'\n");
  3230. return AST_TEST_NOT_RUN;
  3231. }
  3232. list = create_resource_list(test, "foo", "test", resources, ARRAY_LEN(resources));
  3233. if (!list) {
  3234. return AST_TEST_FAIL;
  3235. }
  3236. tree = ast_calloc(1, sizeof(*tree));
  3237. resp = build_resource_tree(NULL, &test_handler, "foo", tree, 1);
  3238. if (resp != 200) {
  3239. ast_test_status_update(test, "Unexpected response %d when building resource tree\n", resp);
  3240. return AST_TEST_FAIL;
  3241. }
  3242. if (!tree->root) {
  3243. ast_test_status_update(test, "Resource tree has no root\n");
  3244. return AST_TEST_FAIL;
  3245. }
  3246. /* We check against all but the final resource since we expect it not to be in the tree */
  3247. if (check_node(test, tree->root, resources, ARRAY_LEN(resources) - 1)) {
  3248. return AST_TEST_FAIL;
  3249. }
  3250. return AST_TEST_PASS;
  3251. }
  3252. AST_TEST_DEFINE(bad_branch)
  3253. {
  3254. RAII_VAR(struct resource_list *, list_1, NULL, cleanup_resource_list);
  3255. RAII_VAR(struct resource_list *, list_2, NULL, cleanup_resource_list);
  3256. RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy);
  3257. const char *resources_1[] = {
  3258. "huey",
  3259. "dewey",
  3260. "louie",
  3261. "gross",
  3262. };
  3263. /* This list has nothing but bad resources */
  3264. const char *resources_2[] = {
  3265. "coconut",
  3266. "cilantro",
  3267. "olive",
  3268. "cheese",
  3269. };
  3270. int resp;
  3271. switch (cmd) {
  3272. case TEST_INIT:
  3273. info->name = "bad_branch";
  3274. info->category = "/res/res_pjsip_pubsub/";
  3275. info->summary = "Ensure bad branches are pruned from the tree";
  3276. info->description =
  3277. "Create a resource list that makes a tree with an entire branch of bad resources.\n"
  3278. "Ensure the bad branch is pruned from the tree.";
  3279. return AST_TEST_NOT_RUN;
  3280. case TEST_EXECUTE:
  3281. break;
  3282. }
  3283. if (ineligible_configuration()) {
  3284. ast_test_status_update(test, "Ineligible configuration for this test. Please add a "
  3285. "'res_pjsip_pubsub' section to sorcery.conf, and set 'resource_list=memory'\n");
  3286. return AST_TEST_NOT_RUN;
  3287. }
  3288. list_1 = create_resource_list(test, "foo", "test", resources_1, ARRAY_LEN(resources_1));
  3289. if (!list_1) {
  3290. return AST_TEST_FAIL;
  3291. }
  3292. list_2 = create_resource_list(test, "gross", "test", resources_2, ARRAY_LEN(resources_2));
  3293. if (!list_2) {
  3294. return AST_TEST_FAIL;
  3295. }
  3296. tree = ast_calloc(1, sizeof(*tree));
  3297. resp = build_resource_tree(NULL, &test_handler, "foo", tree, 1);
  3298. if (resp != 200) {
  3299. ast_test_status_update(test, "Unexpected response %d when building resource tree\n", resp);
  3300. return AST_TEST_FAIL;
  3301. }
  3302. if (!tree->root) {
  3303. ast_test_status_update(test, "Resource tree has no root\n");
  3304. return AST_TEST_FAIL;
  3305. }
  3306. /* We check against all but the final resource of the list since the entire branch should
  3307. * be pruned from the tree
  3308. */
  3309. if (check_node(test, tree->root, resources_1, ARRAY_LEN(resources_1) - 1)) {
  3310. return AST_TEST_FAIL;
  3311. }
  3312. return AST_TEST_PASS;
  3313. }
  3314. AST_TEST_DEFINE(duplicate_resource)
  3315. {
  3316. RAII_VAR(struct resource_list *, list_1, NULL, cleanup_resource_list);
  3317. RAII_VAR(struct resource_list *, list_2, NULL, cleanup_resource_list);
  3318. RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy);
  3319. const char *resources_1[] = {
  3320. "huey",
  3321. "ducks",
  3322. "dewey",
  3323. "louie",
  3324. };
  3325. const char *resources_2[] = {
  3326. "donald",
  3327. "daisy",
  3328. "scrooge",
  3329. "dewey",
  3330. "louie",
  3331. "huey",
  3332. };
  3333. int resp;
  3334. struct tree_node *node;
  3335. switch (cmd) {
  3336. case TEST_INIT:
  3337. info->name = "duplicate_resource";
  3338. info->category = "/res/res_pjsip_pubsub/";
  3339. info->summary = "Ensure duplicated resources do not end up in the tree";
  3340. info->description =
  3341. "Create a resource list with a single duplicated resource. Ensure the duplicated resource does not end up in the tree.";
  3342. return AST_TEST_NOT_RUN;
  3343. case TEST_EXECUTE:
  3344. break;
  3345. }
  3346. if (ineligible_configuration()) {
  3347. ast_test_status_update(test, "Ineligible configuration for this test. Please add a "
  3348. "'res_pjsip_pubsub' section to sorcery.conf, and set 'resource_list=memory'\n");
  3349. return AST_TEST_NOT_RUN;
  3350. }
  3351. list_1 = create_resource_list(test, "foo", "test", resources_1, ARRAY_LEN(resources_1));
  3352. if (!list_1) {
  3353. return AST_TEST_FAIL;
  3354. }
  3355. list_2 = create_resource_list(test, "ducks", "test", resources_2, ARRAY_LEN(resources_2));
  3356. if (!list_2) {
  3357. return AST_TEST_FAIL;
  3358. }
  3359. tree = ast_calloc(1, sizeof(*tree));
  3360. resp = build_resource_tree(NULL, &test_handler, "foo", tree, 1);
  3361. if (resp != 200) {
  3362. ast_test_status_update(test, "Unexpected response %d when building resource tree\n", resp);
  3363. return AST_TEST_FAIL;
  3364. }
  3365. if (!tree->root) {
  3366. ast_test_status_update(test, "Resource tree has no root\n");
  3367. return AST_TEST_FAIL;
  3368. }
  3369. node = tree->root;
  3370. /* This node should have "huey" and "ducks". "dewey" and "louie" should not
  3371. * be present since they were found in the "ducks" list.
  3372. */
  3373. if (check_node(test, node, resources_1, ARRAY_LEN(resources_1) - 2)) {
  3374. return AST_TEST_FAIL;
  3375. }
  3376. /* This node should have "donald", "daisy", "scrooge", "dewey", and "louie".
  3377. * "huey" is not here since that was already encountered in the parent list
  3378. */
  3379. node = AST_VECTOR_GET(&node->children, 1);
  3380. if (check_node(test, node, resources_2, ARRAY_LEN(resources_2) - 1)) {
  3381. return AST_TEST_FAIL;
  3382. }
  3383. return AST_TEST_PASS;
  3384. }
  3385. AST_TEST_DEFINE(loop)
  3386. {
  3387. RAII_VAR(struct resource_list *, list_1, NULL, cleanup_resource_list);
  3388. RAII_VAR(struct resource_list *, list_2, NULL, cleanup_resource_list);
  3389. RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy);
  3390. const char *resources_1[] = {
  3391. "derp",
  3392. };
  3393. const char *resources_2[] = {
  3394. "herp",
  3395. };
  3396. int resp;
  3397. switch (cmd) {
  3398. case TEST_INIT:
  3399. info->name = "loop";
  3400. info->category = "/res/res_pjsip_pubsub/";
  3401. info->summary = "Test that loops are properly detected.";
  3402. info->description =
  3403. "Create two resource lists that refer to each other. Ensure that attempting to build a tree\n"
  3404. "results in an empty tree.";
  3405. return AST_TEST_NOT_RUN;
  3406. case TEST_EXECUTE:
  3407. break;
  3408. }
  3409. if (ineligible_configuration()) {
  3410. ast_test_status_update(test, "Ineligible configuration for this test. Please add a "
  3411. "'res_pjsip_pubsub' section to sorcery.conf, and set 'resource_list=memory'\n");
  3412. return AST_TEST_NOT_RUN;
  3413. }
  3414. list_1 = create_resource_list(test, "herp", "test", resources_1, ARRAY_LEN(resources_1));
  3415. if (!list_1) {
  3416. return AST_TEST_FAIL;
  3417. }
  3418. list_2 = create_resource_list(test, "derp", "test", resources_2, ARRAY_LEN(resources_2));
  3419. if (!list_2) {
  3420. return AST_TEST_FAIL;
  3421. }
  3422. tree = ast_calloc(1, sizeof(*tree));
  3423. resp = build_resource_tree(NULL, &test_handler, "herp", tree, 1);
  3424. if (resp == 200) {
  3425. ast_test_status_update(test, "Unexpected response %d when building resource tree\n", resp);
  3426. return AST_TEST_FAIL;
  3427. }
  3428. return AST_TEST_PASS;
  3429. }
  3430. AST_TEST_DEFINE(bad_event)
  3431. {
  3432. RAII_VAR(struct resource_list *, list, NULL, cleanup_resource_list);
  3433. RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy);
  3434. const char *resources[] = {
  3435. "huey",
  3436. "dewey",
  3437. "louie",
  3438. };
  3439. int resp;
  3440. switch (cmd) {
  3441. case TEST_INIT:
  3442. info->name = "bad_event";
  3443. info->category = "/res/res_pjsip_pubsub/";
  3444. info->summary = "Ensure that list with wrong event specified is not retrieved";
  3445. info->description =
  3446. "Create a simple resource list for event 'tsetse'. Ensure that trying to retrieve the list for event 'test' fails.";
  3447. return AST_TEST_NOT_RUN;
  3448. case TEST_EXECUTE:
  3449. break;
  3450. }
  3451. if (ineligible_configuration()) {
  3452. ast_test_status_update(test, "Ineligible configuration for this test. Please add a "
  3453. "'res_pjsip_pubsub' section to sorcery.conf, and set 'resource_list=memory'\n");
  3454. return AST_TEST_NOT_RUN;
  3455. }
  3456. list = create_resource_list(test, "foo", "tsetse", resources, ARRAY_LEN(resources));
  3457. if (!list) {
  3458. return AST_TEST_FAIL;
  3459. }
  3460. tree = ast_calloc(1, sizeof(*tree));
  3461. /* Since the test_handler is for event "test", this should not build a list, but
  3462. * instead result in a single resource being created, called "foo"
  3463. */
  3464. resp = build_resource_tree(NULL, &test_handler, "foo", tree, 1);
  3465. if (resp != 200) {
  3466. ast_test_status_update(test, "Unexpected response %d when building resource tree\n", resp);
  3467. return AST_TEST_FAIL;
  3468. }
  3469. if (!tree->root) {
  3470. ast_test_status_update(test, "Resource tree has no root\n");
  3471. return AST_TEST_FAIL;
  3472. }
  3473. if (strcmp(tree->root->resource, "foo")) {
  3474. ast_test_status_update(test, "Unexpected resource %s found in tree\n", tree->root->resource);
  3475. return AST_TEST_FAIL;
  3476. }
  3477. return AST_TEST_PASS;
  3478. }
  3479. #endif
  3480. static int resource_endpoint_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  3481. {
  3482. struct ast_sip_publication_resource *resource = obj;
  3483. ast_free(resource->endpoint);
  3484. resource->endpoint = ast_strdup(var->value);
  3485. return 0;
  3486. }
  3487. static int resource_event_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  3488. {
  3489. struct ast_sip_publication_resource *resource = obj;
  3490. /* The event configuration name starts with 'event_' so skip past it to get the real name */
  3491. const char *event = var->name + 6;
  3492. struct ast_variable *item;
  3493. if (ast_strlen_zero(event) || ast_strlen_zero(var->value)) {
  3494. return -1;
  3495. }
  3496. item = ast_variable_new(event, var->value, "");
  3497. if (!item) {
  3498. return -1;
  3499. }
  3500. if (resource->events) {
  3501. item->next = resource->events;
  3502. }
  3503. resource->events = item;
  3504. return 0;
  3505. }
  3506. static int load_module(void)
  3507. {
  3508. static const pj_str_t str_PUBLISH = { "PUBLISH", 7 };
  3509. struct ast_sorcery *sorcery;
  3510. CHECK_PJSIP_MODULE_LOADED();
  3511. sorcery = ast_sip_get_sorcery();
  3512. pjsip_evsub_init_module(ast_sip_get_pjsip_endpoint());
  3513. if (!(sched = ast_sched_context_create())) {
  3514. ast_log(LOG_ERROR, "Could not create scheduler for publication expiration\n");
  3515. return AST_MODULE_LOAD_FAILURE;
  3516. }
  3517. if (ast_sched_start_thread(sched)) {
  3518. ast_log(LOG_ERROR, "Could not start scheduler thread for publication expiration\n");
  3519. ast_sched_context_destroy(sched);
  3520. return AST_MODULE_LOAD_FAILURE;
  3521. }
  3522. pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(), NULL, PJSIP_H_ALLOW, NULL, 1, &str_PUBLISH);
  3523. if (ast_sip_register_service(&pubsub_module)) {
  3524. ast_log(LOG_ERROR, "Could not register pubsub service\n");
  3525. ast_sched_context_destroy(sched);
  3526. return AST_MODULE_LOAD_FAILURE;
  3527. }
  3528. ast_sorcery_apply_config(sorcery, "res_pjsip_pubsub");
  3529. ast_sorcery_apply_default(sorcery, "subscription_persistence", "astdb", "subscription_persistence");
  3530. if (ast_sorcery_object_register(sorcery, "subscription_persistence", subscription_persistence_alloc,
  3531. NULL, NULL)) {
  3532. ast_log(LOG_ERROR, "Could not register subscription persistence object support\n");
  3533. ast_sip_unregister_service(&pubsub_module);
  3534. ast_sched_context_destroy(sched);
  3535. return AST_MODULE_LOAD_FAILURE;
  3536. }
  3537. ast_sorcery_object_field_register(sorcery, "subscription_persistence", "packet", "", OPT_CHAR_ARRAY_T, 0,
  3538. CHARFLDSET(struct subscription_persistence, packet));
  3539. ast_sorcery_object_field_register(sorcery, "subscription_persistence", "src_name", "", OPT_CHAR_ARRAY_T, 0,
  3540. CHARFLDSET(struct subscription_persistence, src_name));
  3541. ast_sorcery_object_field_register(sorcery, "subscription_persistence", "src_port", "0", OPT_UINT_T, 0,
  3542. FLDSET(struct subscription_persistence, src_port));
  3543. ast_sorcery_object_field_register(sorcery, "subscription_persistence", "transport_key", "0", OPT_CHAR_ARRAY_T, 0,
  3544. CHARFLDSET(struct subscription_persistence, transport_key));
  3545. ast_sorcery_object_field_register(sorcery, "subscription_persistence", "local_name", "", OPT_CHAR_ARRAY_T, 0,
  3546. CHARFLDSET(struct subscription_persistence, local_name));
  3547. ast_sorcery_object_field_register(sorcery, "subscription_persistence", "local_port", "0", OPT_UINT_T, 0,
  3548. FLDSET(struct subscription_persistence, local_port));
  3549. ast_sorcery_object_field_register(sorcery, "subscription_persistence", "cseq", "0", OPT_UINT_T, 0,
  3550. FLDSET(struct subscription_persistence, cseq));
  3551. ast_sorcery_object_field_register_custom(sorcery, "subscription_persistence", "endpoint", "",
  3552. persistence_endpoint_str2struct, persistence_endpoint_struct2str, NULL, 0, 0);
  3553. ast_sorcery_object_field_register_custom(sorcery, "subscription_persistence", "tag", "",
  3554. persistence_tag_str2struct, persistence_tag_struct2str, NULL, 0, 0);
  3555. ast_sorcery_object_field_register_custom(sorcery, "subscription_persistence", "expires", "",
  3556. persistence_expires_str2struct, persistence_expires_struct2str, NULL, 0, 0);
  3557. if (apply_list_configuration(sorcery)) {
  3558. ast_sip_unregister_service(&pubsub_module);
  3559. ast_sched_context_destroy(sched);
  3560. }
  3561. ast_sorcery_apply_default(sorcery, "inbound-publication", "config", "pjsip.conf,criteria=type=inbound-publication");
  3562. if (ast_sorcery_object_register(sorcery, "inbound-publication", publication_resource_alloc,
  3563. NULL, NULL)) {
  3564. ast_log(LOG_ERROR, "Could not register subscription persistence object support\n");
  3565. ast_sip_unregister_service(&pubsub_module);
  3566. ast_sched_context_destroy(sched);
  3567. return AST_MODULE_LOAD_FAILURE;
  3568. }
  3569. ast_sorcery_object_field_register(sorcery, "inbound-publication", "type", "", OPT_NOOP_T, 0, 0);
  3570. ast_sorcery_object_field_register_custom(sorcery, "inbound-publication", "endpoint", "",
  3571. resource_endpoint_handler, NULL, NULL, 0, 0);
  3572. ast_sorcery_object_fields_register(sorcery, "inbound-publication", "^event_", resource_event_handler, NULL);
  3573. ast_sorcery_reload_object(sorcery, "inbound-publication");
  3574. if (ast_test_flag(&ast_options, AST_OPT_FLAG_FULLY_BOOTED)) {
  3575. ast_sip_push_task(NULL, subscription_persistence_load, NULL);
  3576. } else {
  3577. stasis_subscribe(ast_manager_get_topic(), subscription_persistence_event_cb, NULL);
  3578. }
  3579. ast_manager_register_xml(AMI_SHOW_SUBSCRIPTIONS_INBOUND, EVENT_FLAG_SYSTEM,
  3580. ami_show_subscriptions_inbound);
  3581. ast_manager_register_xml(AMI_SHOW_SUBSCRIPTIONS_OUTBOUND, EVENT_FLAG_SYSTEM,
  3582. ami_show_subscriptions_outbound);
  3583. ast_manager_register_xml("PJSIPShowResourceLists", EVENT_FLAG_SYSTEM,
  3584. ami_show_resource_lists);
  3585. AST_TEST_REGISTER(resource_tree);
  3586. AST_TEST_REGISTER(complex_resource_tree);
  3587. AST_TEST_REGISTER(bad_resource);
  3588. AST_TEST_REGISTER(bad_branch);
  3589. AST_TEST_REGISTER(duplicate_resource);
  3590. AST_TEST_REGISTER(loop);
  3591. AST_TEST_REGISTER(bad_event);
  3592. return AST_MODULE_LOAD_SUCCESS;
  3593. }
  3594. static int unload_module(void)
  3595. {
  3596. ast_manager_unregister(AMI_SHOW_SUBSCRIPTIONS_OUTBOUND);
  3597. ast_manager_unregister(AMI_SHOW_SUBSCRIPTIONS_INBOUND);
  3598. ast_manager_unregister("PJSIPShowResourceLists");
  3599. if (sched) {
  3600. ast_sched_context_destroy(sched);
  3601. }
  3602. AST_TEST_UNREGISTER(resource_tree);
  3603. AST_TEST_UNREGISTER(complex_resource_tree);
  3604. AST_TEST_UNREGISTER(bad_resource);
  3605. AST_TEST_UNREGISTER(bad_branch);
  3606. AST_TEST_UNREGISTER(duplicate_resource);
  3607. AST_TEST_UNREGISTER(loop);
  3608. AST_TEST_UNREGISTER(bad_event);
  3609. return 0;
  3610. }
  3611. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "PJSIP event resource",
  3612. .support_level = AST_MODULE_SUPPORT_CORE,
  3613. .load = load_module,
  3614. .unload = unload_module,
  3615. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  3616. );