cdr.c 150 KB

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