cdr.c 135 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2006, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Call Detail Record API
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \note Includes code and algorithms from the Zapata library.
  25. *
  26. * \note We do a lot of checking here in the CDR code to try to be sure we don't ever let a CDR slip
  27. * through our fingers somehow. If someone allocates a CDR, it must be completely handled normally
  28. * or a WARNING shall be logged, so that we can best keep track of any escape condition where the CDR
  29. * isn't properly generated and posted.
  30. */
  31. /*! \li \ref cdr.c uses the configuration file \ref cdr.conf
  32. * \addtogroup configuration_file Configuration Files
  33. */
  34. /*!
  35. * \page cdr.conf cdr.conf
  36. * \verbinclude cdr.conf.sample
  37. */
  38. /*** MODULEINFO
  39. <support_level>core</support_level>
  40. ***/
  41. #include "asterisk.h"
  42. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  43. #include <signal.h>
  44. #include <inttypes.h>
  45. #include "asterisk/lock.h"
  46. #include "asterisk/channel.h"
  47. #include "asterisk/cdr.h"
  48. #include "asterisk/callerid.h"
  49. #include "asterisk/manager.h"
  50. #include "asterisk/causes.h"
  51. #include "asterisk/linkedlists.h"
  52. #include "asterisk/utils.h"
  53. #include "asterisk/sched.h"
  54. #include "asterisk/config.h"
  55. #include "asterisk/cli.h"
  56. #include "asterisk/stringfields.h"
  57. #include "asterisk/data.h"
  58. #include "asterisk/config_options.h"
  59. #include "asterisk/json.h"
  60. #include "asterisk/parking.h"
  61. #include "asterisk/stasis.h"
  62. #include "asterisk/stasis_channels.h"
  63. #include "asterisk/stasis_bridges.h"
  64. #include "asterisk/stasis_message_router.h"
  65. #include "asterisk/astobj2.h"
  66. /*** DOCUMENTATION
  67. <configInfo name="cdr" language="en_US">
  68. <synopsis>Call Detail Record configuration</synopsis>
  69. <description>
  70. <para>CDR is Call Detail Record, which provides logging services via a variety of
  71. pluggable backend modules. Detailed call information can be recorded to
  72. databases, files, etc. Useful for billing, fraud prevention, compliance with
  73. Sarbanes-Oxley aka The Enron Act, QOS evaluations, and more.</para>
  74. </description>
  75. <configFile name="cdr.conf">
  76. <configObject name="general">
  77. <synopsis>Global settings applied to the CDR engine.</synopsis>
  78. <configOption name="debug">
  79. <synopsis>Enable/disable verbose CDR debugging.</synopsis>
  80. <description><para>When set to <literal>True</literal>, verbose updates
  81. of changes in CDR information will be logged. Note that this is only
  82. of use when debugging CDR behavior.</para>
  83. </description>
  84. </configOption>
  85. <configOption name="enable">
  86. <synopsis>Enable/disable CDR logging.</synopsis>
  87. <description><para>Define whether or not to use CDR logging. Setting this to "no" will override
  88. any loading of backend CDR modules. Default is "yes".</para>
  89. </description>
  90. </configOption>
  91. <configOption name="unanswered">
  92. <synopsis>Log calls that are never answered.</synopsis>
  93. <description><para>Define whether or not to log unanswered calls. Setting this to "yes" will
  94. report every attempt to ring a phone in dialing attempts, when it was not
  95. answered. For example, if you try to dial 3 extensions, and this option is "yes",
  96. you will get 3 CDR's, one for each phone that was rung. Some find this information horribly
  97. useless. Others find it very valuable. Note, in "yes" mode, you will see one CDR, with one of
  98. the call targets on one side, and the originating channel on the other, and then one CDR for
  99. each channel attempted. This may seem redundant, but cannot be helped.</para>
  100. <para>In brief, this option controls the reporting of unanswered calls which only have an A
  101. party. Calls which get offered to an outgoing line, but are unanswered, are still
  102. logged, and that is the intended behavior. (It also results in some B side CDRs being
  103. output, as they have the B side channel as their source channel, and no destination
  104. channel.)</para>
  105. </description>
  106. </configOption>
  107. <configOption name="congestion">
  108. <synopsis>Log congested calls.</synopsis>
  109. <description><para>Define whether or not to log congested calls. Setting this to "yes" will
  110. report each call that fails to complete due to congestion conditions.</para>
  111. </description>
  112. </configOption>
  113. <configOption name="endbeforehexten">
  114. <synopsis>Don't produce CDRs while executing hangup logic</synopsis>
  115. <description>
  116. <para>As each CDR for a channel is finished, its end time is updated
  117. and the CDR is finalized. When a channel is hung up and hangup
  118. logic is present (in the form of a hangup handler or the
  119. <literal>h</literal> extension), a new CDR is generated for the
  120. channel. Any statistics are gathered from this new CDR. By enabling
  121. this option, no new CDR is created for the dialplan logic that is
  122. executed in <literal>h</literal> extensions or attached hangup handler
  123. subroutines. The default value is <literal>yes</literal>, indicating
  124. that a CDR will be generated during hangup logic.</para>
  125. </description>
  126. </configOption>
  127. <configOption name="initiatedseconds">
  128. <synopsis>Count microseconds for billsec purposes</synopsis>
  129. <description><para>Normally, the <literal>billsec</literal> field logged to the CDR backends
  130. is simply the end time (hangup time) minus the answer time in seconds. Internally,
  131. asterisk stores the time in terms of microseconds and seconds. By setting
  132. initiatedseconds to <literal>yes</literal>, you can force asterisk to report any seconds
  133. that were initiated (a sort of round up method). Technically, this is
  134. when the microsecond part of the end time is greater than the microsecond
  135. part of the answer time, then the billsec time is incremented one second.</para>
  136. </description>
  137. </configOption>
  138. <configOption name="batch">
  139. <synopsis>Submit CDRs to the backends for processing in batches</synopsis>
  140. <description><para>Define the CDR batch mode, where instead of posting the CDR at the end of
  141. every call, the data will be stored in a buffer to help alleviate load on the
  142. asterisk server.</para>
  143. <warning><para>Use of batch mode may result in data loss after unsafe asterisk termination,
  144. i.e., software crash, power failure, kill -9, etc.</para>
  145. </warning>
  146. </description>
  147. </configOption>
  148. <configOption name="size">
  149. <synopsis>The maximum number of CDRs to accumulate before triggering a batch</synopsis>
  150. <description><para>Define the maximum number of CDRs to accumulate in the buffer before posting
  151. them to the backend engines. batch must be set to <literal>yes</literal>.</para>
  152. </description>
  153. </configOption>
  154. <configOption name="time">
  155. <synopsis>The maximum time to accumulate CDRs before triggering a batch</synopsis>
  156. <description><para>Define the maximum time to accumulate CDRs before posting them in a batch to the
  157. backend engines. If this time limit is reached, then it will post the records, regardless of the value
  158. defined for size. batch must be set to <literal>yes</literal>.</para>
  159. <note><para>Time is expressed in seconds.</para></note>
  160. </description>
  161. </configOption>
  162. <configOption name="scheduleronly">
  163. <synopsis>Post batched CDRs on their own thread instead of the scheduler</synopsis>
  164. <description><para>The CDR engine uses the internal asterisk scheduler to determine when to post
  165. records. Posting can either occur inside the scheduler thread, or a new
  166. thread can be spawned for the submission of every batch. For small batches,
  167. it might be acceptable to just use the scheduler thread, so set this to <literal>yes</literal>.
  168. For large batches, say anything over size=10, a new thread is recommended, so
  169. set this to <literal>no</literal>.</para>
  170. </description>
  171. </configOption>
  172. <configOption name="safeshutdown">
  173. <synopsis>Block shutdown of Asterisk until CDRs are submitted</synopsis>
  174. <description><para>When shutting down asterisk, you can block until the CDRs are submitted. If
  175. you don't, then data will likely be lost. You can always check the size of
  176. the CDR batch buffer with the CLI <astcli>cdr status</astcli> command. To enable blocking on
  177. submission of CDR data during asterisk shutdown, set this to <literal>yes</literal>.</para>
  178. </description>
  179. </configOption>
  180. </configObject>
  181. </configFile>
  182. </configInfo>
  183. ***/
  184. /* The prime here should be similar in size to the channel container. */
  185. #ifdef LOW_MEMORY
  186. #define NUM_CDR_BUCKETS 61
  187. #else
  188. #define NUM_CDR_BUCKETS 769
  189. #endif
  190. #define DEFAULT_ENABLED "1"
  191. #define DEFAULT_BATCHMODE "0"
  192. #define DEFAULT_UNANSWERED "0"
  193. #define DEFAULT_CONGESTION "0"
  194. #define DEFAULT_END_BEFORE_H_EXTEN "1"
  195. #define DEFAULT_INITIATED_SECONDS "0"
  196. #define DEFAULT_BATCH_SIZE "100"
  197. #define MAX_BATCH_SIZE 1000
  198. #define DEFAULT_BATCH_TIME "300"
  199. #define MAX_BATCH_TIME 86400
  200. #define DEFAULT_BATCH_SCHEDULER_ONLY "0"
  201. #define DEFAULT_BATCH_SAFE_SHUTDOWN "1"
  202. #define CDR_DEBUG(mod_cfg, fmt, ...) \
  203. do { \
  204. if (ast_test_flag(&(mod_cfg)->general->settings, CDR_DEBUG)) { \
  205. ast_verb(1, (fmt), ##__VA_ARGS__); \
  206. } } while (0)
  207. static void cdr_detach(struct ast_cdr *cdr);
  208. static void cdr_submit_batch(int shutdown);
  209. static int cdr_toggle_runtime_options(void);
  210. /*! \brief The configuration settings for this module */
  211. struct module_config {
  212. struct ast_cdr_config *general; /*< CDR global settings */
  213. };
  214. /*! \brief The container for the module configuration */
  215. static AO2_GLOBAL_OBJ_STATIC(module_configs);
  216. /*! \brief The type definition for general options */
  217. static struct aco_type general_option = {
  218. .type = ACO_GLOBAL,
  219. .name = "general",
  220. .item_offset = offsetof(struct module_config, general),
  221. .category = "^general$",
  222. .category_match = ACO_WHITELIST,
  223. };
  224. static void *module_config_alloc(void);
  225. static void module_config_destructor(void *obj);
  226. /*! \brief The file definition */
  227. static struct aco_file module_file_conf = {
  228. .filename = "cdr.conf",
  229. .skip_category = "(^csv$|^custom$|^manager$|^odbc$|^pgsql$|^radius$|^sqlite$|^tds$|^mysql$)",
  230. .types = ACO_TYPES(&general_option),
  231. };
  232. CONFIG_INFO_CORE("cdr", cfg_info, module_configs, module_config_alloc,
  233. .files = ACO_FILES(&module_file_conf),
  234. );
  235. static struct aco_type *general_options[] = ACO_TYPES(&general_option);
  236. /*! \brief Dispose of a module config object */
  237. static void module_config_destructor(void *obj)
  238. {
  239. struct module_config *cfg = obj;
  240. if (!cfg) {
  241. return;
  242. }
  243. ao2_ref(cfg->general, -1);
  244. }
  245. /*! \brief Create a new module config object */
  246. static void *module_config_alloc(void)
  247. {
  248. struct module_config *mod_cfg;
  249. struct ast_cdr_config *cdr_config;
  250. mod_cfg = ao2_alloc(sizeof(*mod_cfg), module_config_destructor);
  251. if (!mod_cfg) {
  252. return NULL;
  253. }
  254. cdr_config = ao2_alloc(sizeof(*cdr_config), NULL);
  255. if (!cdr_config) {
  256. ao2_ref(cdr_config, -1);
  257. return NULL;
  258. }
  259. mod_cfg->general = cdr_config;
  260. return mod_cfg;
  261. }
  262. /*! \brief Registration object for CDR backends */
  263. struct cdr_beitem {
  264. char name[20];
  265. char desc[80];
  266. ast_cdrbe be;
  267. AST_RWLIST_ENTRY(cdr_beitem) list;
  268. int suspended:1;
  269. };
  270. /*! \brief List of registered backends */
  271. static AST_RWLIST_HEAD_STATIC(be_list, cdr_beitem);
  272. /*! \brief Queued CDR waiting to be batched */
  273. struct cdr_batch_item {
  274. struct ast_cdr *cdr;
  275. struct cdr_batch_item *next;
  276. };
  277. /*! \brief The actual batch queue */
  278. static struct cdr_batch {
  279. int size;
  280. struct cdr_batch_item *head;
  281. struct cdr_batch_item *tail;
  282. } *batch = NULL;
  283. /*! \brief The global sequence counter used for CDRs */
  284. static int global_cdr_sequence = 0;
  285. /*! \brief Scheduler items */
  286. static struct ast_sched_context *sched;
  287. static int cdr_sched = -1;
  288. AST_MUTEX_DEFINE_STATIC(cdr_sched_lock);
  289. static pthread_t cdr_thread = AST_PTHREADT_NULL;
  290. /*! \brief Lock protecting modifications to the batch queue */
  291. AST_MUTEX_DEFINE_STATIC(cdr_batch_lock);
  292. /*! \brief These are used to wake up the CDR thread when there's work to do */
  293. AST_MUTEX_DEFINE_STATIC(cdr_pending_lock);
  294. static ast_cond_t cdr_pending_cond;
  295. /*! \brief A container of the active CDRs indexed by Party A channel id */
  296. static struct ao2_container *active_cdrs_by_channel;
  297. /*! \brief Message router for stasis messages regarding channel state */
  298. static struct stasis_message_router *stasis_router;
  299. /*! \brief Our subscription for bridges */
  300. static struct stasis_forward *bridge_subscription;
  301. /*! \brief Our subscription for channels */
  302. static struct stasis_forward *channel_subscription;
  303. /*! \brief Our subscription for parking */
  304. static struct stasis_forward *parking_subscription;
  305. /*! \brief The parent topic for all topics we want to aggregate for CDRs */
  306. static struct stasis_topic *cdr_topic;
  307. /*! \brief A message type used to synchronize with the CDR topic */
  308. STASIS_MESSAGE_TYPE_DEFN_LOCAL(cdr_sync_message_type);
  309. struct cdr_object;
  310. /*! \brief Return types for \ref process_bridge_enter functions */
  311. enum process_bridge_enter_results {
  312. /*!
  313. * The CDR was the only party in the bridge.
  314. */
  315. BRIDGE_ENTER_ONLY_PARTY,
  316. /*!
  317. * The CDR was able to obtain a Party B from some other party already in the bridge
  318. */
  319. BRIDGE_ENTER_OBTAINED_PARTY_B,
  320. /*!
  321. * The CDR was not able to obtain a Party B
  322. */
  323. BRIDGE_ENTER_NO_PARTY_B,
  324. /*!
  325. * This CDR can't handle a bridge enter message and a new CDR needs to be created
  326. */
  327. BRIDGE_ENTER_NEED_CDR,
  328. };
  329. /*!
  330. * \brief A virtual table used for \ref cdr_object.
  331. *
  332. * Note that all functions are optional - if a subclass does not need an
  333. * implementation, it is safe to leave it NULL.
  334. */
  335. struct cdr_object_fn_table {
  336. /*! \brief Name of the subclass */
  337. const char *name;
  338. /*!
  339. * \brief An initialization function. This will be called automatically
  340. * when a \ref cdr_object is switched to this type in
  341. * \ref cdr_object_transition_state
  342. *
  343. * \param cdr The \ref cdr_object that was just transitioned
  344. */
  345. void (* const init_function)(struct cdr_object *cdr);
  346. /*!
  347. * \brief Process a Party A update for the \ref cdr_object
  348. *
  349. * \param cdr The \ref cdr_object to process the update
  350. * \param snapshot The snapshot for the CDR's Party A
  351. * \retval 0 the CDR handled the update or ignored it
  352. * \retval 1 the CDR is finalized and a new one should be made to handle it
  353. */
  354. int (* const process_party_a)(struct cdr_object *cdr,
  355. struct ast_channel_snapshot *snapshot);
  356. /*!
  357. * \brief Process a Party B update for the \ref cdr_object
  358. *
  359. * \param cdr The \ref cdr_object to process the update
  360. * \param snapshot The snapshot for the CDR's Party B
  361. */
  362. void (* const process_party_b)(struct cdr_object *cdr,
  363. struct ast_channel_snapshot *snapshot);
  364. /*!
  365. * \brief Process the beginning of a dial. A dial message implies one of two
  366. * things:
  367. * The \ref cdr_object's Party A has been originated
  368. * The \ref cdr_object's Party A is dialing its Party B
  369. *
  370. * \param cdr The \ref cdr_object
  371. * \param caller The originator of the dial attempt
  372. * \param peer The destination of the dial attempt
  373. *
  374. * \retval 0 if the parties in the dial were handled by this CDR
  375. * \retval 1 if the parties could not be handled by this CDR
  376. */
  377. int (* const process_dial_begin)(struct cdr_object *cdr,
  378. struct ast_channel_snapshot *caller,
  379. struct ast_channel_snapshot *peer);
  380. /*!
  381. * \brief Process the end of a dial. At the end of a dial, a CDR can be
  382. * transitioned into one of two states - DialedPending
  383. * (\ref dialed_pending_state_fn_table) or Finalized
  384. * (\ref finalized_state_fn_table).
  385. *
  386. * \param cdr The \ref cdr_object
  387. * \param caller The originator of the dial attempt
  388. * \param peer the Destination of the dial attempt
  389. * \param dial_status What happened
  390. *
  391. * \retval 0 if the parties in the dial were handled by this CDR
  392. * \retval 1 if the parties could not be handled by this CDR
  393. */
  394. int (* const process_dial_end)(struct cdr_object *cdr,
  395. struct ast_channel_snapshot *caller,
  396. struct ast_channel_snapshot *peer,
  397. const char *dial_status);
  398. /*!
  399. * \brief Process the entering of a bridge by this CDR. The purpose of this
  400. * callback is to have the CDR prepare itself for the bridge and attempt to
  401. * find a valid Party B. The act of creating new CDRs based on the entering
  402. * of this channel into the bridge is handled by the higher level message
  403. * handler.
  404. *
  405. * Note that this handler is for when a channel enters into a "normal"
  406. * bridge, where people actually talk to each other. Parking is its own
  407. * thing.
  408. *
  409. * \param cdr The \ref cdr_object
  410. * \param bridge The bridge that the Party A just entered into
  411. * \param channel The \ref ast_channel_snapshot for this CDR's Party A
  412. *
  413. * \retval process_bridge_enter_results Defines whether or not this CDR was able
  414. * to fully handle the bridge enter message.
  415. */
  416. enum process_bridge_enter_results (* const process_bridge_enter)(
  417. struct cdr_object *cdr,
  418. struct ast_bridge_snapshot *bridge,
  419. struct ast_channel_snapshot *channel);
  420. /*!
  421. * \brief Process entering into a parking bridge.
  422. *
  423. * \param cdr The \ref cdr_object
  424. * \param bridge The parking bridge that Party A just entered into
  425. * \param channel The \ref ast_channel_snapshot for this CDR's Party A
  426. *
  427. * \retval 0 This CDR successfully transitioned itself into the parked state
  428. * \retval 1 This CDR couldn't handle the parking transition and we need a
  429. * new CDR.
  430. */
  431. int (* const process_parking_bridge_enter)(struct cdr_object *cdr,
  432. struct ast_bridge_snapshot *bridge,
  433. struct ast_channel_snapshot *channel);
  434. /*!
  435. * \brief Process the leaving of a bridge by this CDR.
  436. *
  437. * \param cdr The \ref cdr_object
  438. * \param bridge The bridge that the Party A just left
  439. * \param channel The \ref ast_channel_snapshot for this CDR's Party A
  440. *
  441. * \retval 0 This CDR left successfully
  442. * \retval 1 Error
  443. */
  444. int (* const process_bridge_leave)(struct cdr_object *cdr,
  445. struct ast_bridge_snapshot *bridge,
  446. struct ast_channel_snapshot *channel);
  447. /*!
  448. * \brief Process an update informing us that the channel got itself parked
  449. *
  450. * \param cdr The \ref cdr_object
  451. * \param channel The parking information for this CDR's party A
  452. *
  453. * \retval 0 This CDR successfully parked itself
  454. * \retval 1 This CDR couldn't handle the park
  455. */
  456. int (* const process_parked_channel)(struct cdr_object *cdr,
  457. struct ast_parked_call_payload *parking_info);
  458. };
  459. static int base_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
  460. static enum process_bridge_enter_results base_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
  461. static int base_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
  462. static int base_process_dial_end(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer, const char *dial_status);
  463. static int base_process_parked_channel(struct cdr_object *cdr, struct ast_parked_call_payload *parking_info);
  464. static void single_state_init_function(struct cdr_object *cdr);
  465. static void single_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
  466. static int single_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer);
  467. static enum process_bridge_enter_results single_state_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
  468. static int single_state_process_parking_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
  469. /*!
  470. * \brief The virtual table for the Single state.
  471. *
  472. * A \ref cdr_object starts off in this state. This represents a channel that
  473. * has no Party B information itself.
  474. *
  475. * A \ref cdr_object from this state can go into any of the following states:
  476. * * \ref dial_state_fn_table
  477. * * \ref bridge_state_fn_table
  478. * * \ref finalized_state_fn_table
  479. */
  480. struct cdr_object_fn_table single_state_fn_table = {
  481. .name = "Single",
  482. .init_function = single_state_init_function,
  483. .process_party_a = base_process_party_a,
  484. .process_party_b = single_state_process_party_b,
  485. .process_dial_begin = single_state_process_dial_begin,
  486. .process_dial_end = base_process_dial_end,
  487. .process_bridge_enter = single_state_process_bridge_enter,
  488. .process_parking_bridge_enter = single_state_process_parking_bridge_enter,
  489. .process_bridge_leave = base_process_bridge_leave,
  490. .process_parked_channel = base_process_parked_channel,
  491. };
  492. static void dial_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
  493. static int dial_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer);
  494. static int dial_state_process_dial_end(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer, const char *dial_status);
  495. static enum process_bridge_enter_results dial_state_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
  496. /*!
  497. * \brief The virtual table for the Dial state.
  498. *
  499. * A \ref cdr_object that has begun a dial operation. This state is entered when
  500. * the Party A for a CDR is determined to be dialing out to a Party B or when
  501. * a CDR is for an originated channel (in which case the Party A information is
  502. * the originated channel, and there is no Party B).
  503. *
  504. * A \ref cdr_object from this state can go in any of the following states:
  505. * * \ref dialed_pending_state_fn_table
  506. * * \ref bridge_state_fn_table
  507. * * \ref finalized_state_fn_table
  508. */
  509. struct cdr_object_fn_table dial_state_fn_table = {
  510. .name = "Dial",
  511. .process_party_a = base_process_party_a,
  512. .process_party_b = dial_state_process_party_b,
  513. .process_dial_begin = dial_state_process_dial_begin,
  514. .process_dial_end = dial_state_process_dial_end,
  515. .process_bridge_enter = dial_state_process_bridge_enter,
  516. .process_bridge_leave = base_process_bridge_leave,
  517. };
  518. static int dialed_pending_state_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
  519. static int dialed_pending_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer);
  520. static enum process_bridge_enter_results dialed_pending_state_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
  521. static int dialed_pending_state_process_parking_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
  522. /*!
  523. * \brief The virtual table for the Dialed Pending state.
  524. *
  525. * A \ref cdr_object that has successfully finished a dial operation, but we
  526. * don't know what they're going to do yet. It's theoretically possible to dial
  527. * a party and then have that party not be bridged with the caller; likewise,
  528. * an origination can complete and the channel go off and execute dialplan. The
  529. * pending state acts as a bridge between either:
  530. * * Entering a bridge
  531. * * Getting a new CDR for new dialplan execution
  532. * * Switching from being originated to executing dialplan
  533. *
  534. * A \ref cdr_object from this state can go in any of the following states:
  535. * * \ref single_state_fn_table
  536. * * \ref dialed_pending_state_fn_table
  537. * * \ref bridge_state_fn_table
  538. * * \ref finalized_state_fn_table
  539. */
  540. struct cdr_object_fn_table dialed_pending_state_fn_table = {
  541. .name = "DialedPending",
  542. .process_party_a = dialed_pending_state_process_party_a,
  543. .process_dial_begin = dialed_pending_state_process_dial_begin,
  544. .process_bridge_enter = dialed_pending_state_process_bridge_enter,
  545. .process_parking_bridge_enter = dialed_pending_state_process_parking_bridge_enter,
  546. .process_bridge_leave = base_process_bridge_leave,
  547. .process_parked_channel = base_process_parked_channel,
  548. };
  549. static void bridge_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
  550. static int bridge_state_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
  551. /*!
  552. * \brief The virtual table for the Bridged state
  553. *
  554. * A \ref cdr_object enters this state when it receives notification that the
  555. * channel has entered a bridge.
  556. *
  557. * A \ref cdr_object from this state can go to:
  558. * * \ref finalized_state_fn_table
  559. */
  560. struct cdr_object_fn_table bridge_state_fn_table = {
  561. .name = "Bridged",
  562. .process_party_a = base_process_party_a,
  563. .process_party_b = bridge_state_process_party_b,
  564. .process_bridge_leave = bridge_state_process_bridge_leave,
  565. .process_parked_channel = base_process_parked_channel,
  566. };
  567. static int parked_state_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
  568. /*!
  569. * \brief The virtual table for the Parked state
  570. *
  571. * Parking is weird. Unlike typical bridges, it has to be treated somewhat
  572. * uniquely - a channel in a parking bridge (which is a subclass of a holding
  573. * bridge) has to be handled as if the channel went into an application.
  574. * However, when the channel comes out, we need a new CDR - unlike the Single
  575. * state.
  576. */
  577. struct cdr_object_fn_table parked_state_fn_table = {
  578. .name = "Parked",
  579. .process_party_a = base_process_party_a,
  580. .process_bridge_leave = parked_state_process_bridge_leave,
  581. .process_parked_channel = base_process_parked_channel,
  582. };
  583. static void finalized_state_init_function(struct cdr_object *cdr);
  584. static int finalized_state_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
  585. /*!
  586. * \brief The virtual table for the finalized state.
  587. *
  588. * Once in the finalized state, the CDR is done. No modifications can be made
  589. * to the CDR.
  590. */
  591. struct cdr_object_fn_table finalized_state_fn_table = {
  592. .name = "Finalized",
  593. .init_function = finalized_state_init_function,
  594. .process_party_a = finalized_state_process_party_a,
  595. .process_bridge_enter = base_process_bridge_enter,
  596. };
  597. /*! \brief A wrapper object around a snapshot.
  598. * Fields that are mutable by the CDR engine are replicated here.
  599. */
  600. struct cdr_object_snapshot {
  601. struct ast_channel_snapshot *snapshot; /*!< The channel snapshot */
  602. char userfield[AST_MAX_USER_FIELD]; /*!< Userfield for the channel */
  603. unsigned int flags; /*!< Specific flags for this party */
  604. struct varshead variables; /*!< CDR variables for the channel */
  605. };
  606. /*! \brief An in-memory representation of an active CDR */
  607. struct cdr_object {
  608. struct cdr_object_snapshot party_a; /*!< The Party A information */
  609. struct cdr_object_snapshot party_b; /*!< The Party B information */
  610. struct cdr_object_fn_table *fn_table; /*!< The current virtual table */
  611. enum ast_cdr_disposition disposition; /*!< The disposition of the CDR */
  612. struct timeval start; /*!< When this CDR was created */
  613. struct timeval answer; /*!< Either when the channel was answered, or when the path between channels was established */
  614. struct timeval end; /*!< When this CDR was finalized */
  615. unsigned int sequence; /*!< A monotonically increasing number for each CDR */
  616. struct ast_flags flags; /*!< Flags on the CDR */
  617. AST_DECLARE_STRING_FIELDS(
  618. AST_STRING_FIELD(linkedid); /*!< Linked ID. Cached here as it may change out from party A, which must be immutable */
  619. AST_STRING_FIELD(uniqueid); /*!< Unique id of party A. Cached here as it is the primary key of this CDR */
  620. AST_STRING_FIELD(name); /*!< Channel name of party A. Cached here as the party A address may change */
  621. AST_STRING_FIELD(bridge); /*!< The bridge the party A happens to be in. */
  622. AST_STRING_FIELD(appl); /*!< The last accepted application party A was in */
  623. AST_STRING_FIELD(data); /*!< The data for the last accepted application party A was in */
  624. );
  625. struct cdr_object *next; /*!< The next CDR object in the chain */
  626. struct cdr_object *last; /*!< The last CDR object in the chain */
  627. };
  628. /*!
  629. * \brief Copy variables from one list to another
  630. * \param to_list destination
  631. * \param from_list source
  632. * \retval The number of copied variables
  633. */
  634. static int copy_variables(struct varshead *to_list, struct varshead *from_list)
  635. {
  636. struct ast_var_t *variables;
  637. struct ast_var_t *newvariable;
  638. const char *var;
  639. const char *val;
  640. int x = 0;
  641. AST_LIST_TRAVERSE(from_list, variables, entries) {
  642. var = ast_var_name(variables);
  643. if (ast_strlen_zero(var)) {
  644. continue;
  645. }
  646. val = ast_var_value(variables);
  647. if (ast_strlen_zero(val)) {
  648. continue;
  649. }
  650. newvariable = ast_var_assign(var, val);
  651. if (newvariable) {
  652. AST_LIST_INSERT_HEAD(to_list, newvariable, entries);
  653. ++x;
  654. }
  655. }
  656. return x;
  657. }
  658. /*!
  659. * \brief Delete all variables from a variable list
  660. * \param headp The head pointer to the variable list to delete
  661. */
  662. static void free_variables(struct varshead *headp)
  663. {
  664. struct ast_var_t *vardata;
  665. while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries))) {
  666. ast_var_delete(vardata);
  667. }
  668. }
  669. /*!
  670. * \brief Copy a snapshot and its details
  671. * \param dst The destination
  672. * \param src The source
  673. */
  674. static void cdr_object_snapshot_copy(struct cdr_object_snapshot *dst, struct cdr_object_snapshot *src)
  675. {
  676. if (dst->snapshot) {
  677. ao2_t_ref(dst->snapshot, -1, "release old snapshot during copy");
  678. }
  679. dst->snapshot = src->snapshot;
  680. ao2_t_ref(dst->snapshot, +1, "bump new snapshot during copy");
  681. strcpy(dst->userfield, src->userfield);
  682. dst->flags = src->flags;
  683. copy_variables(&dst->variables, &src->variables);
  684. }
  685. /*!
  686. * \brief Transition a \ref cdr_object to a new state
  687. * \param cdr The \ref cdr_object to transition
  688. * \param fn_table The \ref cdr_object_fn_table state to go to
  689. */
  690. static void cdr_object_transition_state(struct cdr_object *cdr, struct cdr_object_fn_table *fn_table)
  691. {
  692. RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
  693. CDR_DEBUG(mod_cfg, "%p - Transitioning CDR for %s from state %s to %s\n",
  694. cdr, cdr->party_a.snapshot->name,
  695. cdr->fn_table ? cdr->fn_table->name : "NONE", fn_table->name);
  696. cdr->fn_table = fn_table;
  697. if (cdr->fn_table->init_function) {
  698. cdr->fn_table->init_function(cdr);
  699. }
  700. }
  701. /*! \internal
  702. * \brief Hash function for containers of CDRs indexing by Party A uniqueid */
  703. static int cdr_object_channel_hash_fn(const void *obj, const int flags)
  704. {
  705. const struct cdr_object *cdr;
  706. const char *key;
  707. switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
  708. case OBJ_KEY:
  709. key = obj;
  710. break;
  711. case OBJ_POINTER:
  712. cdr = obj;
  713. key = cdr->uniqueid;
  714. break;
  715. default:
  716. ast_assert(0);
  717. return 0;
  718. }
  719. return ast_str_case_hash(key);
  720. }
  721. /*! \internal
  722. * \brief Comparison function for containers of CDRs indexing by Party A uniqueid
  723. */
  724. static int cdr_object_channel_cmp_fn(void *obj, void *arg, int flags)
  725. {
  726. struct cdr_object *left = obj;
  727. struct cdr_object *right = arg;
  728. const char *right_key = arg;
  729. int cmp;
  730. switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
  731. case OBJ_POINTER:
  732. right_key = right->uniqueid;
  733. /* Fall through */
  734. case OBJ_KEY:
  735. cmp = strcmp(left->uniqueid, right_key);
  736. break;
  737. case OBJ_PARTIAL_KEY:
  738. /*
  739. * We could also use a partial key struct containing a length
  740. * so strlen() does not get called for every comparison instead.
  741. */
  742. cmp = strncmp(left->uniqueid, right_key, strlen(right_key));
  743. break;
  744. default:
  745. /* Sort can only work on something with a full or partial key. */
  746. ast_assert(0);
  747. cmp = 0;
  748. break;
  749. }
  750. return cmp ? 0 : CMP_MATCH;
  751. }
  752. /*!
  753. * \brief \ref cdr_object Destructor
  754. */
  755. static void cdr_object_dtor(void *obj)
  756. {
  757. struct cdr_object *cdr = obj;
  758. struct ast_var_t *it_var;
  759. ao2_cleanup(cdr->party_a.snapshot);
  760. ao2_cleanup(cdr->party_b.snapshot);
  761. while ((it_var = AST_LIST_REMOVE_HEAD(&cdr->party_a.variables, entries))) {
  762. ast_var_delete(it_var);
  763. }
  764. while ((it_var = AST_LIST_REMOVE_HEAD(&cdr->party_b.variables, entries))) {
  765. ast_var_delete(it_var);
  766. }
  767. ast_string_field_free_memory(cdr);
  768. ao2_cleanup(cdr->next);
  769. }
  770. /*!
  771. * \brief \ref cdr_object constructor
  772. * \param chan The \ref ast_channel_snapshot that is the CDR's Party A
  773. *
  774. * This implicitly sets the state of the newly created CDR to the Single state
  775. * (\ref single_state_fn_table)
  776. */
  777. static struct cdr_object *cdr_object_alloc(struct ast_channel_snapshot *chan)
  778. {
  779. RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
  780. struct cdr_object *cdr;
  781. ast_assert(chan != NULL);
  782. cdr = ao2_alloc(sizeof(*cdr), cdr_object_dtor);
  783. if (!cdr) {
  784. return NULL;
  785. }
  786. cdr->last = cdr;
  787. if (ast_string_field_init(cdr, 64)) {
  788. ao2_cleanup(cdr);
  789. return NULL;
  790. }
  791. ast_string_field_set(cdr, uniqueid, chan->uniqueid);
  792. ast_string_field_set(cdr, name, chan->name);
  793. ast_string_field_set(cdr, linkedid, chan->linkedid);
  794. cdr->disposition = AST_CDR_NULL;
  795. cdr->sequence = ast_atomic_fetchadd_int(&global_cdr_sequence, +1);
  796. cdr->party_a.snapshot = chan;
  797. ao2_t_ref(cdr->party_a.snapshot, +1, "bump snapshot during CDR creation");
  798. CDR_DEBUG(mod_cfg, "%p - Created CDR for channel %s\n", cdr, chan->name);
  799. cdr_object_transition_state(cdr, &single_state_fn_table);
  800. return cdr;
  801. }
  802. /*!
  803. * \brief Create a new \ref cdr_object and append it to an existing chain
  804. * \param cdr The \ref cdr_object to append to
  805. */
  806. static struct cdr_object *cdr_object_create_and_append(struct cdr_object *cdr)
  807. {
  808. struct cdr_object *new_cdr;
  809. struct cdr_object *it_cdr;
  810. struct cdr_object *cdr_last;
  811. cdr_last = cdr->last;
  812. new_cdr = cdr_object_alloc(cdr_last->party_a.snapshot);
  813. if (!new_cdr) {
  814. return NULL;
  815. }
  816. new_cdr->disposition = AST_CDR_NULL;
  817. /* Copy over the linkedid, as it may have changed */
  818. ast_string_field_set(new_cdr, linkedid, cdr_last->linkedid);
  819. ast_string_field_set(new_cdr, appl, cdr_last->appl);
  820. ast_string_field_set(new_cdr, data, cdr_last->data);
  821. /* Copy over other Party A information */
  822. cdr_object_snapshot_copy(&new_cdr->party_a, &cdr_last->party_a);
  823. /* Append the CDR to the end of the list */
  824. for (it_cdr = cdr; it_cdr->next; it_cdr = it_cdr->next) {
  825. it_cdr->last = new_cdr;
  826. }
  827. it_cdr->last = new_cdr;
  828. it_cdr->next = new_cdr;
  829. return new_cdr;
  830. }
  831. /*!
  832. * \brief Return whether or not a channel has changed its state in the dialplan, subject
  833. * to endbeforehexten logic
  834. *
  835. * \param old_snapshot The previous state
  836. * \param new_snapshot The new state
  837. *
  838. * \retval 0 if the state has not changed
  839. * \retval 1 if the state changed
  840. */
  841. static int snapshot_cep_changed(struct ast_channel_snapshot *old_snapshot,
  842. struct ast_channel_snapshot *new_snapshot)
  843. {
  844. RAII_VAR(struct module_config *, mod_cfg,
  845. ao2_global_obj_ref(module_configs), ao2_cleanup);
  846. /* If we ignore hangup logic, don't indicate that we're executing anything new */
  847. if (ast_test_flag(&mod_cfg->general->settings, CDR_END_BEFORE_H_EXTEN)
  848. && ast_test_flag(&new_snapshot->softhangup_flags, AST_SOFTHANGUP_HANGUP_EXEC)) {
  849. return 0;
  850. }
  851. /* When Party A is originated to an application and the application exits, the stack
  852. * will attempt to clear the application and restore the dummy originate application
  853. * of "AppDialX". Ignore application changes to AppDialX as a result.
  854. */
  855. if (strcmp(new_snapshot->appl, old_snapshot->appl) && strncasecmp(new_snapshot->appl, "appdial", 7)
  856. && (strcmp(new_snapshot->context, old_snapshot->context)
  857. || strcmp(new_snapshot->exten, old_snapshot->exten)
  858. || new_snapshot->priority != old_snapshot->priority)) {
  859. return 1;
  860. }
  861. return 0;
  862. }
  863. /*!
  864. * \brief Return whether or not a \ref ast_channel_snapshot is for a channel
  865. * that was created as the result of a dial operation
  866. *
  867. * \retval 0 the channel was not created as the result of a dial
  868. * \retval 1 the channel was created as the result of a dial
  869. */
  870. static int snapshot_is_dialed(struct ast_channel_snapshot *snapshot)
  871. {
  872. return (ast_test_flag(&snapshot->flags, AST_FLAG_OUTGOING)
  873. && !(ast_test_flag(&snapshot->flags, AST_FLAG_ORIGINATED)));
  874. }
  875. /*!
  876. * \brief Given two CDR snapshots, figure out who should be Party A for the
  877. * resulting CDR
  878. * \param left One of the snapshots
  879. * \param right The other snapshot
  880. * \retval The snapshot that won
  881. */
  882. static struct cdr_object_snapshot *cdr_object_pick_party_a(struct cdr_object_snapshot *left, struct cdr_object_snapshot *right)
  883. {
  884. /* Check whether or not the party is dialed. A dialed party is never the
  885. * Party A with a party that was not dialed.
  886. */
  887. if (!snapshot_is_dialed(left->snapshot) && snapshot_is_dialed(right->snapshot)) {
  888. return left;
  889. } else if (snapshot_is_dialed(left->snapshot) && !snapshot_is_dialed(right->snapshot)) {
  890. return right;
  891. }
  892. /* Try the Party A flag */
  893. if (ast_test_flag(left, AST_CDR_FLAG_PARTY_A) && !ast_test_flag(right, AST_CDR_FLAG_PARTY_A)) {
  894. return left;
  895. } else if (!ast_test_flag(right, AST_CDR_FLAG_PARTY_A) && ast_test_flag(right, AST_CDR_FLAG_PARTY_A)) {
  896. return right;
  897. }
  898. /* Neither party is dialed and neither has the Party A flag - defer to
  899. * creation time */
  900. if (left->snapshot->creationtime.tv_sec < right->snapshot->creationtime.tv_sec) {
  901. return left;
  902. } else if (left->snapshot->creationtime.tv_sec > right->snapshot->creationtime.tv_sec) {
  903. return right;
  904. } else if (left->snapshot->creationtime.tv_usec > right->snapshot->creationtime.tv_usec) {
  905. return right;
  906. } else {
  907. /* Okay, fine, take the left one */
  908. return left;
  909. }
  910. }
  911. /*!
  912. * Compute the duration for a \ref cdr_object
  913. */
  914. static long cdr_object_get_duration(struct cdr_object *cdr)
  915. {
  916. return (long)(ast_tvdiff_ms(ast_tvzero(cdr->end) ? ast_tvnow() : cdr->end, cdr->start) / 1000);
  917. }
  918. /*!
  919. * \brief Compute the billsec for a \ref cdr_object
  920. */
  921. static long cdr_object_get_billsec(struct cdr_object *cdr)
  922. {
  923. RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
  924. long int ms;
  925. if (ast_tvzero(cdr->answer)) {
  926. return 0;
  927. }
  928. ms = ast_tvdiff_ms(ast_tvzero(cdr->end) ? ast_tvnow() : cdr->end, cdr->answer);
  929. if (ast_test_flag(&mod_cfg->general->settings, CDR_INITIATED_SECONDS)
  930. && (ms % 1000 >= 500)) {
  931. ms = (ms / 1000) + 1;
  932. } else {
  933. ms = ms / 1000;
  934. }
  935. return ms;
  936. }
  937. /*!
  938. * \internal
  939. * \brief Set a variable on a CDR object
  940. *
  941. * \param headp The header pointer to the variable to set
  942. * \param name The name of the variable
  943. * \param value The value of the variable
  944. */
  945. static void set_variable(struct varshead *headp, const char *name, const char *value)
  946. {
  947. struct ast_var_t *newvariable;
  948. AST_LIST_TRAVERSE_SAFE_BEGIN(headp, newvariable, entries) {
  949. if (!strcasecmp(ast_var_name(newvariable), name)) {
  950. AST_LIST_REMOVE_CURRENT(entries);
  951. ast_var_delete(newvariable);
  952. break;
  953. }
  954. }
  955. AST_LIST_TRAVERSE_SAFE_END;
  956. if (value && (newvariable = ast_var_assign(name, value))) {
  957. AST_LIST_INSERT_HEAD(headp, newvariable, entries);
  958. }
  959. }
  960. /*!
  961. * \brief Create a chain of \ref ast_cdr objects from a chain of \ref cdr_object
  962. * suitable for consumption by the registered CDR backends
  963. * \param cdr The \ref cdr_object to convert to a public record
  964. * \retval A chain of \ref ast_cdr objects on success
  965. * \retval NULL on failure
  966. */
  967. static struct ast_cdr *cdr_object_create_public_records(struct cdr_object *cdr)
  968. {
  969. struct ast_cdr *pub_cdr = NULL, *cdr_prev = NULL;
  970. struct cdr_object *it_cdr;
  971. struct ast_var_t *it_var, *it_copy_var;
  972. struct ast_channel_snapshot *party_a;
  973. struct ast_channel_snapshot *party_b;
  974. RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
  975. for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
  976. struct ast_cdr *cdr_copy;
  977. /* Don't create records for CDRs where the party A was a dialed channel */
  978. if (snapshot_is_dialed(it_cdr->party_a.snapshot) && !it_cdr->party_b.snapshot) {
  979. ast_debug(1, "CDR for %s is dialed and has no Party B; discarding\n",
  980. it_cdr->party_a.snapshot->name);
  981. continue;
  982. }
  983. cdr_copy = ast_calloc(1, sizeof(*cdr_copy));
  984. if (!cdr_copy) {
  985. ast_free(pub_cdr);
  986. return NULL;
  987. }
  988. party_a = it_cdr->party_a.snapshot;
  989. party_b = it_cdr->party_b.snapshot;
  990. /* Party A */
  991. ast_assert(party_a != NULL);
  992. ast_copy_string(cdr_copy->accountcode, party_a->accountcode, sizeof(cdr_copy->accountcode));
  993. cdr_copy->amaflags = party_a->amaflags;
  994. ast_copy_string(cdr_copy->channel, party_a->name, sizeof(cdr_copy->channel));
  995. ast_callerid_merge(cdr_copy->clid, sizeof(cdr_copy->clid), party_a->caller_name, party_a->caller_number, "");
  996. ast_copy_string(cdr_copy->src, party_a->caller_number, sizeof(cdr_copy->src));
  997. ast_copy_string(cdr_copy->uniqueid, party_a->uniqueid, sizeof(cdr_copy->uniqueid));
  998. ast_copy_string(cdr_copy->lastapp, it_cdr->appl, sizeof(cdr_copy->lastapp));
  999. ast_copy_string(cdr_copy->lastdata, it_cdr->data, sizeof(cdr_copy->lastdata));
  1000. ast_copy_string(cdr_copy->dst, party_a->exten, sizeof(cdr_copy->dst));
  1001. ast_copy_string(cdr_copy->dcontext, party_a->context, sizeof(cdr_copy->dcontext));
  1002. /* Party B */
  1003. if (party_b) {
  1004. ast_copy_string(cdr_copy->dstchannel, party_b->name, sizeof(cdr_copy->dstchannel));
  1005. ast_copy_string(cdr_copy->peeraccount, party_b->accountcode, sizeof(cdr_copy->peeraccount));
  1006. if (!ast_strlen_zero(it_cdr->party_b.userfield)) {
  1007. snprintf(cdr_copy->userfield, sizeof(cdr_copy->userfield), "%s;%s", it_cdr->party_a.userfield, it_cdr->party_b.userfield);
  1008. }
  1009. }
  1010. if (ast_strlen_zero(cdr_copy->userfield) && !ast_strlen_zero(it_cdr->party_a.userfield)) {
  1011. ast_copy_string(cdr_copy->userfield, it_cdr->party_a.userfield, sizeof(cdr_copy->userfield));
  1012. }
  1013. /* Timestamps/durations */
  1014. cdr_copy->start = it_cdr->start;
  1015. cdr_copy->answer = it_cdr->answer;
  1016. cdr_copy->end = it_cdr->end;
  1017. cdr_copy->billsec = cdr_object_get_billsec(it_cdr);
  1018. cdr_copy->duration = cdr_object_get_duration(it_cdr);
  1019. /* Flags and IDs */
  1020. ast_copy_flags(cdr_copy, &it_cdr->flags, AST_FLAGS_ALL);
  1021. ast_copy_string(cdr_copy->linkedid, it_cdr->linkedid, sizeof(cdr_copy->linkedid));
  1022. cdr_copy->disposition = it_cdr->disposition;
  1023. cdr_copy->sequence = it_cdr->sequence;
  1024. /* Variables */
  1025. copy_variables(&cdr_copy->varshead, &it_cdr->party_a.variables);
  1026. AST_LIST_TRAVERSE(&it_cdr->party_b.variables, it_var, entries) {
  1027. int found = 0;
  1028. struct ast_var_t *newvariable;
  1029. AST_LIST_TRAVERSE(&cdr_copy->varshead, it_copy_var, entries) {
  1030. if (!strcasecmp(ast_var_name(it_var), ast_var_name(it_copy_var))) {
  1031. found = 1;
  1032. break;
  1033. }
  1034. }
  1035. if (!found && (newvariable = ast_var_assign(ast_var_name(it_var), ast_var_value(it_var)))) {
  1036. AST_LIST_INSERT_TAIL(&cdr_copy->varshead, newvariable, entries);
  1037. }
  1038. }
  1039. if (!pub_cdr) {
  1040. pub_cdr = cdr_copy;
  1041. cdr_prev = pub_cdr;
  1042. } else {
  1043. cdr_prev->next = cdr_copy;
  1044. cdr_prev = cdr_copy;
  1045. }
  1046. }
  1047. return pub_cdr;
  1048. }
  1049. /*!
  1050. * \brief Dispatch a CDR.
  1051. * \param cdr The \ref cdr_object to dispatch
  1052. *
  1053. * This will create a \ref ast_cdr object and publish it to the various backends
  1054. */
  1055. static void cdr_object_dispatch(struct cdr_object *cdr)
  1056. {
  1057. RAII_VAR(struct module_config *, mod_cfg,
  1058. ao2_global_obj_ref(module_configs), ao2_cleanup);
  1059. struct ast_cdr *pub_cdr;
  1060. CDR_DEBUG(mod_cfg, "%p - Dispatching CDR for Party A %s, Party B %s\n", cdr,
  1061. cdr->party_a.snapshot->name,
  1062. cdr->party_b.snapshot ? cdr->party_b.snapshot->name : "<none>");
  1063. pub_cdr = cdr_object_create_public_records(cdr);
  1064. cdr_detach(pub_cdr);
  1065. }
  1066. /*!
  1067. * \brief Set the disposition on a \ref cdr_object based on a hangupcause code
  1068. * \param cdr The \ref cdr_object
  1069. * \param hangupcause The Asterisk hangup cause code
  1070. */
  1071. static void cdr_object_set_disposition(struct cdr_object *cdr, int hangupcause)
  1072. {
  1073. RAII_VAR(struct module_config *, mod_cfg,
  1074. ao2_global_obj_ref(module_configs), ao2_cleanup);
  1075. /* Change the disposition based on the hang up cause */
  1076. switch (hangupcause) {
  1077. case AST_CAUSE_BUSY:
  1078. cdr->disposition = AST_CDR_BUSY;
  1079. break;
  1080. case AST_CAUSE_CONGESTION:
  1081. if (!ast_test_flag(&mod_cfg->general->settings, CDR_CONGESTION)) {
  1082. cdr->disposition = AST_CDR_FAILED;
  1083. } else {
  1084. cdr->disposition = AST_CDR_CONGESTION;
  1085. }
  1086. break;
  1087. case AST_CAUSE_NO_ROUTE_DESTINATION:
  1088. case AST_CAUSE_UNREGISTERED:
  1089. cdr->disposition = AST_CDR_FAILED;
  1090. break;
  1091. case AST_CAUSE_NORMAL_CLEARING:
  1092. case AST_CAUSE_NO_ANSWER:
  1093. cdr->disposition = AST_CDR_NOANSWER;
  1094. break;
  1095. default:
  1096. break;
  1097. }
  1098. }
  1099. /*!
  1100. * \brief Finalize a CDR.
  1101. *
  1102. * This function is safe to call multiple times. Note that you can call this
  1103. * explicitly before going to the finalized state if there's a chance the CDR
  1104. * will be re-activated, in which case the \ref cdr_object's end time should be
  1105. * cleared. This function is implicitly called when a CDR transitions to the
  1106. * finalized state and right before it is dispatched
  1107. *
  1108. * \param cdr_object The CDR to finalize
  1109. */
  1110. static void cdr_object_finalize(struct cdr_object *cdr)
  1111. {
  1112. RAII_VAR(struct module_config *, mod_cfg,
  1113. ao2_global_obj_ref(module_configs), ao2_cleanup);
  1114. if (!ast_tvzero(cdr->end)) {
  1115. return;
  1116. }
  1117. cdr->end = ast_tvnow();
  1118. if (cdr->disposition == AST_CDR_NULL) {
  1119. if (!ast_tvzero(cdr->answer)) {
  1120. cdr->disposition = AST_CDR_ANSWERED;
  1121. } else if (cdr->party_a.snapshot->hangupcause) {
  1122. cdr_object_set_disposition(cdr, cdr->party_a.snapshot->hangupcause);
  1123. } else if (cdr->party_b.snapshot && cdr->party_b.snapshot->hangupcause) {
  1124. cdr_object_set_disposition(cdr, cdr->party_b.snapshot->hangupcause);
  1125. } else {
  1126. cdr->disposition = AST_CDR_FAILED;
  1127. }
  1128. }
  1129. /* tv_usec is suseconds_t, which could be int or long */
  1130. ast_debug(1, "Finalized CDR for %s - start %ld.%06ld answer %ld.%06ld end %ld.%06ld dispo %s\n",
  1131. cdr->party_a.snapshot->name,
  1132. cdr->start.tv_sec,
  1133. (long)cdr->start.tv_usec,
  1134. cdr->answer.tv_sec,
  1135. (long)cdr->answer.tv_usec,
  1136. cdr->end.tv_sec,
  1137. (long)cdr->end.tv_usec,
  1138. ast_cdr_disp2str(cdr->disposition));
  1139. }
  1140. /*!
  1141. * \brief Check to see if a CDR needs to move to the finalized state because
  1142. * its Party A hungup.
  1143. */
  1144. static void cdr_object_check_party_a_hangup(struct cdr_object *cdr)
  1145. {
  1146. RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
  1147. if (ast_test_flag(&mod_cfg->general->settings, CDR_END_BEFORE_H_EXTEN)
  1148. && ast_test_flag(&cdr->party_a.snapshot->softhangup_flags, AST_SOFTHANGUP_HANGUP_EXEC)) {
  1149. cdr_object_finalize(cdr);
  1150. }
  1151. if (ast_test_flag(&cdr->party_a.snapshot->flags, AST_FLAG_DEAD)
  1152. && cdr->fn_table != &finalized_state_fn_table) {
  1153. cdr_object_transition_state(cdr, &finalized_state_fn_table);
  1154. }
  1155. }
  1156. /*!
  1157. * \brief Check to see if a CDR needs to be answered based on its Party A.
  1158. * Note that this is safe to call as much as you want - we won't answer twice
  1159. */
  1160. static void cdr_object_check_party_a_answer(struct cdr_object *cdr) {
  1161. RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
  1162. if (cdr->party_a.snapshot->state == AST_STATE_UP && ast_tvzero(cdr->answer)) {
  1163. cdr->answer = ast_tvnow();
  1164. /* tv_usec is suseconds_t, which could be int or long */
  1165. CDR_DEBUG(mod_cfg, "%p - Set answered time to %ld.%06ld\n", cdr,
  1166. cdr->answer.tv_sec,
  1167. (long)cdr->answer.tv_usec);
  1168. }
  1169. }
  1170. /* \brief Set Caller ID information on a CDR */
  1171. static void cdr_object_update_cid(struct cdr_object_snapshot *old_snapshot, struct ast_channel_snapshot *new_snapshot)
  1172. {
  1173. if (!old_snapshot->snapshot) {
  1174. set_variable(&old_snapshot->variables, "dnid", new_snapshot->caller_dnid);
  1175. set_variable(&old_snapshot->variables, "callingsubaddr", new_snapshot->caller_subaddr);
  1176. set_variable(&old_snapshot->variables, "calledsubaddr", new_snapshot->dialed_subaddr);
  1177. return;
  1178. }
  1179. if (!strcmp(old_snapshot->snapshot->caller_dnid, new_snapshot->caller_dnid)) {
  1180. set_variable(&old_snapshot->variables, "dnid", new_snapshot->caller_dnid);
  1181. }
  1182. if (!strcmp(old_snapshot->snapshot->caller_subaddr, new_snapshot->caller_subaddr)) {
  1183. set_variable(&old_snapshot->variables, "callingsubaddr", new_snapshot->caller_subaddr);
  1184. }
  1185. if (!strcmp(old_snapshot->snapshot->dialed_subaddr, new_snapshot->dialed_subaddr)) {
  1186. set_variable(&old_snapshot->variables, "calledsubaddr", new_snapshot->dialed_subaddr);
  1187. }
  1188. }
  1189. /*!
  1190. * \brief Swap an old \ref cdr_object_snapshot's \ref ast_channel_snapshot for
  1191. * a new \ref ast_channel_snapshot
  1192. * \param old_snapshot The old \ref cdr_object_snapshot
  1193. * \param new_snapshot The new \ref ast_channel_snapshot for old_snapshot
  1194. */
  1195. static void cdr_object_swap_snapshot(struct cdr_object_snapshot *old_snapshot,
  1196. struct ast_channel_snapshot *new_snapshot)
  1197. {
  1198. cdr_object_update_cid(old_snapshot, new_snapshot);
  1199. if (old_snapshot->snapshot) {
  1200. ao2_t_ref(old_snapshot->snapshot, -1, "Drop ref for swap");
  1201. }
  1202. ao2_t_ref(new_snapshot, +1, "Bump ref for swap");
  1203. old_snapshot->snapshot = new_snapshot;
  1204. }
  1205. /* BASE METHOD IMPLEMENTATIONS */
  1206. static int base_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot)
  1207. {
  1208. RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
  1209. ast_assert(strcasecmp(snapshot->name, cdr->party_a.snapshot->name) == 0);
  1210. /* Ignore any snapshots from a dead or dying channel */
  1211. if (ast_test_flag(&snapshot->softhangup_flags, AST_SOFTHANGUP_HANGUP_EXEC)
  1212. && ast_test_flag(&mod_cfg->general->settings, CDR_END_BEFORE_H_EXTEN)) {
  1213. cdr_object_check_party_a_hangup(cdr);
  1214. return 0;
  1215. }
  1216. cdr_object_swap_snapshot(&cdr->party_a, snapshot);
  1217. /* When Party A is originated to an application and the application exits, the stack
  1218. * will attempt to clear the application and restore the dummy originate application
  1219. * of "AppDialX". Prevent that, and any other application changes we might not want
  1220. * here.
  1221. */
  1222. if (!ast_strlen_zero(snapshot->appl)
  1223. && (strncasecmp(snapshot->appl, "appdial", 7) || ast_strlen_zero(cdr->appl))
  1224. && !ast_test_flag(&cdr->flags, AST_CDR_LOCK_APP)) {
  1225. ast_string_field_set(cdr, appl, snapshot->appl);
  1226. ast_string_field_set(cdr, data, snapshot->data);
  1227. /* Dial (app_dial) is a special case. Because pre-dial handlers, which
  1228. * execute before the dial begins, will alter the application/data to
  1229. * something people typically don't want to see, if we see a channel enter
  1230. * into Dial here, we set the appl/data accordingly and lock it.
  1231. */
  1232. if (!strcmp(snapshot->appl, "Dial")) {
  1233. ast_set_flag(&cdr->flags, AST_CDR_LOCK_APP);
  1234. }
  1235. }
  1236. ast_string_field_set(cdr, linkedid, snapshot->linkedid);
  1237. cdr_object_check_party_a_answer(cdr);
  1238. cdr_object_check_party_a_hangup(cdr);
  1239. return 0;
  1240. }
  1241. static int base_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel)
  1242. {
  1243. /* In general, most things shouldn't get a bridge leave */
  1244. ast_assert(0);
  1245. return 1;
  1246. }
  1247. static int base_process_dial_end(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer, const char *dial_status)
  1248. {
  1249. /* In general, most things shouldn't get a dial end. */
  1250. ast_assert(0);
  1251. return 0;
  1252. }
  1253. static enum process_bridge_enter_results base_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel)
  1254. {
  1255. /* Base process bridge enter simply indicates that we can't handle it */
  1256. return BRIDGE_ENTER_NEED_CDR;
  1257. }
  1258. static int base_process_parked_channel(struct cdr_object *cdr, struct ast_parked_call_payload *parking_info)
  1259. {
  1260. char park_info[128];
  1261. ast_assert(!strcasecmp(parking_info->parkee->name, cdr->party_a.snapshot->name));
  1262. /* Update Party A information regardless */
  1263. cdr->fn_table->process_party_a(cdr, parking_info->parkee);
  1264. /* Fake out where we're parked */
  1265. ast_string_field_set(cdr, appl, "Park");
  1266. snprintf(park_info, sizeof(park_info), "%s:%u", parking_info->parkinglot, parking_info->parkingspace);
  1267. ast_string_field_set(cdr, data, park_info);
  1268. /* Prevent any further changes to the App/Data fields for this record */
  1269. ast_set_flag(&cdr->flags, AST_CDR_LOCK_APP);
  1270. return 0;
  1271. }
  1272. /* SINGLE STATE */
  1273. static void single_state_init_function(struct cdr_object *cdr) {
  1274. cdr->start = ast_tvnow();
  1275. cdr_object_check_party_a_answer(cdr);
  1276. }
  1277. static void single_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot)
  1278. {
  1279. /* This should never happen! */
  1280. ast_assert(cdr->party_b.snapshot == NULL);
  1281. ast_assert(0);
  1282. return;
  1283. }
  1284. static int single_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer)
  1285. {
  1286. RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
  1287. if (caller && !strcasecmp(cdr->party_a.snapshot->name, caller->name)) {
  1288. base_process_party_a(cdr, caller);
  1289. CDR_DEBUG(mod_cfg, "%p - Updated Party A %s snapshot\n", cdr,
  1290. cdr->party_a.snapshot->name);
  1291. cdr_object_swap_snapshot(&cdr->party_b, peer);
  1292. CDR_DEBUG(mod_cfg, "%p - Updated Party B %s snapshot\n", cdr,
  1293. cdr->party_b.snapshot->name);
  1294. /* If we have two parties, lock the application that caused the
  1295. * two parties to be associated. This prevents mid-call event
  1296. * macros/gosubs from perturbing the CDR application/data
  1297. */
  1298. ast_set_flag(&cdr->flags, AST_CDR_LOCK_APP);
  1299. } else if (!strcasecmp(cdr->party_a.snapshot->name, peer->name)) {
  1300. /* We're the entity being dialed, i.e., outbound origination */
  1301. base_process_party_a(cdr, peer);
  1302. CDR_DEBUG(mod_cfg, "%p - Updated Party A %s snapshot\n", cdr,
  1303. cdr->party_a.snapshot->name);
  1304. }
  1305. cdr_object_transition_state(cdr, &dial_state_fn_table);
  1306. return 0;
  1307. }
  1308. /*!
  1309. * \brief Handle a comparison between our \ref cdr_object and a \ref cdr_object
  1310. * already in the bridge while in the Single state. The goal of this is to find
  1311. * a Party B for our CDR.
  1312. *
  1313. * \param cdr Our \ref cdr_object in the Single state
  1314. * \param cand_cdr The \ref cdr_object already in the Bridge state
  1315. *
  1316. * \retval 0 The cand_cdr had a Party A or Party B that we could use as our
  1317. * Party B
  1318. * \retval 1 No party in the cand_cdr could be used as our Party B
  1319. */
  1320. static int single_state_bridge_enter_comparison(struct cdr_object *cdr,
  1321. struct cdr_object *cand_cdr)
  1322. {
  1323. RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
  1324. struct cdr_object_snapshot *party_a;
  1325. /* Don't match on ourselves */
  1326. if (!strcasecmp(cdr->party_a.snapshot->name, cand_cdr->party_a.snapshot->name)) {
  1327. return 1;
  1328. }
  1329. /* Try the candidate CDR's Party A first */
  1330. party_a = cdr_object_pick_party_a(&cdr->party_a, &cand_cdr->party_a);
  1331. if (!strcasecmp(party_a->snapshot->name, cdr->party_a.snapshot->name)) {
  1332. CDR_DEBUG(mod_cfg, "%p - Party A %s has new Party B %s\n",
  1333. cdr, cdr->party_a.snapshot->name, cand_cdr->party_a.snapshot->name);
  1334. cdr_object_snapshot_copy(&cdr->party_b, &cand_cdr->party_a);
  1335. if (!cand_cdr->party_b.snapshot) {
  1336. /* We just stole them - finalize their CDR. Note that this won't
  1337. * transition their state, it just sets the end time and the
  1338. * disposition - if we need to re-activate them later, we can.
  1339. */
  1340. cdr_object_finalize(cand_cdr);
  1341. }
  1342. return 0;
  1343. }
  1344. /* Try their Party B, unless it's us */
  1345. if (!cand_cdr->party_b.snapshot
  1346. || !strcasecmp(cdr->party_a.snapshot->name, cand_cdr->party_b.snapshot->name)) {
  1347. return 1;
  1348. }
  1349. party_a = cdr_object_pick_party_a(&cdr->party_a, &cand_cdr->party_b);
  1350. if (!strcasecmp(party_a->snapshot->name, cdr->party_a.snapshot->name)) {
  1351. CDR_DEBUG(mod_cfg, "%p - Party A %s has new Party B %s\n",
  1352. cdr, cdr->party_a.snapshot->name, cand_cdr->party_b.snapshot->name);
  1353. cdr_object_snapshot_copy(&cdr->party_b, &cand_cdr->party_b);
  1354. return 0;
  1355. }
  1356. return 1;
  1357. }
  1358. static enum process_bridge_enter_results single_state_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel)
  1359. {
  1360. struct ao2_iterator it_cdrs;
  1361. char *channel_id;
  1362. int success = 0;
  1363. ast_string_field_set(cdr, bridge, bridge->uniqueid);
  1364. if (ao2_container_count(bridge->channels) == 1) {
  1365. /* No one in the bridge yet but us! */
  1366. cdr_object_transition_state(cdr, &bridge_state_fn_table);
  1367. return BRIDGE_ENTER_ONLY_PARTY;
  1368. }
  1369. for (it_cdrs = ao2_iterator_init(bridge->channels, 0);
  1370. !success && (channel_id = ao2_iterator_next(&it_cdrs));
  1371. ao2_ref(channel_id, -1)) {
  1372. RAII_VAR(struct cdr_object *, cand_cdr_master,
  1373. ao2_find(active_cdrs_by_channel, channel_id, OBJ_KEY),
  1374. ao2_cleanup);
  1375. struct cdr_object *cand_cdr;
  1376. if (!cand_cdr_master) {
  1377. continue;
  1378. }
  1379. ao2_lock(cand_cdr_master);
  1380. for (cand_cdr = cand_cdr_master; cand_cdr; cand_cdr = cand_cdr->next) {
  1381. /* Skip any records that are not in a bridge or in this bridge.
  1382. * I'm not sure how that would happen, but it pays to be careful. */
  1383. if (cand_cdr->fn_table != &bridge_state_fn_table ||
  1384. strcmp(cdr->bridge, cand_cdr->bridge)) {
  1385. continue;
  1386. }
  1387. if (single_state_bridge_enter_comparison(cdr, cand_cdr)) {
  1388. continue;
  1389. }
  1390. /* We successfully got a party B - break out */
  1391. success = 1;
  1392. break;
  1393. }
  1394. ao2_unlock(cand_cdr_master);
  1395. }
  1396. ao2_iterator_destroy(&it_cdrs);
  1397. /* We always transition state, even if we didn't get a peer */
  1398. cdr_object_transition_state(cdr, &bridge_state_fn_table);
  1399. /* Success implies that we have a Party B */
  1400. if (success) {
  1401. return BRIDGE_ENTER_OBTAINED_PARTY_B;
  1402. }
  1403. return BRIDGE_ENTER_NO_PARTY_B;
  1404. }
  1405. static int single_state_process_parking_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel)
  1406. {
  1407. cdr_object_transition_state(cdr, &parked_state_fn_table);
  1408. return 0;
  1409. }
  1410. /* DIAL STATE */
  1411. static void dial_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot)
  1412. {
  1413. ast_assert(snapshot != NULL);
  1414. if (!cdr->party_b.snapshot
  1415. || strcasecmp(cdr->party_b.snapshot->name, snapshot->name)) {
  1416. return;
  1417. }
  1418. cdr_object_swap_snapshot(&cdr->party_b, snapshot);
  1419. /* If party B hangs up, finalize this CDR */
  1420. if (ast_test_flag(&cdr->party_b.snapshot->flags, AST_FLAG_DEAD)) {
  1421. cdr_object_transition_state(cdr, &finalized_state_fn_table);
  1422. }
  1423. }
  1424. static int dial_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer)
  1425. {
  1426. /* Don't process a begin dial here. A party A already in the dial state will
  1427. * who receives a dial begin for something else will be handled by the
  1428. * message router callback and will add a new CDR for the party A */
  1429. return 1;
  1430. }
  1431. /*!
  1432. * \internal
  1433. * \brief Convert a dial status to a CDR disposition
  1434. */
  1435. static enum ast_cdr_disposition dial_status_to_disposition(const char *dial_status)
  1436. {
  1437. RAII_VAR(struct module_config *, mod_cfg,
  1438. ao2_global_obj_ref(module_configs), ao2_cleanup);
  1439. if (!strcmp(dial_status, "ANSWER")) {
  1440. return AST_CDR_ANSWERED;
  1441. } else if (!strcmp(dial_status, "BUSY")) {
  1442. return AST_CDR_BUSY;
  1443. } else if (!strcmp(dial_status, "CANCEL") || !strcmp(dial_status, "NOANSWER")) {
  1444. return AST_CDR_NOANSWER;
  1445. } else if (!strcmp(dial_status, "CONGESTION")) {
  1446. if (!ast_test_flag(&mod_cfg->general->settings, CDR_CONGESTION)) {
  1447. return AST_CDR_FAILED;
  1448. } else {
  1449. return AST_CDR_CONGESTION;
  1450. }
  1451. } else if (!strcmp(dial_status, "FAILED")) {
  1452. return AST_CDR_FAILED;
  1453. }
  1454. return AST_CDR_FAILED;
  1455. }
  1456. static int dial_state_process_dial_end(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer, const char *dial_status)
  1457. {
  1458. RAII_VAR(struct module_config *, mod_cfg,
  1459. ao2_global_obj_ref(module_configs), ao2_cleanup);
  1460. struct ast_channel_snapshot *party_a;
  1461. if (caller) {
  1462. party_a = caller;
  1463. } else {
  1464. party_a = peer;
  1465. }
  1466. ast_assert(!strcasecmp(cdr->party_a.snapshot->name, party_a->name));
  1467. cdr_object_swap_snapshot(&cdr->party_a, party_a);
  1468. if (cdr->party_b.snapshot) {
  1469. if (strcasecmp(cdr->party_b.snapshot->name, peer->name)) {
  1470. /* Not the status for this CDR - defer back to the message router */
  1471. return 1;
  1472. }
  1473. cdr_object_swap_snapshot(&cdr->party_b, peer);
  1474. }
  1475. /* Set the disposition based on the dial string. */
  1476. cdr->disposition = dial_status_to_disposition(dial_status);
  1477. if (cdr->disposition == AST_CDR_ANSWERED) {
  1478. /* Switch to dial pending to wait and see what the caller does */
  1479. cdr_object_transition_state(cdr, &dialed_pending_state_fn_table);
  1480. } else {
  1481. cdr_object_transition_state(cdr, &finalized_state_fn_table);
  1482. }
  1483. return 0;
  1484. }
  1485. static enum process_bridge_enter_results dial_state_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel)
  1486. {
  1487. struct ao2_iterator it_cdrs;
  1488. char *channel_id;
  1489. int success = 0;
  1490. ast_string_field_set(cdr, bridge, bridge->uniqueid);
  1491. /* Get parties in the bridge */
  1492. if (ao2_container_count(bridge->channels) == 1) {
  1493. /* No one in the bridge yet but us! */
  1494. cdr_object_transition_state(cdr, &bridge_state_fn_table);
  1495. return BRIDGE_ENTER_ONLY_PARTY;
  1496. }
  1497. for (it_cdrs = ao2_iterator_init(bridge->channels, 0);
  1498. !success && (channel_id = ao2_iterator_next(&it_cdrs));
  1499. ao2_ref(channel_id, -1)) {
  1500. RAII_VAR(struct cdr_object *, cand_cdr_master,
  1501. ao2_find(active_cdrs_by_channel, channel_id, OBJ_KEY),
  1502. ao2_cleanup);
  1503. struct cdr_object *cand_cdr;
  1504. if (!cand_cdr_master) {
  1505. continue;
  1506. }
  1507. ao2_lock(cand_cdr_master);
  1508. for (cand_cdr = cand_cdr_master; cand_cdr; cand_cdr = cand_cdr->next) {
  1509. /* Skip any records that are not in a bridge or in this bridge.
  1510. * I'm not sure how that would happen, but it pays to be careful. */
  1511. if (cand_cdr->fn_table != &bridge_state_fn_table ||
  1512. strcmp(cdr->bridge, cand_cdr->bridge)) {
  1513. continue;
  1514. }
  1515. /* If we don't have a Party B (originated channel), skip it */
  1516. if (!cdr->party_b.snapshot) {
  1517. continue;
  1518. }
  1519. /* Skip any records that aren't our Party B */
  1520. if (strcasecmp(cdr->party_b.snapshot->name, cand_cdr->party_a.snapshot->name)) {
  1521. continue;
  1522. }
  1523. cdr_object_snapshot_copy(&cdr->party_b, &cand_cdr->party_a);
  1524. /* If they have a Party B, they joined up with someone else as their
  1525. * Party A. Don't finalize them as they're active. Otherwise, we
  1526. * have stolen them so they need to be finalized.
  1527. */
  1528. if (!cand_cdr->party_b.snapshot) {
  1529. cdr_object_finalize(cand_cdr);
  1530. }
  1531. success = 1;
  1532. break;
  1533. }
  1534. ao2_unlock(cand_cdr_master);
  1535. }
  1536. ao2_iterator_destroy(&it_cdrs);
  1537. /* We always transition state, even if we didn't get a peer */
  1538. cdr_object_transition_state(cdr, &bridge_state_fn_table);
  1539. /* Success implies that we have a Party B */
  1540. if (success) {
  1541. return BRIDGE_ENTER_OBTAINED_PARTY_B;
  1542. }
  1543. return BRIDGE_ENTER_NO_PARTY_B;
  1544. }
  1545. /* DIALED PENDING STATE */
  1546. static int dialed_pending_state_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot)
  1547. {
  1548. /* If we get a CEP change, we're executing dialplan. If we have a Party B
  1549. * that means we need a new CDR; otherwise, switch us over to single.
  1550. */
  1551. if (snapshot_cep_changed(cdr->party_a.snapshot, snapshot)) {
  1552. if (cdr->party_b.snapshot) {
  1553. cdr_object_transition_state(cdr, &finalized_state_fn_table);
  1554. cdr->fn_table->process_party_a(cdr, snapshot);
  1555. return 1;
  1556. } else {
  1557. cdr_object_transition_state(cdr, &single_state_fn_table);
  1558. cdr->fn_table->process_party_a(cdr, snapshot);
  1559. return 0;
  1560. }
  1561. }
  1562. base_process_party_a(cdr, snapshot);
  1563. return 0;
  1564. }
  1565. static enum process_bridge_enter_results dialed_pending_state_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel)
  1566. {
  1567. cdr_object_transition_state(cdr, &dial_state_fn_table);
  1568. return cdr->fn_table->process_bridge_enter(cdr, bridge, channel);
  1569. }
  1570. static int dialed_pending_state_process_parking_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel)
  1571. {
  1572. if (cdr->party_b.snapshot) {
  1573. /* We can't handle this as we have a Party B - ask for a new one */
  1574. return 1;
  1575. }
  1576. cdr_object_transition_state(cdr, &parked_state_fn_table);
  1577. return 0;
  1578. }
  1579. static int dialed_pending_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer)
  1580. {
  1581. cdr_object_transition_state(cdr, &finalized_state_fn_table);
  1582. /* Ask for a new CDR */
  1583. return 1;
  1584. }
  1585. /* BRIDGE STATE */
  1586. static void bridge_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot)
  1587. {
  1588. if (!cdr->party_b.snapshot
  1589. || strcasecmp(cdr->party_b.snapshot->name, snapshot->name)) {
  1590. return;
  1591. }
  1592. cdr_object_swap_snapshot(&cdr->party_b, snapshot);
  1593. /* If party B hangs up, finalize this CDR */
  1594. if (ast_test_flag(&cdr->party_b.snapshot->flags, AST_FLAG_DEAD)) {
  1595. cdr_object_transition_state(cdr, &finalized_state_fn_table);
  1596. }
  1597. }
  1598. static int bridge_state_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel)
  1599. {
  1600. if (strcmp(cdr->bridge, bridge->uniqueid)) {
  1601. return 1;
  1602. }
  1603. if (strcasecmp(cdr->party_a.snapshot->name, channel->name)
  1604. && cdr->party_b.snapshot
  1605. && strcasecmp(cdr->party_b.snapshot->name, channel->name)) {
  1606. return 1;
  1607. }
  1608. cdr_object_transition_state(cdr, &finalized_state_fn_table);
  1609. return 0;
  1610. }
  1611. /* PARKED STATE */
  1612. static int parked_state_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel)
  1613. {
  1614. if (strcasecmp(cdr->party_a.snapshot->name, channel->name)) {
  1615. return 1;
  1616. }
  1617. cdr_object_transition_state(cdr, &finalized_state_fn_table);
  1618. return 0;
  1619. }
  1620. /* FINALIZED STATE */
  1621. static void finalized_state_init_function(struct cdr_object *cdr)
  1622. {
  1623. cdr_object_finalize(cdr);
  1624. }
  1625. static int finalized_state_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot)
  1626. {
  1627. RAII_VAR(struct module_config *, mod_cfg,
  1628. ao2_global_obj_ref(module_configs), ao2_cleanup);
  1629. if (ast_test_flag(&snapshot->softhangup_flags, AST_SOFTHANGUP_HANGUP_EXEC)
  1630. && ast_test_flag(&mod_cfg->general->settings, CDR_END_BEFORE_H_EXTEN)) {
  1631. return 0;
  1632. }
  1633. /* Indicate that, if possible, we should get a new CDR */
  1634. return 1;
  1635. }
  1636. /*!
  1637. * \internal
  1638. * \brief Filter channel snapshots by technology
  1639. */
  1640. static int filter_channel_snapshot(struct ast_channel_snapshot *snapshot)
  1641. {
  1642. return snapshot->tech_properties & AST_CHAN_TP_INTERNAL;
  1643. }
  1644. /*!
  1645. * \internal
  1646. * \brief Filter a channel cache update
  1647. */
  1648. static int filter_channel_cache_message(struct ast_channel_snapshot *old_snapshot,
  1649. struct ast_channel_snapshot *new_snapshot)
  1650. {
  1651. int ret = 0;
  1652. /* Drop cache updates from certain channel technologies */
  1653. if (old_snapshot) {
  1654. ret |= filter_channel_snapshot(old_snapshot);
  1655. }
  1656. if (new_snapshot) {
  1657. ret |= filter_channel_snapshot(new_snapshot);
  1658. }
  1659. return ret;
  1660. }
  1661. /* TOPIC ROUTER CALLBACKS */
  1662. /*!
  1663. * \brief Handler for Stasis-Core dial messages
  1664. * \param data Passed on
  1665. * \param sub The stasis subscription for this message callback
  1666. * \param topic The topic this message was published for
  1667. * \param message The message
  1668. */
  1669. static void handle_dial_message(void *data, struct stasis_subscription *sub, struct stasis_message *message)
  1670. {
  1671. RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
  1672. RAII_VAR(struct cdr_object *, cdr, NULL, ao2_cleanup);
  1673. struct ast_multi_channel_blob *payload = stasis_message_data(message);
  1674. struct ast_channel_snapshot *caller;
  1675. struct ast_channel_snapshot *peer;
  1676. struct cdr_object *it_cdr;
  1677. struct ast_json *dial_status_blob;
  1678. const char *dial_status = NULL;
  1679. int res = 1;
  1680. caller = ast_multi_channel_blob_get_channel(payload, "caller");
  1681. peer = ast_multi_channel_blob_get_channel(payload, "peer");
  1682. if (!peer && !caller) {
  1683. return;
  1684. }
  1685. dial_status_blob = ast_json_object_get(ast_multi_channel_blob_get_json(payload), "dialstatus");
  1686. if (dial_status_blob) {
  1687. dial_status = ast_json_string_get(dial_status_blob);
  1688. }
  1689. CDR_DEBUG(mod_cfg, "Dial %s message for %s, %s: %u.%08u\n",
  1690. ast_strlen_zero(dial_status) ? "Begin" : "End",
  1691. caller ? caller->name : "(none)",
  1692. peer ? peer->name : "(none)",
  1693. (unsigned int)stasis_message_timestamp(message)->tv_sec,
  1694. (unsigned int)stasis_message_timestamp(message)->tv_usec);
  1695. if (filter_channel_snapshot(peer) || (caller && filter_channel_snapshot(caller))) {
  1696. return;
  1697. }
  1698. /* Figure out who is running this show */
  1699. if (caller) {
  1700. cdr = ao2_find(active_cdrs_by_channel, caller->uniqueid, OBJ_KEY);
  1701. } else {
  1702. cdr = ao2_find(active_cdrs_by_channel, peer->uniqueid, OBJ_KEY);
  1703. }
  1704. if (!cdr) {
  1705. ast_log(AST_LOG_WARNING, "No CDR for channel %s\n", caller ? caller->name : peer->name);
  1706. ast_assert(0);
  1707. return;
  1708. }
  1709. ao2_lock(cdr);
  1710. for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
  1711. if (ast_strlen_zero(dial_status)) {
  1712. if (!it_cdr->fn_table->process_dial_begin) {
  1713. continue;
  1714. }
  1715. CDR_DEBUG(mod_cfg, "%p - Processing Dial Begin message for channel %s, peer %s\n",
  1716. it_cdr,
  1717. caller ? caller->name : "(none)",
  1718. peer ? peer->name : "(none)");
  1719. res &= it_cdr->fn_table->process_dial_begin(it_cdr,
  1720. caller,
  1721. peer);
  1722. } else {
  1723. if (!it_cdr->fn_table->process_dial_end) {
  1724. continue;
  1725. }
  1726. CDR_DEBUG(mod_cfg, "%p - Processing Dial End message for channel %s, peer %s\n",
  1727. it_cdr,
  1728. caller ? caller->name : "(none)",
  1729. peer ? peer->name : "(none)");
  1730. it_cdr->fn_table->process_dial_end(it_cdr,
  1731. caller,
  1732. peer,
  1733. dial_status);
  1734. }
  1735. }
  1736. /* If no CDR handled a dial begin message, make a new one */
  1737. if (res && ast_strlen_zero(dial_status)) {
  1738. struct cdr_object *new_cdr;
  1739. new_cdr = cdr_object_create_and_append(cdr);
  1740. if (!new_cdr) {
  1741. ao2_unlock(cdr);
  1742. return;
  1743. }
  1744. new_cdr->fn_table->process_dial_begin(new_cdr,
  1745. caller,
  1746. peer);
  1747. }
  1748. ao2_unlock(cdr);
  1749. }
  1750. static int cdr_object_finalize_party_b(void *obj, void *arg, int flags)
  1751. {
  1752. struct cdr_object *cdr = obj;
  1753. struct ast_channel_snapshot *party_b = arg;
  1754. struct cdr_object *it_cdr;
  1755. for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
  1756. if (it_cdr->party_b.snapshot
  1757. && !strcasecmp(it_cdr->party_b.snapshot->name, party_b->name)) {
  1758. /* Don't transition to the finalized state - let the Party A do
  1759. * that when its ready
  1760. */
  1761. cdr_object_finalize(it_cdr);
  1762. }
  1763. }
  1764. return 0;
  1765. }
  1766. static int cdr_object_update_party_b(void *obj, void *arg, int flags)
  1767. {
  1768. struct cdr_object *cdr = obj;
  1769. struct ast_channel_snapshot *party_b = arg;
  1770. struct cdr_object *it_cdr;
  1771. for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
  1772. if (!it_cdr->fn_table->process_party_b) {
  1773. continue;
  1774. }
  1775. if (it_cdr->party_b.snapshot
  1776. && !strcasecmp(it_cdr->party_b.snapshot->name, party_b->name)) {
  1777. it_cdr->fn_table->process_party_b(it_cdr, party_b);
  1778. }
  1779. }
  1780. return 0;
  1781. }
  1782. /*! \brief Determine if we need to add a new CDR based on snapshots */
  1783. static int check_new_cdr_needed(struct ast_channel_snapshot *old_snapshot,
  1784. struct ast_channel_snapshot *new_snapshot)
  1785. {
  1786. RAII_VAR(struct module_config *, mod_cfg,
  1787. ao2_global_obj_ref(module_configs), ao2_cleanup);
  1788. /* If we're dead, we don't need a new CDR */
  1789. if (!new_snapshot
  1790. || (ast_test_flag(&new_snapshot->softhangup_flags, AST_SOFTHANGUP_HANGUP_EXEC)
  1791. && ast_test_flag(&mod_cfg->general->settings, CDR_END_BEFORE_H_EXTEN))) {
  1792. return 0;
  1793. }
  1794. /* Auto-fall through will increment the priority but have no application */
  1795. if (ast_strlen_zero(new_snapshot->appl)) {
  1796. return 0;
  1797. }
  1798. if (old_snapshot && !snapshot_cep_changed(old_snapshot, new_snapshot)) {
  1799. return 0;
  1800. }
  1801. return 1;
  1802. }
  1803. /*!
  1804. * \brief Handler for Stasis-Core channel cache update messages
  1805. * \param data Passed on
  1806. * \param sub The stasis subscription for this message callback
  1807. * \param topic The topic this message was published for
  1808. * \param message The message
  1809. */
  1810. static void handle_channel_cache_message(void *data, struct stasis_subscription *sub, struct stasis_message *message)
  1811. {
  1812. RAII_VAR(struct cdr_object *, cdr, NULL, ao2_cleanup);
  1813. RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
  1814. struct stasis_cache_update *update = stasis_message_data(message);
  1815. struct ast_channel_snapshot *old_snapshot;
  1816. struct ast_channel_snapshot *new_snapshot;
  1817. const char *uniqueid;
  1818. const char *name;
  1819. struct cdr_object *it_cdr;
  1820. ast_assert(update != NULL);
  1821. ast_assert(ast_channel_snapshot_type() == update->type);
  1822. old_snapshot = stasis_message_data(update->old_snapshot);
  1823. new_snapshot = stasis_message_data(update->new_snapshot);
  1824. uniqueid = new_snapshot ? new_snapshot->uniqueid : old_snapshot->uniqueid;
  1825. name = new_snapshot ? new_snapshot->name : old_snapshot->name;
  1826. if (filter_channel_cache_message(old_snapshot, new_snapshot)) {
  1827. return;
  1828. }
  1829. if (new_snapshot && !old_snapshot) {
  1830. cdr = cdr_object_alloc(new_snapshot);
  1831. if (!cdr) {
  1832. return;
  1833. }
  1834. ao2_link(active_cdrs_by_channel, cdr);
  1835. }
  1836. /* Handle Party A */
  1837. if (!cdr) {
  1838. cdr = ao2_find(active_cdrs_by_channel, uniqueid, OBJ_KEY);
  1839. }
  1840. if (!cdr) {
  1841. ast_log(AST_LOG_WARNING, "No CDR for channel %s\n", name);
  1842. ast_assert(0);
  1843. } else {
  1844. ao2_lock(cdr);
  1845. if (new_snapshot) {
  1846. int all_reject = 1;
  1847. for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
  1848. if (!it_cdr->fn_table->process_party_a) {
  1849. continue;
  1850. }
  1851. all_reject &= it_cdr->fn_table->process_party_a(it_cdr, new_snapshot);
  1852. }
  1853. if (all_reject && check_new_cdr_needed(old_snapshot, new_snapshot)) {
  1854. /* We're not hung up and we have a new snapshot - we need a new CDR */
  1855. struct cdr_object *new_cdr;
  1856. new_cdr = cdr_object_create_and_append(cdr);
  1857. if (new_cdr) {
  1858. new_cdr->fn_table->process_party_a(new_cdr, new_snapshot);
  1859. }
  1860. }
  1861. } else {
  1862. CDR_DEBUG(mod_cfg, "%p - Beginning finalize/dispatch for %s\n", cdr, old_snapshot->name);
  1863. for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
  1864. cdr_object_finalize(it_cdr);
  1865. }
  1866. cdr_object_dispatch(cdr);
  1867. ao2_unlink(active_cdrs_by_channel, cdr);
  1868. }
  1869. ao2_unlock(cdr);
  1870. }
  1871. /* Handle Party B */
  1872. if (new_snapshot) {
  1873. ao2_callback(active_cdrs_by_channel, OBJ_NODATA, cdr_object_update_party_b,
  1874. new_snapshot);
  1875. } else {
  1876. ao2_callback(active_cdrs_by_channel, OBJ_NODATA, cdr_object_finalize_party_b,
  1877. old_snapshot);
  1878. }
  1879. }
  1880. struct bridge_leave_data {
  1881. struct ast_bridge_snapshot *bridge;
  1882. struct ast_channel_snapshot *channel;
  1883. };
  1884. /*! \brief Callback used to notify CDRs of a Party B leaving the bridge */
  1885. static int cdr_object_party_b_left_bridge_cb(void *obj, void *arg, int flags)
  1886. {
  1887. struct cdr_object *cdr = obj;
  1888. struct bridge_leave_data *leave_data = arg;
  1889. struct cdr_object *it_cdr;
  1890. if (strcmp(cdr->bridge, leave_data->bridge->uniqueid)) {
  1891. return 0;
  1892. }
  1893. for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
  1894. if (it_cdr->fn_table != &bridge_state_fn_table) {
  1895. continue;
  1896. }
  1897. if (!it_cdr->party_b.snapshot) {
  1898. continue;
  1899. }
  1900. if (strcasecmp(it_cdr->party_b.snapshot->name, leave_data->channel->name)) {
  1901. continue;
  1902. }
  1903. /* It is our Party B, in our bridge. Set the end time and let the handler
  1904. * transition our CDR appropriately when we leave the bridge.
  1905. */
  1906. cdr_object_finalize(it_cdr);
  1907. }
  1908. return 0;
  1909. }
  1910. /*! \brief Filter bridge messages based on bridge technology */
  1911. static int filter_bridge_messages(struct ast_bridge_snapshot *bridge)
  1912. {
  1913. /* Ignore holding bridge technology messages. We treat this simply as an application
  1914. * that a channel enters into.
  1915. */
  1916. if (!strcmp(bridge->technology, "holding_bridge") && strcmp(bridge->subclass, "parking")) {
  1917. return 1;
  1918. }
  1919. return 0;
  1920. }
  1921. /*!
  1922. * \brief Handler for when a channel leaves a bridge
  1923. * \param data Passed on
  1924. * \param sub The stasis subscription for this message callback
  1925. * \param topic The topic this message was published for
  1926. * \param message The message - hopefully a bridge one!
  1927. */
  1928. static void handle_bridge_leave_message(void *data, struct stasis_subscription *sub,
  1929. struct stasis_message *message)
  1930. {
  1931. struct ast_bridge_blob *update = stasis_message_data(message);
  1932. struct ast_bridge_snapshot *bridge = update->bridge;
  1933. struct ast_channel_snapshot *channel = update->channel;
  1934. RAII_VAR(struct module_config *, mod_cfg,
  1935. ao2_global_obj_ref(module_configs), ao2_cleanup);
  1936. RAII_VAR(struct cdr_object *, cdr,
  1937. ao2_find(active_cdrs_by_channel, channel->uniqueid, OBJ_KEY),
  1938. ao2_cleanup);
  1939. struct cdr_object *it_cdr;
  1940. struct bridge_leave_data leave_data = {
  1941. .bridge = bridge,
  1942. .channel = channel,
  1943. };
  1944. int left_bridge = 0;
  1945. if (filter_bridge_messages(bridge)) {
  1946. return;
  1947. }
  1948. if (filter_channel_snapshot(channel)) {
  1949. return;
  1950. }
  1951. CDR_DEBUG(mod_cfg, "Bridge Leave message for %s: %u.%08u\n",
  1952. channel->name,
  1953. (unsigned int)stasis_message_timestamp(message)->tv_sec,
  1954. (unsigned int)stasis_message_timestamp(message)->tv_usec);
  1955. if (!cdr) {
  1956. ast_log(AST_LOG_WARNING, "No CDR for channel %s\n", channel->name);
  1957. ast_assert(0);
  1958. return;
  1959. }
  1960. /* Party A */
  1961. ao2_lock(cdr);
  1962. for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
  1963. if (!it_cdr->fn_table->process_bridge_leave) {
  1964. continue;
  1965. }
  1966. CDR_DEBUG(mod_cfg, "%p - Processing Bridge Leave for %s\n",
  1967. it_cdr, channel->name);
  1968. if (!it_cdr->fn_table->process_bridge_leave(it_cdr, bridge, channel)) {
  1969. ast_string_field_set(it_cdr, bridge, "");
  1970. left_bridge = 1;
  1971. }
  1972. }
  1973. ao2_unlock(cdr);
  1974. if (!left_bridge) {
  1975. return;
  1976. }
  1977. if (strcmp(bridge->subclass, "parking")) {
  1978. /* Party B */
  1979. ao2_callback(active_cdrs_by_channel, OBJ_NODATA,
  1980. cdr_object_party_b_left_bridge_cb,
  1981. &leave_data);
  1982. }
  1983. }
  1984. /*!
  1985. * \internal
  1986. * \brief Create a new CDR, append it to an existing CDR, and update its snapshots
  1987. *
  1988. * \note The new CDR will be automatically transitioned to the bridge state
  1989. */
  1990. static void bridge_candidate_add_to_cdr(struct cdr_object *cdr,
  1991. struct cdr_object_snapshot *party_b)
  1992. {
  1993. RAII_VAR(struct module_config *, mod_cfg,
  1994. ao2_global_obj_ref(module_configs), ao2_cleanup);
  1995. struct cdr_object *new_cdr;
  1996. new_cdr = cdr_object_create_and_append(cdr);
  1997. if (!new_cdr) {
  1998. return;
  1999. }
  2000. cdr_object_snapshot_copy(&new_cdr->party_b, party_b);
  2001. cdr_object_check_party_a_answer(new_cdr);
  2002. ast_string_field_set(new_cdr, bridge, cdr->bridge);
  2003. cdr_object_transition_state(new_cdr, &bridge_state_fn_table);
  2004. CDR_DEBUG(mod_cfg, "%p - Party A %s has new Party B %s\n",
  2005. new_cdr, new_cdr->party_a.snapshot->name,
  2006. party_b->snapshot->name);
  2007. }
  2008. /*!
  2009. * \brief Process a single \ref bridge_candidate
  2010. *
  2011. * When a CDR enters a bridge, it needs to make pairings with everyone else
  2012. * that it is not currently paired with. This function determines, for the
  2013. * CDR for the channel that entered the bridge and the CDR for every other
  2014. * channel currently in the bridge, who is Party A and makes new CDRs.
  2015. *
  2016. * \param cdr The \ref cdr_obj being processed
  2017. * \param cand_cdr The \ref cdr_object that is a candidate
  2018. *
  2019. */
  2020. static int bridge_candidate_process(struct cdr_object *cdr, struct cdr_object *base_cand_cdr)
  2021. {
  2022. RAII_VAR(struct module_config *, mod_cfg,
  2023. ao2_global_obj_ref(module_configs), ao2_cleanup);
  2024. struct cdr_object_snapshot *party_a;
  2025. struct cdr_object *cand_cdr;
  2026. SCOPED_AO2LOCK(lock, base_cand_cdr);
  2027. for (cand_cdr = base_cand_cdr; cand_cdr; cand_cdr = cand_cdr->next) {
  2028. /* Skip any records that are not in this bridge */
  2029. if (strcmp(cand_cdr->bridge, cdr->bridge)) {
  2030. continue;
  2031. }
  2032. /* If the candidate is us or someone we've taken on, pass on by */
  2033. if (!strcasecmp(cdr->party_a.snapshot->name, cand_cdr->party_a.snapshot->name)
  2034. || (cdr->party_b.snapshot
  2035. && !strcasecmp(cdr->party_b.snapshot->name, cand_cdr->party_a.snapshot->name))) {
  2036. return 0;
  2037. }
  2038. party_a = cdr_object_pick_party_a(&cdr->party_a, &cand_cdr->party_a);
  2039. /* We're party A - make a new CDR, append it to us, and set the candidate as
  2040. * Party B */
  2041. if (!strcasecmp(party_a->snapshot->name, cdr->party_a.snapshot->name)) {
  2042. bridge_candidate_add_to_cdr(cdr, &cand_cdr->party_a);
  2043. return 0;
  2044. }
  2045. /* We're Party B. Check if we can add ourselves immediately or if we need
  2046. * a new CDR for them (they already have a Party B) */
  2047. if (cand_cdr->party_b.snapshot
  2048. && strcasecmp(cand_cdr->party_b.snapshot->name, cdr->party_a.snapshot->name)) {
  2049. bridge_candidate_add_to_cdr(cand_cdr, &cdr->party_a);
  2050. } else {
  2051. CDR_DEBUG(mod_cfg, "%p - Party A %s has new Party B %s\n",
  2052. cand_cdr, cand_cdr->party_a.snapshot->name,
  2053. cdr->party_a.snapshot->name);
  2054. cdr_object_snapshot_copy(&cand_cdr->party_b, &cdr->party_a);
  2055. /* It's possible that this joined at one point and was never chosen
  2056. * as party A. Clear their end time, as it would be set in such a
  2057. * case.
  2058. */
  2059. memset(&cand_cdr->end, 0, sizeof(cand_cdr->end));
  2060. }
  2061. }
  2062. return 0;
  2063. }
  2064. /*!
  2065. * \brief Handle creating bridge pairings for the \ref cdr_object that just
  2066. * entered a bridge
  2067. * \param cdr The \ref cdr_object that just entered the bridge
  2068. * \param bridge The \ref ast_bridge_snapshot representing the bridge it just entered
  2069. */
  2070. static void handle_bridge_pairings(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge)
  2071. {
  2072. struct ao2_iterator it_channels;
  2073. char *channel_id;
  2074. it_channels = ao2_iterator_init(bridge->channels, 0);
  2075. while ((channel_id = ao2_iterator_next(&it_channels))) {
  2076. RAII_VAR(struct cdr_object *, cand_cdr,
  2077. ao2_find(active_cdrs_by_channel, channel_id, OBJ_KEY),
  2078. ao2_cleanup);
  2079. if (!cand_cdr) {
  2080. ao2_ref(channel_id, -1);
  2081. continue;
  2082. }
  2083. bridge_candidate_process(cdr, cand_cdr);
  2084. ao2_ref(channel_id, -1);
  2085. }
  2086. ao2_iterator_destroy(&it_channels);
  2087. }
  2088. /*! \brief Handle entering into a parking bridge
  2089. * \param cdr The CDR to operate on
  2090. * \param bridge The bridge the channel just entered
  2091. * \param channel The channel snapshot
  2092. */
  2093. static void handle_parking_bridge_enter_message(struct cdr_object *cdr,
  2094. struct ast_bridge_snapshot *bridge,
  2095. struct ast_channel_snapshot *channel)
  2096. {
  2097. RAII_VAR(struct module_config *, mod_cfg,
  2098. ao2_global_obj_ref(module_configs), ao2_cleanup);
  2099. int res = 1;
  2100. struct cdr_object *it_cdr;
  2101. struct cdr_object *new_cdr;
  2102. ao2_lock(cdr);
  2103. for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
  2104. if (it_cdr->fn_table->process_parking_bridge_enter) {
  2105. res &= it_cdr->fn_table->process_parking_bridge_enter(it_cdr, bridge, channel);
  2106. }
  2107. if (it_cdr->fn_table->process_party_a) {
  2108. CDR_DEBUG(mod_cfg, "%p - Updating Party A %s snapshot\n", it_cdr,
  2109. channel->name);
  2110. it_cdr->fn_table->process_party_a(it_cdr, channel);
  2111. }
  2112. }
  2113. if (res) {
  2114. /* No one handled it - we need a new one! */
  2115. new_cdr = cdr_object_create_and_append(cdr);
  2116. if (new_cdr) {
  2117. /* Let the single state transition us to Parked */
  2118. cdr_object_transition_state(new_cdr, &single_state_fn_table);
  2119. new_cdr->fn_table->process_parking_bridge_enter(new_cdr, bridge, channel);
  2120. }
  2121. }
  2122. ao2_unlock(cdr);
  2123. }
  2124. /*! \brief Handle a bridge enter message for a 'normal' bridge
  2125. * \param cdr The CDR to operate on
  2126. * \param bridge The bridge the channel just entered
  2127. * \param channel The channel snapshot
  2128. */
  2129. static void handle_standard_bridge_enter_message(struct cdr_object *cdr,
  2130. struct ast_bridge_snapshot *bridge,
  2131. struct ast_channel_snapshot *channel)
  2132. {
  2133. RAII_VAR(struct module_config *, mod_cfg,
  2134. ao2_global_obj_ref(module_configs), ao2_cleanup);
  2135. enum process_bridge_enter_results result;
  2136. struct cdr_object *it_cdr;
  2137. struct cdr_object *new_cdr;
  2138. struct cdr_object *handled_cdr = NULL;
  2139. ao2_lock(cdr);
  2140. for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
  2141. if (it_cdr->fn_table->process_party_a) {
  2142. CDR_DEBUG(mod_cfg, "%p - Updating Party A %s snapshot\n", it_cdr,
  2143. channel->name);
  2144. it_cdr->fn_table->process_party_a(it_cdr, channel);
  2145. }
  2146. /* Notify all states that they have entered a bridge */
  2147. if (it_cdr->fn_table->process_bridge_enter) {
  2148. CDR_DEBUG(mod_cfg, "%p - Processing bridge enter for %s\n", it_cdr,
  2149. channel->name);
  2150. result = it_cdr->fn_table->process_bridge_enter(it_cdr, bridge, channel);
  2151. switch (result) {
  2152. case BRIDGE_ENTER_ONLY_PARTY:
  2153. /* Fall through */
  2154. case BRIDGE_ENTER_OBTAINED_PARTY_B:
  2155. if (!handled_cdr) {
  2156. handled_cdr = it_cdr;
  2157. }
  2158. break;
  2159. case BRIDGE_ENTER_NEED_CDR:
  2160. /* Pass */
  2161. break;
  2162. case BRIDGE_ENTER_NO_PARTY_B:
  2163. /* We didn't win on any - end this CDR. If someone else comes in later
  2164. * that is Party B to this CDR, it can re-activate this CDR.
  2165. */
  2166. if (!handled_cdr) {
  2167. handled_cdr = it_cdr;
  2168. }
  2169. cdr_object_finalize(cdr);
  2170. break;
  2171. }
  2172. }
  2173. }
  2174. /* Create the new matchings, but only for either:
  2175. * * The first CDR in the chain that handled it. This avoids issues with
  2176. * forked CDRs.
  2177. * * If no one handled it, the last CDR in the chain. This would occur if
  2178. * a CDR joined a bridge and it wasn't Party A for anyone. We still need
  2179. * to make pairings with everyone in the bridge.
  2180. */
  2181. if (handled_cdr) {
  2182. handle_bridge_pairings(handled_cdr, bridge);
  2183. } else {
  2184. /* Nothing handled it - we need a new one! */
  2185. new_cdr = cdr_object_create_and_append(cdr);
  2186. if (new_cdr) {
  2187. /* This is guaranteed to succeed: the new CDR is created in the single state
  2188. * and will be able to handle the bridge enter message
  2189. */
  2190. handle_standard_bridge_enter_message(cdr, bridge, channel);
  2191. }
  2192. }
  2193. ao2_unlock(cdr);
  2194. }
  2195. /*!
  2196. * \internal
  2197. * \brief Handler for Stasis-Core bridge enter messages
  2198. * \param data Passed on
  2199. * \param sub The stasis subscription for this message callback
  2200. * \param topic The topic this message was published for
  2201. * \param message The message - hopefully a bridge one!
  2202. */
  2203. static void handle_bridge_enter_message(void *data, struct stasis_subscription *sub,
  2204. struct stasis_message *message)
  2205. {
  2206. struct ast_bridge_blob *update = stasis_message_data(message);
  2207. struct ast_bridge_snapshot *bridge = update->bridge;
  2208. struct ast_channel_snapshot *channel = update->channel;
  2209. RAII_VAR(struct cdr_object *, cdr,
  2210. ao2_find(active_cdrs_by_channel, channel->uniqueid, OBJ_KEY),
  2211. ao2_cleanup);
  2212. RAII_VAR(struct module_config *, mod_cfg,
  2213. ao2_global_obj_ref(module_configs), ao2_cleanup);
  2214. if (filter_bridge_messages(bridge)) {
  2215. return;
  2216. }
  2217. if (filter_channel_snapshot(channel)) {
  2218. return;
  2219. }
  2220. CDR_DEBUG(mod_cfg, "Bridge Enter message for channel %s: %u.%08u\n",
  2221. channel->name,
  2222. (unsigned int)stasis_message_timestamp(message)->tv_sec,
  2223. (unsigned int)stasis_message_timestamp(message)->tv_usec);
  2224. if (!cdr) {
  2225. ast_log(AST_LOG_WARNING, "No CDR for channel %s\n", channel->name);
  2226. ast_assert(0);
  2227. return;
  2228. }
  2229. if (!strcmp(bridge->subclass, "parking")) {
  2230. handle_parking_bridge_enter_message(cdr, bridge, channel);
  2231. } else {
  2232. handle_standard_bridge_enter_message(cdr, bridge, channel);
  2233. }
  2234. }
  2235. /*!
  2236. * \brief Handler for when a channel is parked
  2237. * \param data Passed on
  2238. * \param sub The stasis subscription for this message callback
  2239. * \param topic The topic this message was published for
  2240. * \param message The message about who got parked
  2241. * */
  2242. static void handle_parked_call_message(void *data, struct stasis_subscription *sub,
  2243. struct stasis_message *message)
  2244. {
  2245. struct ast_parked_call_payload *payload = stasis_message_data(message);
  2246. struct ast_channel_snapshot *channel = payload->parkee;
  2247. RAII_VAR(struct cdr_object *, cdr, NULL, ao2_cleanup);
  2248. RAII_VAR(struct module_config *, mod_cfg,
  2249. ao2_global_obj_ref(module_configs), ao2_cleanup);
  2250. int unhandled = 1;
  2251. struct cdr_object *it_cdr;
  2252. /* Anything other than getting parked will be handled by other updates */
  2253. if (payload->event_type != PARKED_CALL) {
  2254. return;
  2255. }
  2256. /* No one got parked? */
  2257. if (!channel) {
  2258. return;
  2259. }
  2260. if (filter_channel_snapshot(channel)) {
  2261. return;
  2262. }
  2263. CDR_DEBUG(mod_cfg, "Parked Call message for channel %s: %u.%08u\n",
  2264. channel->name,
  2265. (unsigned int)stasis_message_timestamp(message)->tv_sec,
  2266. (unsigned int)stasis_message_timestamp(message)->tv_usec);
  2267. cdr = ao2_find(active_cdrs_by_channel, channel->uniqueid, OBJ_KEY);
  2268. if (!cdr) {
  2269. ast_log(AST_LOG_WARNING, "No CDR for channel %s\n", channel->name);
  2270. ast_assert(0);
  2271. return;
  2272. }
  2273. ao2_lock(cdr);
  2274. for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
  2275. if (it_cdr->fn_table->process_parked_channel) {
  2276. unhandled &= it_cdr->fn_table->process_parked_channel(it_cdr, payload);
  2277. }
  2278. }
  2279. if (unhandled) {
  2280. /* Nothing handled the messgae - we need a new one! */
  2281. struct cdr_object *new_cdr = cdr_object_create_and_append(cdr);
  2282. if (new_cdr) {
  2283. /* As the new CDR is created in the single state, it is guaranteed
  2284. * to have a function for the parked call message and will handle
  2285. * the message */
  2286. new_cdr->fn_table->process_parked_channel(new_cdr, payload);
  2287. }
  2288. }
  2289. ao2_unlock(cdr);
  2290. }
  2291. /*!
  2292. * \brief Handler for a synchronization message
  2293. * \param data Passed on
  2294. * \param sub The stasis subscription for this message callback
  2295. * \param topic The topic this message was published for
  2296. * \param message A blank ao2 object
  2297. * */
  2298. static void handle_cdr_sync_message(void *data, struct stasis_subscription *sub,
  2299. struct stasis_message *message)
  2300. {
  2301. return;
  2302. }
  2303. struct ast_cdr_config *ast_cdr_get_config(void)
  2304. {
  2305. RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
  2306. ao2_ref(mod_cfg->general, +1);
  2307. return mod_cfg->general;
  2308. }
  2309. void ast_cdr_set_config(struct ast_cdr_config *config)
  2310. {
  2311. RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
  2312. ao2_cleanup(mod_cfg->general);
  2313. mod_cfg->general = config;
  2314. ao2_ref(mod_cfg->general, +1);
  2315. cdr_toggle_runtime_options();
  2316. }
  2317. int ast_cdr_is_enabled(void)
  2318. {
  2319. RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
  2320. return ast_test_flag(&mod_cfg->general->settings, CDR_ENABLED);
  2321. }
  2322. int ast_cdr_backend_suspend(const char *name)
  2323. {
  2324. int success = -1;
  2325. struct cdr_beitem *i = NULL;
  2326. AST_RWLIST_WRLOCK(&be_list);
  2327. AST_RWLIST_TRAVERSE(&be_list, i, list) {
  2328. if (!strcasecmp(name, i->name)) {
  2329. ast_debug(3, "Suspending CDR backend %s\n", i->name);
  2330. i->suspended = 1;
  2331. success = 0;
  2332. }
  2333. }
  2334. AST_RWLIST_UNLOCK(&be_list);
  2335. return success;
  2336. }
  2337. int ast_cdr_backend_unsuspend(const char *name)
  2338. {
  2339. int success = -1;
  2340. struct cdr_beitem *i = NULL;
  2341. AST_RWLIST_WRLOCK(&be_list);
  2342. AST_RWLIST_TRAVERSE(&be_list, i, list) {
  2343. if (!strcasecmp(name, i->name)) {
  2344. ast_debug(3, "Unsuspending CDR backend %s\n", i->name);
  2345. i->suspended = 0;
  2346. success = 0;
  2347. }
  2348. }
  2349. AST_RWLIST_UNLOCK(&be_list);
  2350. return success;
  2351. }
  2352. int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
  2353. {
  2354. struct cdr_beitem *i = NULL;
  2355. if (!name)
  2356. return -1;
  2357. if (!be) {
  2358. ast_log(LOG_WARNING, "CDR engine '%s' lacks backend\n", name);
  2359. return -1;
  2360. }
  2361. AST_RWLIST_WRLOCK(&be_list);
  2362. AST_RWLIST_TRAVERSE(&be_list, i, list) {
  2363. if (!strcasecmp(name, i->name)) {
  2364. ast_log(LOG_WARNING, "Already have a CDR backend called '%s'\n", name);
  2365. AST_RWLIST_UNLOCK(&be_list);
  2366. return -1;
  2367. }
  2368. }
  2369. if (!(i = ast_calloc(1, sizeof(*i))))
  2370. return -1;
  2371. i->be = be;
  2372. ast_copy_string(i->name, name, sizeof(i->name));
  2373. ast_copy_string(i->desc, desc, sizeof(i->desc));
  2374. AST_RWLIST_INSERT_HEAD(&be_list, i, list);
  2375. AST_RWLIST_UNLOCK(&be_list);
  2376. return 0;
  2377. }
  2378. int ast_cdr_unregister(const char *name)
  2379. {
  2380. struct cdr_beitem *match = NULL;
  2381. int active_count;
  2382. AST_RWLIST_WRLOCK(&be_list);
  2383. AST_RWLIST_TRAVERSE(&be_list, match, list) {
  2384. if (!strcasecmp(name, match->name)) {
  2385. break;
  2386. }
  2387. }
  2388. if (!match) {
  2389. AST_RWLIST_UNLOCK(&be_list);
  2390. return 0;
  2391. }
  2392. active_count = ao2_container_count(active_cdrs_by_channel);
  2393. if (!match->suspended && active_count != 0) {
  2394. AST_RWLIST_UNLOCK(&be_list);
  2395. ast_log(AST_LOG_WARNING, "Unable to unregister CDR backend %s; %d CDRs are still active\n",
  2396. name, active_count);
  2397. return -1;
  2398. }
  2399. AST_RWLIST_REMOVE(&be_list, match, list);
  2400. AST_RWLIST_UNLOCK(&be_list);
  2401. ast_verb(2, "Unregistered '%s' CDR backend\n", name);
  2402. ast_free(match);
  2403. return 0;
  2404. }
  2405. struct ast_cdr *ast_cdr_dup(struct ast_cdr *cdr)
  2406. {
  2407. struct ast_cdr *newcdr;
  2408. if (!cdr) {
  2409. return NULL;
  2410. }
  2411. newcdr = ast_cdr_alloc();
  2412. if (!newcdr) {
  2413. return NULL;
  2414. }
  2415. *newcdr = *cdr;
  2416. AST_LIST_HEAD_INIT_NOLOCK(&newcdr->varshead);
  2417. copy_variables(&newcdr->varshead, &cdr->varshead);
  2418. newcdr->next = NULL;
  2419. return newcdr;
  2420. }
  2421. static const char *cdr_format_var_internal(struct ast_cdr *cdr, const char *name)
  2422. {
  2423. struct ast_var_t *variables;
  2424. if (ast_strlen_zero(name)) {
  2425. return NULL;
  2426. }
  2427. AST_LIST_TRAVERSE(&cdr->varshead, variables, entries) {
  2428. if (!strcasecmp(name, ast_var_name(variables))) {
  2429. return ast_var_value(variables);
  2430. }
  2431. }
  2432. return NULL;
  2433. }
  2434. static void cdr_get_tv(struct timeval when, const char *fmt, char *buf, int bufsize)
  2435. {
  2436. if (fmt == NULL) { /* raw mode */
  2437. snprintf(buf, bufsize, "%ld.%06ld", (long)when.tv_sec, (long)when.tv_usec);
  2438. } else {
  2439. buf[0] = '\0';/* Ensure the buffer is initialized. */
  2440. if (when.tv_sec) {
  2441. struct ast_tm tm;
  2442. ast_localtime(&when, &tm, NULL);
  2443. ast_strftime(buf, bufsize, fmt, &tm);
  2444. }
  2445. }
  2446. }
  2447. void ast_cdr_format_var(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int raw)
  2448. {
  2449. const char *fmt = "%Y-%m-%d %T";
  2450. const char *varbuf;
  2451. if (!cdr) {
  2452. return;
  2453. }
  2454. *ret = NULL;
  2455. if (!strcasecmp(name, "clid")) {
  2456. ast_copy_string(workspace, cdr->clid, workspacelen);
  2457. } else if (!strcasecmp(name, "src")) {
  2458. ast_copy_string(workspace, cdr->src, workspacelen);
  2459. } else if (!strcasecmp(name, "dst")) {
  2460. ast_copy_string(workspace, cdr->dst, workspacelen);
  2461. } else if (!strcasecmp(name, "dcontext")) {
  2462. ast_copy_string(workspace, cdr->dcontext, workspacelen);
  2463. } else if (!strcasecmp(name, "channel")) {
  2464. ast_copy_string(workspace, cdr->channel, workspacelen);
  2465. } else if (!strcasecmp(name, "dstchannel")) {
  2466. ast_copy_string(workspace, cdr->dstchannel, workspacelen);
  2467. } else if (!strcasecmp(name, "lastapp")) {
  2468. ast_copy_string(workspace, cdr->lastapp, workspacelen);
  2469. } else if (!strcasecmp(name, "lastdata")) {
  2470. ast_copy_string(workspace, cdr->lastdata, workspacelen);
  2471. } else if (!strcasecmp(name, "start")) {
  2472. cdr_get_tv(cdr->start, raw ? NULL : fmt, workspace, workspacelen);
  2473. } else if (!strcasecmp(name, "answer")) {
  2474. cdr_get_tv(cdr->answer, raw ? NULL : fmt, workspace, workspacelen);
  2475. } else if (!strcasecmp(name, "end")) {
  2476. cdr_get_tv(cdr->end, raw ? NULL : fmt, workspace, workspacelen);
  2477. } else if (!strcasecmp(name, "duration")) {
  2478. snprintf(workspace, workspacelen, "%ld", cdr->end.tv_sec != 0 ? cdr->duration : (long)ast_tvdiff_ms(ast_tvnow(), cdr->start) / 1000);
  2479. } else if (!strcasecmp(name, "billsec")) {
  2480. snprintf(workspace, workspacelen, "%ld", (cdr->billsec || !ast_tvzero(cdr->end) || ast_tvzero(cdr->answer)) ? cdr->billsec : (long)ast_tvdiff_ms(ast_tvnow(), cdr->answer) / 1000);
  2481. } else if (!strcasecmp(name, "disposition")) {
  2482. if (raw) {
  2483. snprintf(workspace, workspacelen, "%ld", cdr->disposition);
  2484. } else {
  2485. ast_copy_string(workspace, ast_cdr_disp2str(cdr->disposition), workspacelen);
  2486. }
  2487. } else if (!strcasecmp(name, "amaflags")) {
  2488. if (raw) {
  2489. snprintf(workspace, workspacelen, "%ld", cdr->amaflags);
  2490. } else {
  2491. ast_copy_string(workspace, ast_channel_amaflags2string(cdr->amaflags), workspacelen);
  2492. }
  2493. } else if (!strcasecmp(name, "accountcode")) {
  2494. ast_copy_string(workspace, cdr->accountcode, workspacelen);
  2495. } else if (!strcasecmp(name, "peeraccount")) {
  2496. ast_copy_string(workspace, cdr->peeraccount, workspacelen);
  2497. } else if (!strcasecmp(name, "uniqueid")) {
  2498. ast_copy_string(workspace, cdr->uniqueid, workspacelen);
  2499. } else if (!strcasecmp(name, "linkedid")) {
  2500. ast_copy_string(workspace, cdr->linkedid, workspacelen);
  2501. } else if (!strcasecmp(name, "userfield")) {
  2502. ast_copy_string(workspace, cdr->userfield, workspacelen);
  2503. } else if (!strcasecmp(name, "sequence")) {
  2504. snprintf(workspace, workspacelen, "%d", cdr->sequence);
  2505. } else if ((varbuf = cdr_format_var_internal(cdr, name))) {
  2506. ast_copy_string(workspace, varbuf, workspacelen);
  2507. } else {
  2508. workspace[0] = '\0';
  2509. }
  2510. if (!ast_strlen_zero(workspace)) {
  2511. *ret = workspace;
  2512. }
  2513. }
  2514. /*
  2515. * \internal
  2516. * \brief Callback that finds all CDRs that reference a particular channel by name
  2517. */
  2518. static int cdr_object_select_all_by_name_cb(void *obj, void *arg, int flags)
  2519. {
  2520. struct cdr_object *cdr = obj;
  2521. const char *name = arg;
  2522. if (!strcasecmp(cdr->party_a.snapshot->name, name) ||
  2523. (cdr->party_b.snapshot && !strcasecmp(cdr->party_b.snapshot->name, name))) {
  2524. return CMP_MATCH;
  2525. }
  2526. return 0;
  2527. }
  2528. /*
  2529. * \internal
  2530. * \brief Callback that finds a CDR by channel name
  2531. */
  2532. static int cdr_object_get_by_name_cb(void *obj, void *arg, int flags)
  2533. {
  2534. struct cdr_object *cdr = obj;
  2535. const char *name = arg;
  2536. if (!strcasecmp(cdr->party_a.snapshot->name, name)) {
  2537. return CMP_MATCH;
  2538. }
  2539. return 0;
  2540. }
  2541. /* Read Only CDR variables */
  2542. static const char * const cdr_readonly_vars[] = {
  2543. "clid",
  2544. "src",
  2545. "dst",
  2546. "dcontext",
  2547. "channel",
  2548. "dstchannel",
  2549. "lastapp",
  2550. "lastdata",
  2551. "start",
  2552. "answer",
  2553. "end",
  2554. "duration",
  2555. "billsec",
  2556. "disposition",
  2557. "amaflags",
  2558. "accountcode",
  2559. "uniqueid",
  2560. "linkedid",
  2561. "userfield",
  2562. "sequence",
  2563. NULL
  2564. };
  2565. int ast_cdr_setvar(const char *channel_name, const char *name, const char *value)
  2566. {
  2567. struct cdr_object *cdr;
  2568. struct cdr_object *it_cdr;
  2569. struct ao2_iterator *it_cdrs;
  2570. char *arg = ast_strdupa(channel_name);
  2571. int x;
  2572. for (x = 0; cdr_readonly_vars[x]; x++) {
  2573. if (!strcasecmp(name, cdr_readonly_vars[x])) {
  2574. ast_log(LOG_ERROR, "Attempt to set the '%s' read-only variable!\n", name);
  2575. return -1;
  2576. }
  2577. }
  2578. it_cdrs = ao2_callback(active_cdrs_by_channel, OBJ_MULTIPLE, cdr_object_select_all_by_name_cb, arg);
  2579. if (!it_cdrs) {
  2580. ast_log(AST_LOG_ERROR, "Unable to find CDR for channel %s\n", channel_name);
  2581. return -1;
  2582. }
  2583. for (; (cdr = ao2_iterator_next(it_cdrs)); ao2_unlock(cdr), ao2_cleanup(cdr)) {
  2584. ao2_lock(cdr);
  2585. for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
  2586. struct varshead *headp = NULL;
  2587. if (it_cdr->fn_table == &finalized_state_fn_table) {
  2588. continue;
  2589. }
  2590. if (!strcasecmp(channel_name, it_cdr->party_a.snapshot->name)) {
  2591. headp = &it_cdr->party_a.variables;
  2592. } else if (it_cdr->party_b.snapshot
  2593. && !strcasecmp(channel_name, it_cdr->party_b.snapshot->name)) {
  2594. headp = &it_cdr->party_b.variables;
  2595. }
  2596. if (headp) {
  2597. set_variable(headp, name, value);
  2598. }
  2599. }
  2600. }
  2601. ao2_iterator_destroy(it_cdrs);
  2602. return 0;
  2603. }
  2604. /*!
  2605. * \brief Format a variable on a \ref cdr_object
  2606. */
  2607. static void cdr_object_format_var_internal(struct cdr_object *cdr, const char *name, char *value, size_t length)
  2608. {
  2609. struct ast_var_t *variable;
  2610. AST_LIST_TRAVERSE(&cdr->party_a.variables, variable, entries) {
  2611. if (!strcasecmp(name, ast_var_name(variable))) {
  2612. ast_copy_string(value, ast_var_value(variable), length);
  2613. return;
  2614. }
  2615. }
  2616. *value = '\0';
  2617. }
  2618. /*!
  2619. * \brief Format one of the standard properties on a \ref cdr_object
  2620. */
  2621. static int cdr_object_format_property(struct cdr_object *cdr_obj, const char *name, char *value, size_t length)
  2622. {
  2623. struct ast_channel_snapshot *party_a = cdr_obj->party_a.snapshot;
  2624. struct ast_channel_snapshot *party_b = cdr_obj->party_b.snapshot;
  2625. if (!strcasecmp(name, "clid")) {
  2626. ast_callerid_merge(value, length, party_a->caller_name, party_a->caller_number, "");
  2627. } else if (!strcasecmp(name, "src")) {
  2628. ast_copy_string(value, party_a->caller_number, length);
  2629. } else if (!strcasecmp(name, "dst")) {
  2630. ast_copy_string(value, party_a->exten, length);
  2631. } else if (!strcasecmp(name, "dcontext")) {
  2632. ast_copy_string(value, party_a->context, length);
  2633. } else if (!strcasecmp(name, "channel")) {
  2634. ast_copy_string(value, party_a->name, length);
  2635. } else if (!strcasecmp(name, "dstchannel")) {
  2636. if (party_b) {
  2637. ast_copy_string(value, party_b->name, length);
  2638. } else {
  2639. ast_copy_string(value, "", length);
  2640. }
  2641. } else if (!strcasecmp(name, "lastapp")) {
  2642. ast_copy_string(value, party_a->appl, length);
  2643. } else if (!strcasecmp(name, "lastdata")) {
  2644. ast_copy_string(value, party_a->data, length);
  2645. } else if (!strcasecmp(name, "start")) {
  2646. cdr_get_tv(cdr_obj->start, NULL, value, length);
  2647. } else if (!strcasecmp(name, "answer")) {
  2648. cdr_get_tv(cdr_obj->answer, NULL, value, length);
  2649. } else if (!strcasecmp(name, "end")) {
  2650. cdr_get_tv(cdr_obj->end, NULL, value, length);
  2651. } else if (!strcasecmp(name, "duration")) {
  2652. snprintf(value, length, "%ld", cdr_object_get_duration(cdr_obj));
  2653. } else if (!strcasecmp(name, "billsec")) {
  2654. snprintf(value, length, "%ld", cdr_object_get_billsec(cdr_obj));
  2655. } else if (!strcasecmp(name, "disposition")) {
  2656. snprintf(value, length, "%d", cdr_obj->disposition);
  2657. } else if (!strcasecmp(name, "amaflags")) {
  2658. snprintf(value, length, "%d", party_a->amaflags);
  2659. } else if (!strcasecmp(name, "accountcode")) {
  2660. ast_copy_string(value, party_a->accountcode, length);
  2661. } else if (!strcasecmp(name, "peeraccount")) {
  2662. if (party_b) {
  2663. ast_copy_string(value, party_b->accountcode, length);
  2664. } else {
  2665. ast_copy_string(value, "", length);
  2666. }
  2667. } else if (!strcasecmp(name, "uniqueid")) {
  2668. ast_copy_string(value, party_a->uniqueid, length);
  2669. } else if (!strcasecmp(name, "linkedid")) {
  2670. ast_copy_string(value, cdr_obj->linkedid, length);
  2671. } else if (!strcasecmp(name, "userfield")) {
  2672. ast_copy_string(value, cdr_obj->party_a.userfield, length);
  2673. } else if (!strcasecmp(name, "sequence")) {
  2674. snprintf(value, length, "%d", cdr_obj->sequence);
  2675. } else {
  2676. return 1;
  2677. }
  2678. return 0;
  2679. }
  2680. /*! \internal
  2681. * \brief Look up and retrieve a CDR object by channel name
  2682. * \param name The name of the channel
  2683. * \retval NULL on error
  2684. * \retval The \ref cdr_object for the channel on success, with the reference
  2685. * count bumped by one.
  2686. */
  2687. static struct cdr_object *cdr_object_get_by_name(const char *name)
  2688. {
  2689. char *param;
  2690. if (ast_strlen_zero(name)) {
  2691. return NULL;
  2692. }
  2693. param = ast_strdupa(name);
  2694. return ao2_callback(active_cdrs_by_channel, 0, cdr_object_get_by_name_cb, param);
  2695. }
  2696. int ast_cdr_getvar(const char *channel_name, const char *name, char *value, size_t length)
  2697. {
  2698. RAII_VAR(struct cdr_object *, cdr, cdr_object_get_by_name(channel_name), ao2_cleanup);
  2699. struct cdr_object *cdr_obj;
  2700. if (!cdr) {
  2701. ast_log(AST_LOG_ERROR, "Unable to find CDR for channel %s\n", channel_name);
  2702. return 1;
  2703. }
  2704. if (ast_strlen_zero(name)) {
  2705. return 1;
  2706. }
  2707. ao2_lock(cdr);
  2708. cdr_obj = cdr->last;
  2709. if (cdr_object_format_property(cdr_obj, name, value, length)) {
  2710. /* Property failed; attempt variable */
  2711. cdr_object_format_var_internal(cdr_obj, name, value, length);
  2712. }
  2713. ao2_unlock(cdr);
  2714. return 0;
  2715. }
  2716. int ast_cdr_serialize_variables(const char *channel_name, struct ast_str **buf, char delim, char sep)
  2717. {
  2718. RAII_VAR(struct cdr_object *, cdr, cdr_object_get_by_name(channel_name), ao2_cleanup);
  2719. struct cdr_object *it_cdr;
  2720. struct ast_var_t *variable;
  2721. const char *var;
  2722. RAII_VAR(char *, workspace, ast_malloc(256), ast_free);
  2723. int total = 0, x = 0, i;
  2724. if (!workspace) {
  2725. return 0;
  2726. }
  2727. if (!cdr) {
  2728. RAII_VAR(struct module_config *, mod_cfg,
  2729. ao2_global_obj_ref(module_configs), ao2_cleanup);
  2730. if (ast_test_flag(&mod_cfg->general->settings, CDR_ENABLED)) {
  2731. ast_log(AST_LOG_ERROR, "Unable to find CDR for channel %s\n", channel_name);
  2732. }
  2733. return 0;
  2734. }
  2735. ast_str_reset(*buf);
  2736. ao2_lock(cdr);
  2737. for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
  2738. if (++x > 1)
  2739. ast_str_append(buf, 0, "\n");
  2740. AST_LIST_TRAVERSE(&it_cdr->party_a.variables, variable, entries) {
  2741. if (!(var = ast_var_name(variable))) {
  2742. continue;
  2743. }
  2744. if (ast_str_append(buf, 0, "level %d: %s%c%s%c", x, var, delim, S_OR(ast_var_value(variable), ""), sep) < 0) {
  2745. ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
  2746. break;
  2747. }
  2748. total++;
  2749. }
  2750. for (i = 0; cdr_readonly_vars[i]; i++) {
  2751. if (cdr_object_format_property(it_cdr, cdr_readonly_vars[i], workspace, sizeof(workspace))) {
  2752. /* Unhandled read-only CDR variable. */
  2753. ast_assert(0);
  2754. continue;
  2755. }
  2756. if (!ast_strlen_zero(workspace)
  2757. && ast_str_append(buf, 0, "level %d: %s%c%s%c", x, cdr_readonly_vars[i], delim, workspace, sep) < 0) {
  2758. ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
  2759. break;
  2760. }
  2761. total++;
  2762. }
  2763. }
  2764. ao2_unlock(cdr);
  2765. return total;
  2766. }
  2767. void ast_cdr_free(struct ast_cdr *cdr)
  2768. {
  2769. while (cdr) {
  2770. struct ast_cdr *next = cdr->next;
  2771. free_variables(&cdr->varshead);
  2772. ast_free(cdr);
  2773. cdr = next;
  2774. }
  2775. }
  2776. struct ast_cdr *ast_cdr_alloc(void)
  2777. {
  2778. struct ast_cdr *x;
  2779. x = ast_calloc(1, sizeof(*x));
  2780. return x;
  2781. }
  2782. const char *ast_cdr_disp2str(int disposition)
  2783. {
  2784. switch (disposition) {
  2785. case AST_CDR_NULL:
  2786. return "NO ANSWER"; /* by default, for backward compatibility */
  2787. case AST_CDR_NOANSWER:
  2788. return "NO ANSWER";
  2789. case AST_CDR_FAILED:
  2790. return "FAILED";
  2791. case AST_CDR_BUSY:
  2792. return "BUSY";
  2793. case AST_CDR_ANSWERED:
  2794. return "ANSWERED";
  2795. case AST_CDR_CONGESTION:
  2796. return "CONGESTION";
  2797. }
  2798. return "UNKNOWN";
  2799. }
  2800. struct party_b_userfield_update {
  2801. const char *channel_name;
  2802. const char *userfield;
  2803. };
  2804. /*! \brief Callback used to update the userfield on Party B on all CDRs */
  2805. static int cdr_object_update_party_b_userfield_cb(void *obj, void *arg, int flags)
  2806. {
  2807. struct cdr_object *cdr = obj;
  2808. struct party_b_userfield_update *info = arg;
  2809. struct cdr_object *it_cdr;
  2810. for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
  2811. if (it_cdr->fn_table == &finalized_state_fn_table) {
  2812. continue;
  2813. }
  2814. if (it_cdr->party_b.snapshot
  2815. && !strcasecmp(it_cdr->party_b.snapshot->name, info->channel_name)) {
  2816. strcpy(it_cdr->party_b.userfield, info->userfield);
  2817. }
  2818. }
  2819. return 0;
  2820. }
  2821. void ast_cdr_setuserfield(const char *channel_name, const char *userfield)
  2822. {
  2823. RAII_VAR(struct cdr_object *, cdr, cdr_object_get_by_name(channel_name), ao2_cleanup);
  2824. struct party_b_userfield_update party_b_info = {
  2825. .channel_name = channel_name,
  2826. .userfield = userfield,
  2827. };
  2828. struct cdr_object *it_cdr;
  2829. /* Handle Party A */
  2830. if (cdr) {
  2831. ao2_lock(cdr);
  2832. for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
  2833. if (it_cdr->fn_table == &finalized_state_fn_table) {
  2834. continue;
  2835. }
  2836. strcpy(it_cdr->party_a.userfield, userfield);
  2837. }
  2838. ao2_unlock(cdr);
  2839. }
  2840. /* Handle Party B */
  2841. ao2_callback(active_cdrs_by_channel, OBJ_NODATA,
  2842. cdr_object_update_party_b_userfield_cb,
  2843. &party_b_info);
  2844. }
  2845. static void post_cdr(struct ast_cdr *cdr)
  2846. {
  2847. RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
  2848. struct cdr_beitem *i;
  2849. for (; cdr ; cdr = cdr->next) {
  2850. /* For people, who don't want to see unanswered single-channel events */
  2851. if (!ast_test_flag(&mod_cfg->general->settings, CDR_UNANSWERED) &&
  2852. cdr->disposition < AST_CDR_ANSWERED &&
  2853. (ast_strlen_zero(cdr->channel) || ast_strlen_zero(cdr->dstchannel))) {
  2854. ast_debug(1, "Skipping CDR for %s since we weren't answered\n", cdr->channel);
  2855. continue;
  2856. }
  2857. if (ast_test_flag(cdr, AST_CDR_FLAG_DISABLE)) {
  2858. continue;
  2859. }
  2860. AST_RWLIST_RDLOCK(&be_list);
  2861. AST_RWLIST_TRAVERSE(&be_list, i, list) {
  2862. if (!i->suspended) {
  2863. i->be(cdr);
  2864. }
  2865. }
  2866. AST_RWLIST_UNLOCK(&be_list);
  2867. }
  2868. }
  2869. int ast_cdr_set_property(const char *channel_name, enum ast_cdr_options option)
  2870. {
  2871. RAII_VAR(struct cdr_object *, cdr, cdr_object_get_by_name(channel_name), ao2_cleanup);
  2872. struct cdr_object *it_cdr;
  2873. if (!cdr) {
  2874. return -1;
  2875. }
  2876. ao2_lock(cdr);
  2877. for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
  2878. if (it_cdr->fn_table == &finalized_state_fn_table) {
  2879. continue;
  2880. }
  2881. /* Note: in general, set the flags on both the CDR record as well as the
  2882. * Party A. Sometimes all we have is the Party A to look at.
  2883. */
  2884. ast_set_flag(&it_cdr->flags, option);
  2885. ast_set_flag(&it_cdr->party_a, option);
  2886. }
  2887. ao2_unlock(cdr);
  2888. return 0;
  2889. }
  2890. int ast_cdr_clear_property(const char *channel_name, enum ast_cdr_options option)
  2891. {
  2892. RAII_VAR(struct cdr_object *, cdr, cdr_object_get_by_name(channel_name), ao2_cleanup);
  2893. struct cdr_object *it_cdr;
  2894. if (!cdr) {
  2895. return -1;
  2896. }
  2897. ao2_lock(cdr);
  2898. for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
  2899. if (it_cdr->fn_table == &finalized_state_fn_table) {
  2900. continue;
  2901. }
  2902. ast_clear_flag(&it_cdr->flags, option);
  2903. }
  2904. ao2_unlock(cdr);
  2905. return 0;
  2906. }
  2907. int ast_cdr_reset(const char *channel_name, int keep_variables)
  2908. {
  2909. RAII_VAR(struct cdr_object *, cdr, cdr_object_get_by_name(channel_name), ao2_cleanup);
  2910. struct ast_var_t *vardata;
  2911. struct cdr_object *it_cdr;
  2912. if (!cdr) {
  2913. return -1;
  2914. }
  2915. ao2_lock(cdr);
  2916. for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
  2917. /* clear variables */
  2918. if (!keep_variables) {
  2919. while ((vardata = AST_LIST_REMOVE_HEAD(&it_cdr->party_a.variables, entries))) {
  2920. ast_var_delete(vardata);
  2921. }
  2922. if (cdr->party_b.snapshot) {
  2923. while ((vardata = AST_LIST_REMOVE_HEAD(&it_cdr->party_b.variables, entries))) {
  2924. ast_var_delete(vardata);
  2925. }
  2926. }
  2927. }
  2928. /* Reset to initial state */
  2929. memset(&it_cdr->start, 0, sizeof(it_cdr->start));
  2930. memset(&it_cdr->end, 0, sizeof(it_cdr->end));
  2931. memset(&it_cdr->answer, 0, sizeof(it_cdr->answer));
  2932. it_cdr->start = ast_tvnow();
  2933. cdr_object_check_party_a_answer(it_cdr);
  2934. }
  2935. ao2_unlock(cdr);
  2936. return 0;
  2937. }
  2938. int ast_cdr_fork(const char *channel_name, struct ast_flags *options)
  2939. {
  2940. RAII_VAR(struct cdr_object *, cdr, cdr_object_get_by_name(channel_name), ao2_cleanup);
  2941. struct cdr_object *new_cdr;
  2942. struct cdr_object *it_cdr;
  2943. struct cdr_object *cdr_obj;
  2944. if (!cdr) {
  2945. return -1;
  2946. }
  2947. {
  2948. SCOPED_AO2LOCK(lock, cdr);
  2949. cdr_obj = cdr->last;
  2950. if (cdr_obj->fn_table == &finalized_state_fn_table) {
  2951. /* If the last CDR in the chain is finalized, don't allow a fork -
  2952. * things are already dying at this point
  2953. */
  2954. return -1;
  2955. }
  2956. /* Copy over the basic CDR information. The Party A information is
  2957. * copied over automatically as part of the append
  2958. */
  2959. ast_debug(1, "Forking CDR for channel %s\n", cdr->party_a.snapshot->name);
  2960. new_cdr = cdr_object_create_and_append(cdr);
  2961. if (!new_cdr) {
  2962. return -1;
  2963. }
  2964. new_cdr->fn_table = cdr_obj->fn_table;
  2965. ast_string_field_set(new_cdr, bridge, cdr->bridge);
  2966. new_cdr->flags = cdr->flags;
  2967. /* Explicitly clear the AST_CDR_LOCK_APP flag - we want
  2968. * the application to be changed on the new CDR if the
  2969. * dialplan demands it
  2970. */
  2971. ast_clear_flag(&new_cdr->flags, AST_CDR_LOCK_APP);
  2972. /* If there's a Party B, copy it over as well */
  2973. if (cdr_obj->party_b.snapshot) {
  2974. new_cdr->party_b.snapshot = cdr_obj->party_b.snapshot;
  2975. ao2_ref(new_cdr->party_b.snapshot, +1);
  2976. strcpy(new_cdr->party_b.userfield, cdr_obj->party_b.userfield);
  2977. new_cdr->party_b.flags = cdr_obj->party_b.flags;
  2978. if (ast_test_flag(options, AST_CDR_FLAG_KEEP_VARS)) {
  2979. copy_variables(&new_cdr->party_b.variables, &cdr_obj->party_b.variables);
  2980. }
  2981. }
  2982. new_cdr->start = cdr_obj->start;
  2983. new_cdr->answer = cdr_obj->answer;
  2984. /* Modify the times based on the flags passed in */
  2985. if (ast_test_flag(options, AST_CDR_FLAG_SET_ANSWER)
  2986. && new_cdr->party_a.snapshot->state == AST_STATE_UP) {
  2987. new_cdr->answer = ast_tvnow();
  2988. }
  2989. if (ast_test_flag(options, AST_CDR_FLAG_RESET)) {
  2990. new_cdr->answer = ast_tvnow();
  2991. new_cdr->start = ast_tvnow();
  2992. }
  2993. /* Create and append, by default, copies over the variables */
  2994. if (!ast_test_flag(options, AST_CDR_FLAG_KEEP_VARS)) {
  2995. free_variables(&new_cdr->party_a.variables);
  2996. }
  2997. /* Finalize any current CDRs */
  2998. if (ast_test_flag(options, AST_CDR_FLAG_FINALIZE)) {
  2999. for (it_cdr = cdr; it_cdr != new_cdr; it_cdr = it_cdr->next) {
  3000. if (it_cdr->fn_table == &finalized_state_fn_table) {
  3001. continue;
  3002. }
  3003. /* Force finalization on the CDR. This will bypass any checks for
  3004. * end before 'h' extension.
  3005. */
  3006. cdr_object_finalize(it_cdr);
  3007. cdr_object_transition_state(it_cdr, &finalized_state_fn_table);
  3008. }
  3009. }
  3010. }
  3011. return 0;
  3012. }
  3013. /*! \note Don't call without cdr_batch_lock */
  3014. static void reset_batch(void)
  3015. {
  3016. batch->size = 0;
  3017. batch->head = NULL;
  3018. batch->tail = NULL;
  3019. }
  3020. /*! \note Don't call without cdr_batch_lock */
  3021. static int init_batch(void)
  3022. {
  3023. /* This is the single meta-batch used to keep track of all CDRs during the entire life of the program */
  3024. if (!(batch = ast_malloc(sizeof(*batch))))
  3025. return -1;
  3026. reset_batch();
  3027. return 0;
  3028. }
  3029. static void *do_batch_backend_process(void *data)
  3030. {
  3031. struct cdr_batch_item *processeditem;
  3032. struct cdr_batch_item *batchitem = data;
  3033. /* Push each CDR into storage mechanism(s) and free all the memory */
  3034. while (batchitem) {
  3035. post_cdr(batchitem->cdr);
  3036. ast_cdr_free(batchitem->cdr);
  3037. processeditem = batchitem;
  3038. batchitem = batchitem->next;
  3039. ast_free(processeditem);
  3040. }
  3041. return NULL;
  3042. }
  3043. static void cdr_submit_batch(int do_shutdown)
  3044. {
  3045. RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
  3046. struct cdr_batch_item *oldbatchitems = NULL;
  3047. pthread_t batch_post_thread = AST_PTHREADT_NULL;
  3048. /* if there's no batch, or no CDRs in the batch, then there's nothing to do */
  3049. if (!batch || !batch->head) {
  3050. return;
  3051. }
  3052. /* move the old CDRs aside, and prepare a new CDR batch */
  3053. ast_mutex_lock(&cdr_batch_lock);
  3054. oldbatchitems = batch->head;
  3055. reset_batch();
  3056. ast_mutex_unlock(&cdr_batch_lock);
  3057. /* if configured, spawn a new thread to post these CDRs,
  3058. also try to save as much as possible if we are shutting down safely */
  3059. if (ast_test_flag(&mod_cfg->general->batch_settings.settings, BATCH_MODE_SCHEDULER_ONLY) || do_shutdown) {
  3060. ast_debug(1, "CDR single-threaded batch processing begins now\n");
  3061. do_batch_backend_process(oldbatchitems);
  3062. } else {
  3063. if (ast_pthread_create_detached_background(&batch_post_thread, NULL, do_batch_backend_process, oldbatchitems)) {
  3064. ast_log(LOG_WARNING, "CDR processing thread could not detach, now trying in this thread\n");
  3065. do_batch_backend_process(oldbatchitems);
  3066. } else {
  3067. ast_debug(1, "CDR multi-threaded batch processing begins now\n");
  3068. }
  3069. }
  3070. }
  3071. static int submit_scheduled_batch(const void *data)
  3072. {
  3073. RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
  3074. cdr_submit_batch(0);
  3075. /* manually reschedule from this point in time */
  3076. ast_mutex_lock(&cdr_sched_lock);
  3077. cdr_sched = ast_sched_add(sched, mod_cfg->general->batch_settings.size * 1000, submit_scheduled_batch, NULL);
  3078. ast_mutex_unlock(&cdr_sched_lock);
  3079. /* returning zero so the scheduler does not automatically reschedule */
  3080. return 0;
  3081. }
  3082. /*! Do not hold the batch lock while calling this function */
  3083. static void submit_unscheduled_batch(void)
  3084. {
  3085. /* Prevent two deletes from happening at the same time */
  3086. ast_mutex_lock(&cdr_sched_lock);
  3087. /* this is okay since we are not being called from within the scheduler */
  3088. AST_SCHED_DEL(sched, cdr_sched);
  3089. /* schedule the submission to occur ASAP (1 ms) */
  3090. cdr_sched = ast_sched_add(sched, 1, submit_scheduled_batch, NULL);
  3091. ast_mutex_unlock(&cdr_sched_lock);
  3092. /* signal the do_cdr thread to wakeup early and do some work (that lazy thread ;) */
  3093. ast_mutex_lock(&cdr_pending_lock);
  3094. ast_cond_signal(&cdr_pending_cond);
  3095. ast_mutex_unlock(&cdr_pending_lock);
  3096. }
  3097. static void cdr_detach(struct ast_cdr *cdr)
  3098. {
  3099. struct cdr_batch_item *newtail;
  3100. int curr;
  3101. RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
  3102. int submit_batch = 0;
  3103. if (!cdr) {
  3104. return;
  3105. }
  3106. /* maybe they disabled CDR stuff completely, so just drop it */
  3107. if (!ast_test_flag(&mod_cfg->general->settings, CDR_ENABLED)) {
  3108. ast_debug(1, "Dropping CDR !\n");
  3109. ast_cdr_free(cdr);
  3110. return;
  3111. }
  3112. /* post stuff immediately if we are not in batch mode, this is legacy behaviour */
  3113. if (!ast_test_flag(&mod_cfg->general->settings, CDR_BATCHMODE)) {
  3114. post_cdr(cdr);
  3115. ast_cdr_free(cdr);
  3116. return;
  3117. }
  3118. /* otherwise, each CDR gets put into a batch list (at the end) */
  3119. ast_debug(1, "CDR detaching from this thread\n");
  3120. /* we'll need a new tail for every CDR */
  3121. if (!(newtail = ast_calloc(1, sizeof(*newtail)))) {
  3122. post_cdr(cdr);
  3123. ast_cdr_free(cdr);
  3124. return;
  3125. }
  3126. /* don't traverse a whole list (just keep track of the tail) */
  3127. ast_mutex_lock(&cdr_batch_lock);
  3128. if (!batch)
  3129. init_batch();
  3130. if (!batch->head) {
  3131. /* new batch is empty, so point the head at the new tail */
  3132. batch->head = newtail;
  3133. } else {
  3134. /* already got a batch with something in it, so just append a new tail */
  3135. batch->tail->next = newtail;
  3136. }
  3137. newtail->cdr = cdr;
  3138. batch->tail = newtail;
  3139. curr = batch->size++;
  3140. /* if we have enough stuff to post, then do it */
  3141. if (curr >= (mod_cfg->general->batch_settings.size - 1)) {
  3142. submit_batch = 1;
  3143. }
  3144. ast_mutex_unlock(&cdr_batch_lock);
  3145. /* Don't call submit_unscheduled_batch with the cdr_batch_lock held */
  3146. if (submit_batch) {
  3147. submit_unscheduled_batch();
  3148. }
  3149. }
  3150. static void *do_cdr(void *data)
  3151. {
  3152. struct timespec timeout;
  3153. int schedms;
  3154. int numevents = 0;
  3155. for (;;) {
  3156. struct timeval now;
  3157. schedms = ast_sched_wait(sched);
  3158. /* this shouldn't happen, but provide a 1 second default just in case */
  3159. if (schedms <= 0)
  3160. schedms = 1000;
  3161. now = ast_tvadd(ast_tvnow(), ast_samp2tv(schedms, 1000));
  3162. timeout.tv_sec = now.tv_sec;
  3163. timeout.tv_nsec = now.tv_usec * 1000;
  3164. /* prevent stuff from clobbering cdr_pending_cond, then wait on signals sent to it until the timeout expires */
  3165. ast_mutex_lock(&cdr_pending_lock);
  3166. ast_cond_timedwait(&cdr_pending_cond, &cdr_pending_lock, &timeout);
  3167. numevents = ast_sched_runq(sched);
  3168. ast_mutex_unlock(&cdr_pending_lock);
  3169. ast_debug(2, "Processed %d scheduled CDR batches from the run queue\n", numevents);
  3170. }
  3171. return NULL;
  3172. }
  3173. static char *handle_cli_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3174. {
  3175. RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
  3176. switch (cmd) {
  3177. case CLI_INIT:
  3178. e->command = "cdr set debug [on|off]";
  3179. e->usage = "Enable or disable extra debugging in the CDR Engine. Note\n"
  3180. "that this will dump debug information to the VERBOSE setting\n"
  3181. "and should only be used when debugging information from the\n"
  3182. "CDR engine is needed.\n";
  3183. return NULL;
  3184. case CLI_GENERATE:
  3185. return NULL;
  3186. }
  3187. if (a->argc != 4) {
  3188. return CLI_SHOWUSAGE;
  3189. }
  3190. if (!strcasecmp(a->argv[3], "on")
  3191. && !ast_test_flag(&mod_cfg->general->settings, CDR_DEBUG)) {
  3192. ast_set_flag(&mod_cfg->general->settings, CDR_DEBUG);
  3193. ast_cli(a->fd, "CDR debugging enabled\n");
  3194. } else if (!strcasecmp(a->argv[3], "off")
  3195. && ast_test_flag(&mod_cfg->general->settings, CDR_DEBUG)) {
  3196. ast_clear_flag(&mod_cfg->general->settings, CDR_DEBUG);
  3197. ast_cli(a->fd, "CDR debugging disabled\n");
  3198. }
  3199. return CLI_SUCCESS;
  3200. }
  3201. /*! \brief Complete user input for 'cdr show' */
  3202. static char *cli_complete_show(struct ast_cli_args *a)
  3203. {
  3204. char *result = NULL;
  3205. int wordlen = strlen(a->word);
  3206. int which = 0;
  3207. struct ao2_iterator it_cdrs;
  3208. struct cdr_object *cdr;
  3209. it_cdrs = ao2_iterator_init(active_cdrs_by_channel, 0);
  3210. while ((cdr = ao2_iterator_next(&it_cdrs))) {
  3211. if (!strncasecmp(a->word, cdr->party_a.snapshot->name, wordlen) &&
  3212. (++which > a->n)) {
  3213. result = ast_strdup(cdr->party_a.snapshot->name);
  3214. if (result) {
  3215. ao2_ref(cdr, -1);
  3216. break;
  3217. }
  3218. }
  3219. ao2_ref(cdr, -1);
  3220. }
  3221. ao2_iterator_destroy(&it_cdrs);
  3222. return result;
  3223. }
  3224. static void cli_show_channels(struct ast_cli_args *a)
  3225. {
  3226. struct ao2_iterator it_cdrs;
  3227. struct cdr_object *cdr;
  3228. char start_time_buffer[64];
  3229. char answer_time_buffer[64];
  3230. char end_time_buffer[64];
  3231. #define TITLE_STRING "%-25.25s %-25.25s %-15.15s %-8.8s %-8.8s %-8.8s %-8.8s %-8.8s\n"
  3232. #define FORMAT_STRING "%-25.25s %-25.25s %-15.15s %-8.8s %-8.8s %-8.8s %-8.8ld %-8.8ld\n"
  3233. ast_cli(a->fd, "\n");
  3234. ast_cli(a->fd, "Channels with Call Detail Record (CDR) Information\n");
  3235. ast_cli(a->fd, "--------------------------------------------------\n");
  3236. ast_cli(a->fd, TITLE_STRING, "Channel", "Dst. Channel", "LastApp", "Start", "Answer", "End", "Billsec", "Duration");
  3237. it_cdrs = ao2_iterator_init(active_cdrs_by_channel, 0);
  3238. for (; (cdr = ao2_iterator_next(&it_cdrs)); ao2_cleanup(cdr)) {
  3239. struct cdr_object *it_cdr;
  3240. struct timeval start_time = { 0, };
  3241. struct timeval answer_time = { 0, };
  3242. struct timeval end_time = { 0, };
  3243. SCOPED_AO2LOCK(lock, cdr);
  3244. /* Calculate the start, end, answer, billsec, and duration over the
  3245. * life of all of the CDR entries
  3246. */
  3247. for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
  3248. if (snapshot_is_dialed(it_cdr->party_a.snapshot)) {
  3249. continue;
  3250. }
  3251. if (ast_tvzero(start_time)) {
  3252. start_time = it_cdr->start;
  3253. }
  3254. if (!ast_tvzero(it_cdr->answer) && ast_tvzero(answer_time)) {
  3255. answer_time = it_cdr->answer;
  3256. }
  3257. }
  3258. /* If there was no start time, then all CDRs were for a dialed channel; skip */
  3259. if (ast_tvzero(start_time)) {
  3260. continue;
  3261. }
  3262. it_cdr = cdr->last;
  3263. end_time = ast_tvzero(it_cdr->end) ? ast_tvnow() : it_cdr->end;
  3264. cdr_get_tv(start_time, "%T", start_time_buffer, sizeof(start_time_buffer));
  3265. cdr_get_tv(answer_time, "%T", answer_time_buffer, sizeof(answer_time_buffer));
  3266. cdr_get_tv(end_time, "%T", end_time_buffer, sizeof(end_time_buffer));
  3267. ast_cli(a->fd, FORMAT_STRING, it_cdr->party_a.snapshot->name,
  3268. it_cdr->party_b.snapshot ? it_cdr->party_b.snapshot->name : "<none>",
  3269. it_cdr->appl,
  3270. start_time_buffer,
  3271. answer_time_buffer,
  3272. end_time_buffer,
  3273. ast_tvzero(answer_time) ? 0 : (long)ast_tvdiff_ms(end_time, answer_time) / 1000,
  3274. (long)ast_tvdiff_ms(end_time, start_time) / 1000);
  3275. }
  3276. ao2_iterator_destroy(&it_cdrs);
  3277. #undef FORMAT_STRING
  3278. #undef TITLE_STRING
  3279. }
  3280. static void cli_show_channel(struct ast_cli_args *a)
  3281. {
  3282. struct cdr_object *it_cdr;
  3283. char clid[64];
  3284. char start_time_buffer[64];
  3285. char answer_time_buffer[64];
  3286. char end_time_buffer[64];
  3287. const char *channel_name = a->argv[3];
  3288. RAII_VAR(struct cdr_object *, cdr, NULL, ao2_cleanup);
  3289. #define TITLE_STRING "%-10.10s %-20.20s %-25.25s %-15.15s %-15.15s %-8.8s %-8.8s %-8.8s %-8.8s %-8.8s\n"
  3290. #define FORMAT_STRING "%-10.10s %-20.20s %-25.25s %-15.15s %-15.15s %-8.8s %-8.8s %-8.8s %-8.8ld %-8.8ld\n"
  3291. cdr = cdr_object_get_by_name(channel_name);
  3292. if (!cdr) {
  3293. ast_cli(a->fd, "Unknown channel: %s\n", channel_name);
  3294. return;
  3295. }
  3296. ast_cli(a->fd, "\n");
  3297. ast_cli(a->fd, "Call Detail Record (CDR) Information for %s\n", channel_name);
  3298. ast_cli(a->fd, "--------------------------------------------------\n");
  3299. ast_cli(a->fd, TITLE_STRING, "AccountCode", "CallerID", "Dst. Channel", "LastApp", "Data", "Start", "Answer", "End", "Billsec", "Duration");
  3300. ao2_lock(cdr);
  3301. for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
  3302. struct timeval end;
  3303. if (snapshot_is_dialed(it_cdr->party_a.snapshot)) {
  3304. continue;
  3305. }
  3306. ast_callerid_merge(clid, sizeof(clid), it_cdr->party_a.snapshot->caller_name, it_cdr->party_a.snapshot->caller_number, "");
  3307. if (ast_tvzero(it_cdr->end)) {
  3308. end = ast_tvnow();
  3309. } else {
  3310. end = it_cdr->end;
  3311. }
  3312. cdr_get_tv(it_cdr->start, "%T", start_time_buffer, sizeof(start_time_buffer));
  3313. cdr_get_tv(it_cdr->answer, "%T", answer_time_buffer, sizeof(answer_time_buffer));
  3314. cdr_get_tv(end, "%T", end_time_buffer, sizeof(end_time_buffer));
  3315. ast_cli(a->fd, FORMAT_STRING,
  3316. it_cdr->party_a.snapshot->accountcode,
  3317. clid,
  3318. it_cdr->party_b.snapshot ? it_cdr->party_b.snapshot->name : "<none>",
  3319. it_cdr->appl,
  3320. it_cdr->data,
  3321. start_time_buffer,
  3322. answer_time_buffer,
  3323. end_time_buffer,
  3324. (long)ast_tvdiff_ms(end, it_cdr->answer) / 1000,
  3325. (long)ast_tvdiff_ms(end, it_cdr->start) / 1000);
  3326. }
  3327. ao2_unlock(cdr);
  3328. #undef FORMAT_STRING
  3329. #undef TITLE_STRING
  3330. }
  3331. static char *handle_cli_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3332. {
  3333. switch (cmd) {
  3334. case CLI_INIT:
  3335. e->command = "cdr show active";
  3336. e->usage =
  3337. "Usage: cdr show active [channel]\n"
  3338. " Displays a summary of all Call Detail Records when [channel]\n"
  3339. " is omitted; displays all of the Call Detail Records\n"
  3340. " currently in flight for a given [channel] when [channel] is\n"
  3341. " specified.\n\n"
  3342. " Note that this will not display Call Detail Records that\n"
  3343. " have already been dispatched to a backend storage, nor for\n"
  3344. " channels that are no longer active.\n";
  3345. return NULL;
  3346. case CLI_GENERATE:
  3347. return cli_complete_show(a);
  3348. }
  3349. if (a->argc > 4) {
  3350. return CLI_SHOWUSAGE;
  3351. } else if (a->argc < 4) {
  3352. cli_show_channels(a);
  3353. } else {
  3354. cli_show_channel(a);
  3355. }
  3356. return CLI_SUCCESS;
  3357. }
  3358. static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3359. {
  3360. struct cdr_beitem *beitem = NULL;
  3361. RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
  3362. int cnt = 0;
  3363. long nextbatchtime = 0;
  3364. switch (cmd) {
  3365. case CLI_INIT:
  3366. e->command = "cdr show status";
  3367. e->usage =
  3368. "Usage: cdr show status\n"
  3369. " Displays the Call Detail Record engine system status.\n";
  3370. return NULL;
  3371. case CLI_GENERATE:
  3372. return NULL;
  3373. }
  3374. if (a->argc > 3) {
  3375. return CLI_SHOWUSAGE;
  3376. }
  3377. ast_cli(a->fd, "\n");
  3378. ast_cli(a->fd, "Call Detail Record (CDR) settings\n");
  3379. ast_cli(a->fd, "----------------------------------\n");
  3380. ast_cli(a->fd, " Logging: %s\n", ast_test_flag(&mod_cfg->general->settings, CDR_ENABLED) ? "Enabled" : "Disabled");
  3381. ast_cli(a->fd, " Mode: %s\n", ast_test_flag(&mod_cfg->general->settings, CDR_BATCHMODE) ? "Batch" : "Simple");
  3382. if (ast_test_flag(&mod_cfg->general->settings, CDR_ENABLED)) {
  3383. ast_cli(a->fd, " Log unanswered calls: %s\n", ast_test_flag(&mod_cfg->general->settings, CDR_UNANSWERED) ? "Yes" : "No");
  3384. ast_cli(a->fd, " Log congestion: %s\n\n", ast_test_flag(&mod_cfg->general->settings, CDR_CONGESTION) ? "Yes" : "No");
  3385. if (ast_test_flag(&mod_cfg->general->settings, CDR_BATCHMODE)) {
  3386. ast_cli(a->fd, "* Batch Mode Settings\n");
  3387. ast_cli(a->fd, " -------------------\n");
  3388. if (batch)
  3389. cnt = batch->size;
  3390. if (cdr_sched > -1)
  3391. nextbatchtime = ast_sched_when(sched, cdr_sched);
  3392. ast_cli(a->fd, " Safe shutdown: %s\n", ast_test_flag(&mod_cfg->general->batch_settings.settings, BATCH_MODE_SAFE_SHUTDOWN) ? "Enabled" : "Disabled");
  3393. ast_cli(a->fd, " Threading model: %s\n", ast_test_flag(&mod_cfg->general->batch_settings.settings, BATCH_MODE_SCHEDULER_ONLY) ? "Scheduler only" : "Scheduler plus separate threads");
  3394. ast_cli(a->fd, " Current batch size: %d record%s\n", cnt, ESS(cnt));
  3395. ast_cli(a->fd, " Maximum batch size: %d record%s\n", mod_cfg->general->batch_settings.size, ESS(mod_cfg->general->batch_settings.size));
  3396. ast_cli(a->fd, " Maximum batch time: %d second%s\n", mod_cfg->general->batch_settings.time, ESS(mod_cfg->general->batch_settings.time));
  3397. ast_cli(a->fd, " Next batch processing time: %ld second%s\n\n", nextbatchtime, ESS(nextbatchtime));
  3398. }
  3399. ast_cli(a->fd, "* Registered Backends\n");
  3400. ast_cli(a->fd, " -------------------\n");
  3401. AST_RWLIST_RDLOCK(&be_list);
  3402. if (AST_RWLIST_EMPTY(&be_list)) {
  3403. ast_cli(a->fd, " (none)\n");
  3404. } else {
  3405. AST_RWLIST_TRAVERSE(&be_list, beitem, list) {
  3406. ast_cli(a->fd, " %s%s\n", beitem->name, beitem->suspended ? " (suspended) " : "");
  3407. }
  3408. }
  3409. AST_RWLIST_UNLOCK(&be_list);
  3410. ast_cli(a->fd, "\n");
  3411. }
  3412. return CLI_SUCCESS;
  3413. }
  3414. static char *handle_cli_submit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3415. {
  3416. RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
  3417. switch (cmd) {
  3418. case CLI_INIT:
  3419. e->command = "cdr submit";
  3420. e->usage =
  3421. "Usage: cdr submit\n"
  3422. "Posts all pending batched CDR data to the configured CDR\n"
  3423. "backend engine modules.\n";
  3424. return NULL;
  3425. case CLI_GENERATE:
  3426. return NULL;
  3427. }
  3428. if (a->argc > 2) {
  3429. return CLI_SHOWUSAGE;
  3430. }
  3431. if (!ast_test_flag(&mod_cfg->general->settings, CDR_ENABLED)) {
  3432. ast_cli(a->fd, "Cannot submit CDR batch: CDR engine disabled.\n");
  3433. return CLI_SUCCESS;
  3434. }
  3435. if (ast_test_flag(&mod_cfg->general->settings, CDR_BATCHMODE)) {
  3436. ast_cli(a->fd, "Cannot submit CDR batch: batch mode not enabled.\n");
  3437. return CLI_SUCCESS;
  3438. }
  3439. submit_unscheduled_batch();
  3440. ast_cli(a->fd, "Submitted CDRs to backend engines for processing. This may take a while.\n");
  3441. return CLI_SUCCESS;
  3442. }
  3443. static struct ast_cli_entry cli_commands[] = {
  3444. AST_CLI_DEFINE(handle_cli_submit, "Posts all pending batched CDR data"),
  3445. AST_CLI_DEFINE(handle_cli_status, "Display the CDR status"),
  3446. AST_CLI_DEFINE(handle_cli_show, "Display active CDRs for channels"),
  3447. AST_CLI_DEFINE(handle_cli_debug, "Enable debugging in the CDR engine"),
  3448. };
  3449. /*!
  3450. * \brief This dispatches *all* \ref cdr_objects. It should only be used during
  3451. * shutdown, so that we get billing records for everything that we can.
  3452. */
  3453. static int cdr_object_dispatch_all_cb(void *obj, void *arg, int flags)
  3454. {
  3455. struct cdr_object *cdr = obj;
  3456. struct cdr_object *it_cdr;
  3457. ao2_lock(cdr);
  3458. for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
  3459. cdr_object_transition_state(it_cdr, &finalized_state_fn_table);
  3460. }
  3461. cdr_object_dispatch(cdr);
  3462. ao2_unlock(cdr);
  3463. return 0;
  3464. }
  3465. static void finalize_batch_mode(void)
  3466. {
  3467. if (cdr_thread == AST_PTHREADT_NULL) {
  3468. return;
  3469. }
  3470. /* wake up the thread so it will exit */
  3471. pthread_cancel(cdr_thread);
  3472. pthread_kill(cdr_thread, SIGURG);
  3473. pthread_join(cdr_thread, NULL);
  3474. cdr_thread = AST_PTHREADT_NULL;
  3475. ast_cond_destroy(&cdr_pending_cond);
  3476. ast_cdr_engine_term();
  3477. }
  3478. struct stasis_message_router *ast_cdr_message_router(void)
  3479. {
  3480. if (!stasis_router) {
  3481. return NULL;
  3482. }
  3483. ao2_bump(stasis_router);
  3484. return stasis_router;
  3485. }
  3486. /*!
  3487. * \brief Destroy the active Stasis subscriptions
  3488. */
  3489. static void destroy_subscriptions(void)
  3490. {
  3491. channel_subscription = stasis_forward_cancel(channel_subscription);
  3492. bridge_subscription = stasis_forward_cancel(bridge_subscription);
  3493. parking_subscription = stasis_forward_cancel(parking_subscription);
  3494. }
  3495. /*!
  3496. * \brief Create the Stasis subcriptions for CDRs
  3497. */
  3498. static int create_subscriptions(void)
  3499. {
  3500. if (!cdr_topic) {
  3501. return -1;
  3502. }
  3503. if (channel_subscription || bridge_subscription || parking_subscription) {
  3504. return 0;
  3505. }
  3506. channel_subscription = stasis_forward_all(ast_channel_topic_all_cached(), cdr_topic);
  3507. if (!channel_subscription) {
  3508. return -1;
  3509. }
  3510. bridge_subscription = stasis_forward_all(ast_bridge_topic_all_cached(), cdr_topic);
  3511. if (!bridge_subscription) {
  3512. return -1;
  3513. }
  3514. parking_subscription = stasis_forward_all(ast_parking_topic(), cdr_topic);
  3515. if (!parking_subscription) {
  3516. return -1;
  3517. }
  3518. return 0;
  3519. }
  3520. static int process_config(int reload)
  3521. {
  3522. RAII_VAR(struct module_config *, mod_cfg, module_config_alloc(), ao2_cleanup);
  3523. if (!reload) {
  3524. if (aco_info_init(&cfg_info)) {
  3525. return 1;
  3526. }
  3527. aco_option_register(&cfg_info, "enable", ACO_EXACT, general_options, DEFAULT_ENABLED, OPT_BOOLFLAG_T, 1, FLDSET(struct ast_cdr_config, settings), CDR_ENABLED);
  3528. aco_option_register(&cfg_info, "debug", ACO_EXACT, general_options, 0, OPT_BOOLFLAG_T, 1, FLDSET(struct ast_cdr_config, settings), CDR_DEBUG);
  3529. aco_option_register(&cfg_info, "unanswered", ACO_EXACT, general_options, DEFAULT_UNANSWERED, OPT_BOOLFLAG_T, 1, FLDSET(struct ast_cdr_config, settings), CDR_UNANSWERED);
  3530. aco_option_register(&cfg_info, "congestion", ACO_EXACT, general_options, 0, OPT_BOOLFLAG_T, 1, FLDSET(struct ast_cdr_config, settings), CDR_CONGESTION);
  3531. aco_option_register(&cfg_info, "batch", ACO_EXACT, general_options, DEFAULT_BATCHMODE, OPT_BOOLFLAG_T, 1, FLDSET(struct ast_cdr_config, settings), CDR_BATCHMODE);
  3532. aco_option_register(&cfg_info, "endbeforehexten", ACO_EXACT, general_options, DEFAULT_END_BEFORE_H_EXTEN, OPT_BOOLFLAG_T, 1, FLDSET(struct ast_cdr_config, settings), CDR_END_BEFORE_H_EXTEN);
  3533. aco_option_register(&cfg_info, "initiatedseconds", ACO_EXACT, general_options, DEFAULT_INITIATED_SECONDS, OPT_BOOLFLAG_T, 1, FLDSET(struct ast_cdr_config, settings), CDR_INITIATED_SECONDS);
  3534. aco_option_register(&cfg_info, "scheduleronly", ACO_EXACT, general_options, DEFAULT_BATCH_SCHEDULER_ONLY, OPT_BOOLFLAG_T, 1, FLDSET(struct ast_cdr_config, batch_settings.settings), BATCH_MODE_SCHEDULER_ONLY);
  3535. aco_option_register(&cfg_info, "safeshutdown", ACO_EXACT, general_options, DEFAULT_BATCH_SAFE_SHUTDOWN, OPT_BOOLFLAG_T, 1, FLDSET(struct ast_cdr_config, batch_settings.settings), BATCH_MODE_SAFE_SHUTDOWN);
  3536. aco_option_register(&cfg_info, "size", ACO_EXACT, general_options, DEFAULT_BATCH_SIZE, OPT_UINT_T, PARSE_IN_RANGE, FLDSET(struct ast_cdr_config, batch_settings.size), 0, MAX_BATCH_SIZE);
  3537. aco_option_register(&cfg_info, "time", ACO_EXACT, general_options, DEFAULT_BATCH_TIME, OPT_UINT_T, PARSE_IN_RANGE, FLDSET(struct ast_cdr_config, batch_settings.time), 0, MAX_BATCH_TIME);
  3538. }
  3539. if (aco_process_config(&cfg_info, reload)) {
  3540. if (!mod_cfg) {
  3541. return 1;
  3542. }
  3543. /* If we couldn't process the configuration and this wasn't a reload,
  3544. * create a default config
  3545. */
  3546. if (!reload && !(aco_set_defaults(&general_option, "general", mod_cfg->general))) {
  3547. ast_log(LOG_NOTICE, "Failed to process CDR configuration; using defaults\n");
  3548. ao2_global_obj_replace_unref(module_configs, mod_cfg);
  3549. return 0;
  3550. }
  3551. return 1;
  3552. }
  3553. return 0;
  3554. }
  3555. static void cdr_engine_cleanup(void)
  3556. {
  3557. destroy_subscriptions();
  3558. }
  3559. static void cdr_engine_shutdown(void)
  3560. {
  3561. stasis_message_router_unsubscribe_and_join(stasis_router);
  3562. stasis_router = NULL;
  3563. ao2_cleanup(cdr_topic);
  3564. cdr_topic = NULL;
  3565. STASIS_MESSAGE_TYPE_CLEANUP(cdr_sync_message_type);
  3566. ao2_callback(active_cdrs_by_channel, OBJ_NODATA, cdr_object_dispatch_all_cb,
  3567. NULL);
  3568. finalize_batch_mode();
  3569. ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
  3570. ast_sched_context_destroy(sched);
  3571. sched = NULL;
  3572. ast_free(batch);
  3573. batch = NULL;
  3574. aco_info_destroy(&cfg_info);
  3575. ao2_global_obj_release(module_configs);
  3576. ao2_container_unregister("cdrs_by_channel");
  3577. ao2_ref(active_cdrs_by_channel, -1);
  3578. active_cdrs_by_channel = NULL;
  3579. }
  3580. static void cdr_enable_batch_mode(struct ast_cdr_config *config)
  3581. {
  3582. SCOPED_LOCK(batch, &cdr_batch_lock, ast_mutex_lock, ast_mutex_unlock);
  3583. /* Only create the thread level portions once */
  3584. if (cdr_thread == AST_PTHREADT_NULL) {
  3585. ast_cond_init(&cdr_pending_cond, NULL);
  3586. if (ast_pthread_create_background(&cdr_thread, NULL, do_cdr, NULL) < 0) {
  3587. ast_log(LOG_ERROR, "Unable to start CDR thread.\n");
  3588. return;
  3589. }
  3590. }
  3591. /* Kill the currently scheduled item */
  3592. AST_SCHED_DEL(sched, cdr_sched);
  3593. cdr_sched = ast_sched_add(sched, config->batch_settings.time * 1000, submit_scheduled_batch, NULL);
  3594. ast_log(LOG_NOTICE, "CDR batch mode logging enabled, first of either size %d or time %d seconds.\n",
  3595. config->batch_settings.size, config->batch_settings.time);
  3596. }
  3597. /*!
  3598. * \internal
  3599. * \brief Print channel object key (name).
  3600. * \since 12.0.0
  3601. *
  3602. * \param v_obj A pointer to the object we want the key printed.
  3603. * \param where User data needed by prnt to determine where to put output.
  3604. * \param prnt Print output callback function to use.
  3605. *
  3606. * \return Nothing
  3607. */
  3608. static void cdr_container_print_fn(void *v_obj, void *where, ao2_prnt_fn *prnt)
  3609. {
  3610. struct cdr_object *cdr = v_obj;
  3611. struct cdr_object *it_cdr;
  3612. if (!cdr) {
  3613. return;
  3614. }
  3615. for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
  3616. prnt(where, "Party A: %s; Party B: %s; Bridge %s\n", it_cdr->party_a.snapshot->name, it_cdr->party_b.snapshot ? it_cdr->party_b.snapshot->name : "<unknown>",
  3617. it_cdr->bridge);
  3618. }
  3619. }
  3620. /*!
  3621. * \brief Checks if CDRs are enabled and enables/disables the necessary options
  3622. */
  3623. static int cdr_toggle_runtime_options(void)
  3624. {
  3625. RAII_VAR(struct module_config *, mod_cfg,
  3626. ao2_global_obj_ref(module_configs), ao2_cleanup);
  3627. if (ast_test_flag(&mod_cfg->general->settings, CDR_ENABLED)) {
  3628. if (create_subscriptions()) {
  3629. destroy_subscriptions();
  3630. ast_log(AST_LOG_ERROR, "Failed to create Stasis subscriptions\n");
  3631. return -1;
  3632. }
  3633. if (ast_test_flag(&mod_cfg->general->settings, CDR_BATCHMODE)) {
  3634. cdr_enable_batch_mode(mod_cfg->general);
  3635. } else {
  3636. ast_log(LOG_NOTICE, "CDR simple logging enabled.\n");
  3637. }
  3638. } else {
  3639. destroy_subscriptions();
  3640. ast_log(LOG_NOTICE, "CDR logging disabled.\n");
  3641. }
  3642. return 0;
  3643. }
  3644. int ast_cdr_engine_init(void)
  3645. {
  3646. if (process_config(0)) {
  3647. return -1;
  3648. }
  3649. cdr_topic = stasis_topic_create("cdr_engine");
  3650. if (!cdr_topic) {
  3651. return -1;
  3652. }
  3653. stasis_router = stasis_message_router_create(cdr_topic);
  3654. if (!stasis_router) {
  3655. return -1;
  3656. }
  3657. if (STASIS_MESSAGE_TYPE_INIT(cdr_sync_message_type)) {
  3658. return -1;
  3659. }
  3660. stasis_message_router_add_cache_update(stasis_router, ast_channel_snapshot_type(), handle_channel_cache_message, NULL);
  3661. stasis_message_router_add(stasis_router, ast_channel_dial_type(), handle_dial_message, NULL);
  3662. stasis_message_router_add(stasis_router, ast_channel_entered_bridge_type(), handle_bridge_enter_message, NULL);
  3663. stasis_message_router_add(stasis_router, ast_channel_left_bridge_type(), handle_bridge_leave_message, NULL);
  3664. stasis_message_router_add(stasis_router, ast_parked_call_type(), handle_parked_call_message, NULL);
  3665. stasis_message_router_add(stasis_router, cdr_sync_message_type(), handle_cdr_sync_message, NULL);
  3666. active_cdrs_by_channel = ao2_container_alloc(NUM_CDR_BUCKETS,
  3667. cdr_object_channel_hash_fn, cdr_object_channel_cmp_fn);
  3668. if (!active_cdrs_by_channel) {
  3669. return -1;
  3670. }
  3671. ao2_container_register("cdrs_by_channel", active_cdrs_by_channel, cdr_container_print_fn);
  3672. sched = ast_sched_context_create();
  3673. if (!sched) {
  3674. ast_log(LOG_ERROR, "Unable to create schedule context.\n");
  3675. return -1;
  3676. }
  3677. ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands));
  3678. ast_register_cleanup(cdr_engine_cleanup);
  3679. ast_register_atexit(cdr_engine_shutdown);
  3680. return cdr_toggle_runtime_options();
  3681. }
  3682. void ast_cdr_engine_term(void)
  3683. {
  3684. RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
  3685. RAII_VAR(void *, payload, ao2_alloc(sizeof(*payload), NULL), ao2_cleanup);
  3686. RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
  3687. /* Since this is called explicitly during process shutdown, we might not have ever
  3688. * been initialized. If so, the config object will be NULL.
  3689. */
  3690. if (!mod_cfg) {
  3691. return;
  3692. }
  3693. /* Make sure we have the needed items */
  3694. if (!stasis_router || !payload) {
  3695. return;
  3696. }
  3697. ast_debug(1, "CDR Engine termination request received; waiting on messages...\n");
  3698. message = stasis_message_create(cdr_sync_message_type(), payload);
  3699. if (message) {
  3700. stasis_message_router_publish_sync(stasis_router, message);
  3701. }
  3702. if (ast_test_flag(&mod_cfg->general->settings, CDR_BATCHMODE)) {
  3703. cdr_submit_batch(ast_test_flag(&mod_cfg->general->batch_settings.settings, BATCH_MODE_SAFE_SHUTDOWN));
  3704. }
  3705. }
  3706. int ast_cdr_engine_reload(void)
  3707. {
  3708. RAII_VAR(struct module_config *, old_mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
  3709. RAII_VAR(struct module_config *, mod_cfg, NULL, ao2_cleanup);
  3710. if (process_config(1)) {
  3711. return -1;
  3712. }
  3713. mod_cfg = ao2_global_obj_ref(module_configs);
  3714. if (!ast_test_flag(&mod_cfg->general->settings, CDR_ENABLED) ||
  3715. !(ast_test_flag(&mod_cfg->general->settings, CDR_BATCHMODE))) {
  3716. /* If batch mode used to be enabled, finalize the batch */
  3717. if (ast_test_flag(&old_mod_cfg->general->settings, CDR_BATCHMODE)) {
  3718. finalize_batch_mode();
  3719. }
  3720. }
  3721. return cdr_toggle_runtime_options();
  3722. }