cdr.c 140 KB

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