pbx.c 263 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2008, 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 Core PBX routines.
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/_private.h"
  30. #include "asterisk/paths.h" /* use ast_config_AST_SYSTEM_NAME */
  31. #include <ctype.h>
  32. #include <time.h>
  33. #include <sys/time.h>
  34. #if defined(HAVE_SYSINFO)
  35. #include <sys/sysinfo.h>
  36. #endif
  37. #if defined(SOLARIS)
  38. #include <sys/loadavg.h>
  39. #endif
  40. #include "asterisk/lock.h"
  41. #include "asterisk/cli.h"
  42. #include "asterisk/pbx.h"
  43. #include "asterisk/channel.h"
  44. #include "asterisk/file.h"
  45. #include "asterisk/callerid.h"
  46. #include "asterisk/cdr.h"
  47. #include "asterisk/config.h"
  48. #include "asterisk/term.h"
  49. #include "asterisk/time.h"
  50. #include "asterisk/manager.h"
  51. #include "asterisk/ast_expr.h"
  52. #include "asterisk/linkedlists.h"
  53. #define SAY_STUBS /* generate declarations and stubs for say methods */
  54. #include "asterisk/say.h"
  55. #include "asterisk/utils.h"
  56. #include "asterisk/causes.h"
  57. #include "asterisk/musiconhold.h"
  58. #include "asterisk/app.h"
  59. #include "asterisk/devicestate.h"
  60. #include "asterisk/presencestate.h"
  61. #include "asterisk/hashtab.h"
  62. #include "asterisk/module.h"
  63. #include "asterisk/indications.h"
  64. #include "asterisk/taskprocessor.h"
  65. #include "asterisk/xmldoc.h"
  66. #include "asterisk/astobj2.h"
  67. #include "asterisk/stasis_channels.h"
  68. #include "asterisk/dial.h"
  69. #include "asterisk/vector.h"
  70. #include "pbx_private.h"
  71. /*!
  72. * \note I M P O R T A N T :
  73. *
  74. * The speed of extension handling will likely be among the most important
  75. * aspects of this PBX. The switching scheme as it exists right now isn't
  76. * terribly bad (it's O(N+M), where N is the # of extensions and M is the avg #
  77. * of priorities, but a constant search time here would be great ;-)
  78. *
  79. * A new algorithm to do searching based on a 'compiled' pattern tree is introduced
  80. * here, and shows a fairly flat (constant) search time, even for over
  81. * 10000 patterns.
  82. *
  83. * Also, using a hash table for context/priority name lookup can help prevent
  84. * the find_extension routines from absorbing exponential cpu cycles as the number
  85. * of contexts/priorities grow. I've previously tested find_extension with red-black trees,
  86. * which have O(log2(n)) speed. Right now, I'm using hash tables, which do
  87. * searches (ideally) in O(1) time. While these techniques do not yield much
  88. * speed in small dialplans, they are worth the trouble in large dialplans.
  89. *
  90. */
  91. /*** DOCUMENTATION
  92. <function name="EXCEPTION" language="en_US">
  93. <synopsis>
  94. Retrieve the details of the current dialplan exception.
  95. </synopsis>
  96. <syntax>
  97. <parameter name="field" required="true">
  98. <para>The following fields are available for retrieval:</para>
  99. <enumlist>
  100. <enum name="reason">
  101. <para>INVALID, ERROR, RESPONSETIMEOUT, ABSOLUTETIMEOUT, or custom
  102. value set by the RaiseException() application</para>
  103. </enum>
  104. <enum name="context">
  105. <para>The context executing when the exception occurred.</para>
  106. </enum>
  107. <enum name="exten">
  108. <para>The extension executing when the exception occurred.</para>
  109. </enum>
  110. <enum name="priority">
  111. <para>The numeric priority executing when the exception occurred.</para>
  112. </enum>
  113. </enumlist>
  114. </parameter>
  115. </syntax>
  116. <description>
  117. <para>Retrieve the details (specified <replaceable>field</replaceable>) of the current dialplan exception.</para>
  118. </description>
  119. <see-also>
  120. <ref type="application">RaiseException</ref>
  121. </see-also>
  122. </function>
  123. <function name="TESTTIME" language="en_US">
  124. <synopsis>
  125. Sets a time to be used with the channel to test logical conditions.
  126. </synopsis>
  127. <syntax>
  128. <parameter name="date" required="true" argsep=" ">
  129. <para>Date in ISO 8601 format</para>
  130. </parameter>
  131. <parameter name="time" required="true" argsep=" ">
  132. <para>Time in HH:MM:SS format (24-hour time)</para>
  133. </parameter>
  134. <parameter name="zone" required="false">
  135. <para>Timezone name</para>
  136. </parameter>
  137. </syntax>
  138. <description>
  139. <para>To test dialplan timing conditions at times other than the current time, use
  140. this function to set an alternate date and time. For example, you may wish to evaluate
  141. whether a location will correctly identify to callers that the area is closed on Christmas
  142. Day, when Christmas would otherwise fall on a day when the office is normally open.</para>
  143. </description>
  144. <see-also>
  145. <ref type="application">GotoIfTime</ref>
  146. </see-also>
  147. </function>
  148. <manager name="ShowDialPlan" language="en_US">
  149. <synopsis>
  150. Show dialplan contexts and extensions
  151. </synopsis>
  152. <syntax>
  153. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  154. <parameter name="Extension">
  155. <para>Show a specific extension.</para>
  156. </parameter>
  157. <parameter name="Context">
  158. <para>Show a specific context.</para>
  159. </parameter>
  160. </syntax>
  161. <description>
  162. <para>Show dialplan contexts and extensions. Be aware that showing the full dialplan
  163. may take a lot of capacity.</para>
  164. </description>
  165. </manager>
  166. <manager name="ExtensionStateList" language="en_US">
  167. <synopsis>
  168. List the current known extension states.
  169. </synopsis>
  170. <syntax>
  171. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  172. </syntax>
  173. <description>
  174. <para>This will list out all known extension states in a
  175. sequence of <replaceable>ExtensionStatus</replaceable> events.
  176. When finished, a <replaceable>ExtensionStateListComplete</replaceable> event
  177. will be emitted.</para>
  178. </description>
  179. <see-also>
  180. <ref type="manager">ExtensionState</ref>
  181. <ref type="function">HINT</ref>
  182. <ref type="function">EXTENSION_STATE</ref>
  183. </see-also>
  184. <responses>
  185. <list-elements>
  186. <xi:include xpointer="xpointer(/docs/managerEvent[@name='ExtensionStatus'])" />
  187. </list-elements>
  188. <managerEvent name="ExtensionStateListComplete" language="en_US">
  189. <managerEventInstance class="EVENT_FLAG_COMMAND">
  190. <synopsis>
  191. Indicates the end of the list the current known extension states.
  192. </synopsis>
  193. <syntax>
  194. <parameter name="EventList">
  195. <para>Conveys the status of the event list.</para>
  196. </parameter>
  197. <parameter name="ListItems">
  198. <para>Conveys the number of statuses reported.</para>
  199. </parameter>
  200. </syntax>
  201. </managerEventInstance>
  202. </managerEvent>
  203. </responses>
  204. </manager>
  205. ***/
  206. #ifdef LOW_MEMORY
  207. #define EXT_DATA_SIZE 256
  208. #else
  209. #define EXT_DATA_SIZE 8192
  210. #endif
  211. #define SWITCH_DATA_LENGTH 256
  212. #define VAR_NORMAL 1
  213. #define VAR_SOFTTRAN 2
  214. #define VAR_HARDTRAN 3
  215. struct ast_context;
  216. struct ast_app;
  217. AST_THREADSTORAGE(switch_data);
  218. AST_THREADSTORAGE(extensionstate_buf);
  219. /*!
  220. \brief ast_exten: An extension
  221. The dialplan is saved as a linked list with each context
  222. having it's own linked list of extensions - one item per
  223. priority.
  224. */
  225. struct ast_exten {
  226. char *exten; /*!< Clean Extension id */
  227. char *name; /*!< Extension name (may include '-' eye candy) */
  228. int matchcid; /*!< Match caller id ? */
  229. const char *cidmatch; /*!< Caller id to match for this extension */
  230. const char *cidmatch_display; /*!< Caller id to match (display version) */
  231. int priority; /*!< Priority */
  232. const char *label; /*!< Label */
  233. struct ast_context *parent; /*!< The context this extension belongs to */
  234. const char *app; /*!< Application to execute */
  235. struct ast_app *cached_app; /*!< Cached location of application */
  236. void *data; /*!< Data to use (arguments) */
  237. void (*datad)(void *); /*!< Data destructor */
  238. struct ast_exten *peer; /*!< Next higher priority with our extension */
  239. struct ast_hashtab *peer_table; /*!< Priorities list in hashtab form -- only on the head of the peer list */
  240. struct ast_hashtab *peer_label_table; /*!< labeled priorities in the peers -- only on the head of the peer list */
  241. const char *registrar; /*!< Registrar */
  242. struct ast_exten *next; /*!< Extension with a greater ID */
  243. char stuff[0];
  244. };
  245. /*! \brief ast_include: include= support in extensions.conf */
  246. struct ast_include {
  247. const char *name;
  248. const char *rname; /*!< Context to include */
  249. const char *registrar; /*!< Registrar */
  250. int hastime; /*!< If time construct exists */
  251. struct ast_timing timing; /*!< time construct */
  252. struct ast_include *next; /*!< Link them together */
  253. char stuff[0];
  254. };
  255. /*! \brief ast_sw: Switch statement in extensions.conf */
  256. struct ast_sw {
  257. char *name;
  258. const char *registrar; /*!< Registrar */
  259. char *data; /*!< Data load */
  260. int eval;
  261. AST_LIST_ENTRY(ast_sw) list;
  262. char stuff[0];
  263. };
  264. /*! \brief ast_ignorepat: Ignore patterns in dial plan */
  265. struct ast_ignorepat {
  266. const char *registrar;
  267. struct ast_ignorepat *next;
  268. const char pattern[0];
  269. };
  270. /*! \brief match_char: forms a syntax tree for quick matching of extension patterns */
  271. struct match_char
  272. {
  273. int is_pattern; /* the pattern started with '_' */
  274. int deleted; /* if this is set, then... don't return it */
  275. int specificity; /* simply the strlen of x, or 10 for X, 9 for Z, and 8 for N; and '.' and '!' will add 11 ? */
  276. struct match_char *alt_char;
  277. struct match_char *next_char;
  278. struct ast_exten *exten; /* attached to last char of a pattern for exten */
  279. char x[1]; /* the pattern itself-- matches a single char */
  280. };
  281. struct scoreboard /* make sure all fields are 0 before calling new_find_extension */
  282. {
  283. int total_specificity;
  284. int total_length;
  285. char last_char; /* set to ! or . if they are the end of the pattern */
  286. int canmatch; /* if the string to match was just too short */
  287. struct match_char *node;
  288. struct ast_exten *canmatch_exten;
  289. struct ast_exten *exten;
  290. };
  291. /*! \brief ast_context: An extension context */
  292. struct ast_context {
  293. ast_rwlock_t lock; /*!< A lock to prevent multiple threads from clobbering the context */
  294. struct ast_exten *root; /*!< The root of the list of extensions */
  295. struct ast_hashtab *root_table; /*!< For exact matches on the extensions in the pattern tree, and for traversals of the pattern_tree */
  296. struct match_char *pattern_tree; /*!< A tree to speed up extension pattern matching */
  297. struct ast_context *next; /*!< Link them together */
  298. struct ast_include *includes; /*!< Include other contexts */
  299. struct ast_ignorepat *ignorepats; /*!< Patterns for which to continue playing dialtone */
  300. char *registrar; /*!< Registrar -- make sure you malloc this, as the registrar may have to survive module unloads */
  301. int refcount; /*!< each module that would have created this context should inc/dec this as appropriate */
  302. AST_LIST_HEAD_NOLOCK(, ast_sw) alts; /*!< Alternative switches */
  303. ast_mutex_t macrolock; /*!< A lock to implement "exclusive" macros - held whilst a call is executing in the macro */
  304. char name[0]; /*!< Name of the context */
  305. };
  306. /*! \brief ast_state_cb: An extension state notify register item */
  307. struct ast_state_cb {
  308. /*! Watcher ID returned when registered. */
  309. int id;
  310. /*! Arbitrary data passed for callbacks. */
  311. void *data;
  312. /*! Flag if this callback is an extended callback containing detailed device status */
  313. int extended;
  314. /*! Callback when state changes. */
  315. ast_state_cb_type change_cb;
  316. /*! Callback when destroyed so any resources given by the registerer can be freed. */
  317. ast_state_cb_destroy_type destroy_cb;
  318. /*! \note Only used by ast_merge_contexts_and_delete */
  319. AST_LIST_ENTRY(ast_state_cb) entry;
  320. };
  321. /*!
  322. * \brief Structure for dial plan hints
  323. *
  324. * \note Hints are pointers from an extension in the dialplan to
  325. * one or more devices (tech/name)
  326. *
  327. * See \ref AstExtState
  328. */
  329. struct ast_hint {
  330. /*!
  331. * \brief Hint extension
  332. *
  333. * \note
  334. * Will never be NULL while the hint is in the hints container.
  335. */
  336. struct ast_exten *exten;
  337. struct ao2_container *callbacks; /*!< Device state callback container for this extension */
  338. /*! Dev state variables */
  339. int laststate; /*!< Last known device state */
  340. /*! Presence state variables */
  341. int last_presence_state; /*!< Last known presence state */
  342. char *last_presence_subtype; /*!< Last known presence subtype string */
  343. char *last_presence_message; /*!< Last known presence message string */
  344. char context_name[AST_MAX_CONTEXT];/*!< Context of destroyed hint extension. */
  345. char exten_name[AST_MAX_EXTENSION];/*!< Extension of destroyed hint extension. */
  346. AST_VECTOR(, char *) devices; /*!< Devices associated with the hint */
  347. };
  348. STASIS_MESSAGE_TYPE_DEFN_LOCAL(hint_change_message_type);
  349. STASIS_MESSAGE_TYPE_DEFN_LOCAL(hint_remove_message_type);
  350. #define HINTDEVICE_DATA_LENGTH 16
  351. AST_THREADSTORAGE(hintdevice_data);
  352. /* --- Hash tables of various objects --------*/
  353. #ifdef LOW_MEMORY
  354. #define HASH_EXTENHINT_SIZE 17
  355. #else
  356. #define HASH_EXTENHINT_SIZE 563
  357. #endif
  358. /*! \brief Container for hint devices */
  359. static struct ao2_container *hintdevices;
  360. /*!
  361. * \brief Structure for dial plan hint devices
  362. * \note hintdevice is one device pointing to a hint.
  363. */
  364. struct ast_hintdevice {
  365. /*!
  366. * \brief Hint this hintdevice belongs to.
  367. * \note Holds a reference to the hint object.
  368. */
  369. struct ast_hint *hint;
  370. /*! Name of the hint device. */
  371. char hintdevice[1];
  372. };
  373. /*!
  374. * \note Using the device for hash
  375. */
  376. static int hintdevice_hash_cb(const void *obj, const int flags)
  377. {
  378. const struct ast_hintdevice *ext;
  379. const char *key;
  380. switch (flags & OBJ_SEARCH_MASK) {
  381. case OBJ_SEARCH_KEY:
  382. key = obj;
  383. break;
  384. case OBJ_SEARCH_OBJECT:
  385. ext = obj;
  386. key = ext->hintdevice;
  387. break;
  388. default:
  389. ast_assert(0);
  390. return 0;
  391. }
  392. return ast_str_case_hash(key);
  393. }
  394. /*!
  395. * \note Devices on hints are not unique so no CMP_STOP is returned
  396. * Dont use ao2_find against hintdevices container cause there always
  397. * could be more than one result.
  398. */
  399. static int hintdevice_cmp_multiple(void *obj, void *arg, int flags)
  400. {
  401. struct ast_hintdevice *left = obj;
  402. struct ast_hintdevice *right = arg;
  403. const char *right_key = arg;
  404. int cmp;
  405. switch (flags & OBJ_SEARCH_MASK) {
  406. case OBJ_SEARCH_OBJECT:
  407. right_key = right->hintdevice;
  408. /* Fall through */
  409. case OBJ_SEARCH_KEY:
  410. cmp = strcasecmp(left->hintdevice, right_key);
  411. break;
  412. case OBJ_SEARCH_PARTIAL_KEY:
  413. /*
  414. * We could also use a partial key struct containing a length
  415. * so strlen() does not get called for every comparison instead.
  416. */
  417. cmp = strncmp(left->hintdevice, right_key, strlen(right_key));
  418. break;
  419. default:
  420. ast_assert(0);
  421. cmp = 0;
  422. break;
  423. }
  424. return cmp ? 0 : CMP_MATCH;
  425. }
  426. /*! \internal \brief \c ao2_callback function to remove hintdevices */
  427. static int hintdevice_remove_cb(void *obj, void *arg, void *data, int flags)
  428. {
  429. struct ast_hintdevice *candidate = obj;
  430. char *device = arg;
  431. struct ast_hint *hint = data;
  432. if (!strcasecmp(candidate->hintdevice, device)
  433. && candidate->hint == hint) {
  434. return CMP_MATCH;
  435. }
  436. return 0;
  437. }
  438. static int remove_hintdevice(struct ast_hint *hint)
  439. {
  440. while (AST_VECTOR_SIZE(&hint->devices) > 0) {
  441. char *device = AST_VECTOR_GET(&hint->devices, 0);
  442. ao2_t_callback_data(hintdevices, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA,
  443. hintdevice_remove_cb, device, hint, "Remove device from container");
  444. AST_VECTOR_REMOVE_UNORDERED(&hint->devices, 0);
  445. ast_free(device);
  446. }
  447. return 0;
  448. }
  449. static char *parse_hint_device(struct ast_str *hint_args);
  450. /*!
  451. * \internal
  452. * \brief Destroy the given hintdevice object.
  453. *
  454. * \param obj Hint device to destroy.
  455. *
  456. * \return Nothing
  457. */
  458. static void hintdevice_destroy(void *obj)
  459. {
  460. struct ast_hintdevice *doomed = obj;
  461. if (doomed->hint) {
  462. ao2_ref(doomed->hint, -1);
  463. doomed->hint = NULL;
  464. }
  465. }
  466. /*! \brief add hintdevice structure and link it into the container.
  467. */
  468. static int add_hintdevice(struct ast_hint *hint, const char *devicelist)
  469. {
  470. struct ast_str *str;
  471. char *parse;
  472. char *cur;
  473. struct ast_hintdevice *device;
  474. int devicelength;
  475. if (!hint || !devicelist) {
  476. /* Trying to add garbage? Don't bother. */
  477. return 0;
  478. }
  479. if (!(str = ast_str_thread_get(&hintdevice_data, 16))) {
  480. return -1;
  481. }
  482. ast_str_set(&str, 0, "%s", devicelist);
  483. parse = parse_hint_device(str);
  484. /* Spit on '&' and ',' to handle presence hints as well */
  485. while ((cur = strsep(&parse, "&,"))) {
  486. char *device_name;
  487. devicelength = strlen(cur);
  488. if (!devicelength) {
  489. continue;
  490. }
  491. device_name = ast_strdup(cur);
  492. if (!device_name) {
  493. return -1;
  494. }
  495. device = ao2_t_alloc(sizeof(*device) + devicelength, hintdevice_destroy,
  496. "allocating a hintdevice structure");
  497. if (!device) {
  498. ast_free(device_name);
  499. return -1;
  500. }
  501. strcpy(device->hintdevice, cur);
  502. ao2_ref(hint, +1);
  503. device->hint = hint;
  504. if (AST_VECTOR_APPEND(&hint->devices, device_name)) {
  505. ast_free(device_name);
  506. ao2_ref(device, -1);
  507. return -1;
  508. }
  509. ao2_t_link(hintdevices, device, "Linking device into hintdevice container.");
  510. ao2_t_ref(device, -1, "hintdevice is linked so we can unref");
  511. }
  512. return 0;
  513. }
  514. static const struct cfextension_states {
  515. int extension_state;
  516. const char * const text;
  517. } extension_states[] = {
  518. { AST_EXTENSION_NOT_INUSE, "Idle" },
  519. { AST_EXTENSION_INUSE, "InUse" },
  520. { AST_EXTENSION_BUSY, "Busy" },
  521. { AST_EXTENSION_UNAVAILABLE, "Unavailable" },
  522. { AST_EXTENSION_RINGING, "Ringing" },
  523. { AST_EXTENSION_INUSE | AST_EXTENSION_RINGING, "InUse&Ringing" },
  524. { AST_EXTENSION_ONHOLD, "Hold" },
  525. { AST_EXTENSION_INUSE | AST_EXTENSION_ONHOLD, "InUse&Hold" }
  526. };
  527. struct pbx_exception {
  528. AST_DECLARE_STRING_FIELDS(
  529. AST_STRING_FIELD(context); /*!< Context associated with this exception */
  530. AST_STRING_FIELD(exten); /*!< Exten associated with this exception */
  531. AST_STRING_FIELD(reason); /*!< The exception reason */
  532. );
  533. int priority; /*!< Priority associated with this exception */
  534. };
  535. static int matchcid(const char *cidpattern, const char *callerid);
  536. #ifdef NEED_DEBUG
  537. static void log_match_char_tree(struct match_char *node, char *prefix); /* for use anywhere */
  538. #endif
  539. static void new_find_extension(const char *str, struct scoreboard *score,
  540. struct match_char *tree, int length, int spec, const char *callerid,
  541. const char *label, enum ext_match_t action);
  542. static struct match_char *already_in_tree(struct match_char *current, char *pat, int is_pattern);
  543. static struct match_char *add_exten_to_pattern_tree(struct ast_context *con,
  544. struct ast_exten *e1, int findonly);
  545. static void create_match_char_tree(struct ast_context *con);
  546. static struct ast_exten *get_canmatch_exten(struct match_char *node);
  547. static void destroy_pattern_tree(struct match_char *pattern_tree);
  548. static int hashtab_compare_extens(const void *ha_a, const void *ah_b);
  549. static int hashtab_compare_exten_numbers(const void *ah_a, const void *ah_b);
  550. static int hashtab_compare_exten_labels(const void *ah_a, const void *ah_b);
  551. static unsigned int hashtab_hash_extens(const void *obj);
  552. static unsigned int hashtab_hash_priority(const void *obj);
  553. static unsigned int hashtab_hash_labels(const void *obj);
  554. static void __ast_internal_context_destroy( struct ast_context *con);
  555. static int ast_add_extension_nolock(const char *context, int replace, const char *extension,
  556. int priority, const char *label, const char *callerid,
  557. const char *application, void *data, void (*datad)(void *), const char *registrar);
  558. static int ast_add_extension2_lockopt(struct ast_context *con,
  559. int replace, const char *extension, int priority, const char *label, const char *callerid,
  560. const char *application, void *data, void (*datad)(void *),
  561. const char *registrar, int lock_context);
  562. static struct ast_context *find_context_locked(const char *context);
  563. static struct ast_context *find_context(const char *context);
  564. static void get_device_state_causing_channels(struct ao2_container *c);
  565. static unsigned int ext_strncpy(char *dst, const char *src, size_t dst_size, int nofluff);
  566. /*!
  567. * \internal
  568. * \brief Character array comparison function for qsort.
  569. *
  570. * \param a Left side object.
  571. * \param b Right side object.
  572. *
  573. * \retval <0 if a < b
  574. * \retval =0 if a = b
  575. * \retval >0 if a > b
  576. */
  577. static int compare_char(const void *a, const void *b)
  578. {
  579. const unsigned char *ac = a;
  580. const unsigned char *bc = b;
  581. return *ac - *bc;
  582. }
  583. /* labels, contexts are case sensitive priority numbers are ints */
  584. int ast_hashtab_compare_contexts(const void *ah_a, const void *ah_b)
  585. {
  586. const struct ast_context *ac = ah_a;
  587. const struct ast_context *bc = ah_b;
  588. if (!ac || !bc) /* safety valve, but it might prevent a crash you'd rather have happen */
  589. return 1;
  590. /* assume context names are registered in a string table! */
  591. return strcmp(ac->name, bc->name);
  592. }
  593. static int hashtab_compare_extens(const void *ah_a, const void *ah_b)
  594. {
  595. const struct ast_exten *ac = ah_a;
  596. const struct ast_exten *bc = ah_b;
  597. int x = strcmp(ac->exten, bc->exten);
  598. if (x) { /* if exten names are diff, then return */
  599. return x;
  600. }
  601. /* but if they are the same, do the cidmatch values match? */
  602. /* not sure which side may be using ast_ext_matchcid_types, so check both */
  603. if (ac->matchcid == AST_EXT_MATCHCID_ANY || bc->matchcid == AST_EXT_MATCHCID_ANY) {
  604. return 0;
  605. }
  606. if (ac->matchcid == AST_EXT_MATCHCID_OFF && bc->matchcid == AST_EXT_MATCHCID_OFF) {
  607. return 0;
  608. }
  609. if (ac->matchcid != bc->matchcid) {
  610. return 1;
  611. }
  612. /* all other cases already disposed of, match now required on callerid string (cidmatch) */
  613. /* although ast_add_extension2_lockopt() enforces non-zero ptr, caller may not have */
  614. if (ast_strlen_zero(ac->cidmatch) && ast_strlen_zero(bc->cidmatch)) {
  615. return 0;
  616. }
  617. return strcmp(ac->cidmatch, bc->cidmatch);
  618. }
  619. static int hashtab_compare_exten_numbers(const void *ah_a, const void *ah_b)
  620. {
  621. const struct ast_exten *ac = ah_a;
  622. const struct ast_exten *bc = ah_b;
  623. return ac->priority != bc->priority;
  624. }
  625. static int hashtab_compare_exten_labels(const void *ah_a, const void *ah_b)
  626. {
  627. const struct ast_exten *ac = ah_a;
  628. const struct ast_exten *bc = ah_b;
  629. return strcmp(S_OR(ac->label, ""), S_OR(bc->label, ""));
  630. }
  631. unsigned int ast_hashtab_hash_contexts(const void *obj)
  632. {
  633. const struct ast_context *ac = obj;
  634. return ast_hashtab_hash_string(ac->name);
  635. }
  636. static unsigned int hashtab_hash_extens(const void *obj)
  637. {
  638. const struct ast_exten *ac = obj;
  639. unsigned int x = ast_hashtab_hash_string(ac->exten);
  640. unsigned int y = 0;
  641. if (ac->matchcid == AST_EXT_MATCHCID_ON)
  642. y = ast_hashtab_hash_string(ac->cidmatch);
  643. return x+y;
  644. }
  645. static unsigned int hashtab_hash_priority(const void *obj)
  646. {
  647. const struct ast_exten *ac = obj;
  648. return ast_hashtab_hash_int(ac->priority);
  649. }
  650. static unsigned int hashtab_hash_labels(const void *obj)
  651. {
  652. const struct ast_exten *ac = obj;
  653. return ast_hashtab_hash_string(S_OR(ac->label, ""));
  654. }
  655. static int autofallthrough = 1;
  656. static int extenpatternmatchnew = 0;
  657. static char *overrideswitch = NULL;
  658. /*! \brief Subscription for device state change events */
  659. static struct stasis_subscription *device_state_sub;
  660. /*! \brief Subscription for presence state change events */
  661. static struct stasis_subscription *presence_state_sub;
  662. AST_MUTEX_DEFINE_STATIC(maxcalllock);
  663. static int countcalls;
  664. static int totalcalls;
  665. static struct ast_context *contexts;
  666. static struct ast_hashtab *contexts_table = NULL;
  667. /*!
  668. * \brief Lock for the ast_context list
  669. * \note
  670. * This lock MUST be recursive, or a deadlock on reload may result. See
  671. * https://issues.asterisk.org/view.php?id=17643
  672. */
  673. AST_MUTEX_DEFINE_STATIC(conlock);
  674. /*!
  675. * \brief Lock to hold off restructuring of hints by ast_merge_contexts_and_delete.
  676. */
  677. AST_MUTEX_DEFINE_STATIC(context_merge_lock);
  678. static int stateid = 1;
  679. /*!
  680. * \note When holding this container's lock, do _not_ do
  681. * anything that will cause conlock to be taken, unless you
  682. * _already_ hold it. The ast_merge_contexts_and_delete function
  683. * will take the locks in conlock/hints order, so any other
  684. * paths that require both locks must also take them in that
  685. * order.
  686. */
  687. static struct ao2_container *hints;
  688. static struct ao2_container *statecbs;
  689. #ifdef CONTEXT_DEBUG
  690. /* these routines are provided for doing run-time checks
  691. on the extension structures, in case you are having
  692. problems, this routine might help you localize where
  693. the problem is occurring. It's kinda like a debug memory
  694. allocator's arena checker... It'll eat up your cpu cycles!
  695. but you'll see, if you call it in the right places,
  696. right where your problems began...
  697. */
  698. /* you can break on the check_contexts_trouble()
  699. routine in your debugger to stop at the moment
  700. there's a problem */
  701. void check_contexts_trouble(void);
  702. void check_contexts_trouble(void)
  703. {
  704. int x = 1;
  705. x = 2;
  706. }
  707. int check_contexts(char *, int);
  708. int check_contexts(char *file, int line )
  709. {
  710. struct ast_hashtab_iter *t1;
  711. struct ast_context *c1, *c2;
  712. int found = 0;
  713. struct ast_exten *e1, *e2, *e3;
  714. struct ast_exten ex;
  715. /* try to find inconsistencies */
  716. /* is every context in the context table in the context list and vice-versa ? */
  717. if (!contexts_table) {
  718. ast_log(LOG_NOTICE,"Called from: %s:%d: No contexts_table!\n", file, line);
  719. usleep(500000);
  720. }
  721. t1 = ast_hashtab_start_traversal(contexts_table);
  722. while( (c1 = ast_hashtab_next(t1))) {
  723. for(c2=contexts;c2;c2=c2->next) {
  724. if (!strcmp(c1->name, c2->name)) {
  725. found = 1;
  726. break;
  727. }
  728. }
  729. if (!found) {
  730. ast_log(LOG_NOTICE,"Called from: %s:%d: Could not find the %s context in the linked list\n", file, line, c1->name);
  731. check_contexts_trouble();
  732. }
  733. }
  734. ast_hashtab_end_traversal(t1);
  735. for(c2=contexts;c2;c2=c2->next) {
  736. c1 = find_context_locked(c2->name);
  737. if (!c1) {
  738. ast_log(LOG_NOTICE,"Called from: %s:%d: Could not find the %s context in the hashtab\n", file, line, c2->name);
  739. check_contexts_trouble();
  740. } else
  741. ast_unlock_contexts();
  742. }
  743. /* loop thru all contexts, and verify the exten structure compares to the
  744. hashtab structure */
  745. for(c2=contexts;c2;c2=c2->next) {
  746. c1 = find_context_locked(c2->name);
  747. if (c1) {
  748. ast_unlock_contexts();
  749. /* is every entry in the root list also in the root_table? */
  750. for(e1 = c1->root; e1; e1=e1->next)
  751. {
  752. char dummy_name[1024];
  753. ex.exten = dummy_name;
  754. ex.matchcid = e1->matchcid;
  755. ex.cidmatch = e1->cidmatch;
  756. ast_copy_string(dummy_name, e1->exten, sizeof(dummy_name));
  757. e2 = ast_hashtab_lookup(c1->root_table, &ex);
  758. if (!e2) {
  759. if (e1->matchcid == AST_EXT_MATCHCID_ON) {
  760. ast_log(LOG_NOTICE, "Called from: %s:%d: The %s context records "
  761. "the exten %s (CID match: %s) but it is not in its root_table\n",
  762. file, line, c2->name, dummy_name, e1->cidmatch_display);
  763. } else {
  764. ast_log(LOG_NOTICE, "Called from: %s:%d: The %s context records "
  765. "the exten %s but it is not in its root_table\n",
  766. file, line, c2->name, dummy_name);
  767. }
  768. check_contexts_trouble();
  769. }
  770. }
  771. /* is every entry in the root_table also in the root list? */
  772. if (!c2->root_table) {
  773. if (c2->root) {
  774. ast_log(LOG_NOTICE,"Called from: %s:%d: No c2->root_table for context %s!\n", file, line, c2->name);
  775. usleep(500000);
  776. }
  777. } else {
  778. t1 = ast_hashtab_start_traversal(c2->root_table);
  779. while( (e2 = ast_hashtab_next(t1)) ) {
  780. for(e1=c2->root;e1;e1=e1->next) {
  781. if (!strcmp(e1->exten, e2->exten)) {
  782. found = 1;
  783. break;
  784. }
  785. }
  786. if (!found) {
  787. ast_log(LOG_NOTICE,"Called from: %s:%d: The %s context records the exten %s but it is not in its root_table\n", file, line, c2->name, e2->exten);
  788. check_contexts_trouble();
  789. }
  790. }
  791. ast_hashtab_end_traversal(t1);
  792. }
  793. }
  794. /* is every priority reflected in the peer_table at the head of the list? */
  795. /* is every entry in the root list also in the root_table? */
  796. /* are the per-extension peer_tables in the right place? */
  797. for(e1 = c2->root; e1; e1 = e1->next) {
  798. for(e2=e1;e2;e2=e2->peer) {
  799. ex.priority = e2->priority;
  800. if (e2 != e1 && e2->peer_table) {
  801. ast_log(LOG_NOTICE,"Called from: %s:%d: The %s context, %s exten, %d priority has a peer_table entry, and shouldn't!\n", file, line, c2->name, e1->exten, e2->priority );
  802. check_contexts_trouble();
  803. }
  804. if (e2 != e1 && e2->peer_label_table) {
  805. ast_log(LOG_NOTICE,"Called from: %s:%d: The %s context, %s exten, %d priority has a peer_label_table entry, and shouldn't!\n", file, line, c2->name, e1->exten, e2->priority );
  806. check_contexts_trouble();
  807. }
  808. if (e2 == e1 && !e2->peer_table){
  809. ast_log(LOG_NOTICE,"Called from: %s:%d: The %s context, %s exten, %d priority doesn't have a peer_table!\n", file, line, c2->name, e1->exten, e2->priority );
  810. check_contexts_trouble();
  811. }
  812. if (e2 == e1 && !e2->peer_label_table) {
  813. ast_log(LOG_NOTICE,"Called from: %s:%d: The %s context, %s exten, %d priority doesn't have a peer_label_table!\n", file, line, c2->name, e1->exten, e2->priority );
  814. check_contexts_trouble();
  815. }
  816. e3 = ast_hashtab_lookup(e1->peer_table, &ex);
  817. if (!e3) {
  818. ast_log(LOG_NOTICE,"Called from: %s:%d: The %s context, %s exten, %d priority is not reflected in the peer_table\n", file, line, c2->name, e1->exten, e2->priority );
  819. check_contexts_trouble();
  820. }
  821. }
  822. if (!e1->peer_table){
  823. ast_log(LOG_NOTICE,"Called from: %s:%d: No e1->peer_table!\n", file, line);
  824. usleep(500000);
  825. }
  826. /* is every entry in the peer_table also in the peer list? */
  827. t1 = ast_hashtab_start_traversal(e1->peer_table);
  828. while( (e2 = ast_hashtab_next(t1)) ) {
  829. for(e3=e1;e3;e3=e3->peer) {
  830. if (e3->priority == e2->priority) {
  831. found = 1;
  832. break;
  833. }
  834. }
  835. if (!found) {
  836. ast_log(LOG_NOTICE,"Called from: %s:%d: The %s context, %s exten, %d priority is not reflected in the peer list\n", file, line, c2->name, e1->exten, e2->priority );
  837. check_contexts_trouble();
  838. }
  839. }
  840. ast_hashtab_end_traversal(t1);
  841. }
  842. }
  843. return 0;
  844. }
  845. #endif
  846. static inline int include_valid(struct ast_include *i)
  847. {
  848. if (!i->hastime)
  849. return 1;
  850. return ast_check_timing(&(i->timing));
  851. }
  852. static void pbx_destroy(struct ast_pbx *p)
  853. {
  854. ast_free(p);
  855. }
  856. /* form a tree that fully describes all the patterns in a context's extensions
  857. * in this tree, a "node" represents an individual character or character set
  858. * meant to match the corresponding character in a dial string. The tree
  859. * consists of a series of match_char structs linked in a chain
  860. * via the alt_char pointers. More than one pattern can share the same parts of the
  861. * tree as other extensions with the same pattern to that point.
  862. * My first attempt to duplicate the finding of the 'best' pattern was flawed in that
  863. * I misunderstood the general algorithm. I thought that the 'best' pattern
  864. * was the one with lowest total score. This was not true. Thus, if you have
  865. * patterns "1XXXXX" and "X11111", you would be tempted to say that "X11111" is
  866. * the "best" match because it has fewer X's, and is therefore more specific,
  867. * but this is not how the old algorithm works. It sorts matching patterns
  868. * in a similar collating sequence as sorting alphabetic strings, from left to
  869. * right. Thus, "1XXXXX" comes before "X11111", and would be the "better" match,
  870. * because "1" is more specific than "X".
  871. * So, to accomodate this philosophy, I sort the tree branches along the alt_char
  872. * line so they are lowest to highest in specificity numbers. This way, as soon
  873. * as we encounter our first complete match, we automatically have the "best"
  874. * match and can stop the traversal immediately. Same for CANMATCH/MATCHMORE.
  875. * If anyone would like to resurrect the "wrong" pattern trie searching algorithm,
  876. * they are welcome to revert pbx to before 1 Apr 2008.
  877. * As an example, consider these 4 extensions:
  878. * (a) NXXNXXXXXX
  879. * (b) 307754XXXX
  880. * (c) fax
  881. * (d) NXXXXXXXXX
  882. *
  883. * In the above, between (a) and (d), (a) is a more specific pattern than (d), and would win over
  884. * most numbers. For all numbers beginning with 307754, (b) should always win.
  885. *
  886. * These pattern should form a (sorted) tree that looks like this:
  887. * { "3" } --next--> { "0" } --next--> { "7" } --next--> { "7" } --next--> { "5" } ... blah ... --> { "X" exten_match: (b) }
  888. * |
  889. * |alt
  890. * |
  891. * { "f" } --next--> { "a" } --next--> { "x" exten_match: (c) }
  892. * { "N" } --next--> { "X" } --next--> { "X" } --next--> { "N" } --next--> { "X" } ... blah ... --> { "X" exten_match: (a) }
  893. * | |
  894. * | |alt
  895. * |alt |
  896. * | { "X" } --next--> { "X" } ... blah ... --> { "X" exten_match: (d) }
  897. * |
  898. * NULL
  899. *
  900. * In the above, I could easily turn "N" into "23456789", but I think that a quick "if( *z >= '2' && *z <= '9' )" might take
  901. * fewer CPU cycles than a call to strchr("23456789",*z), where *z is the char to match...
  902. *
  903. * traversal is pretty simple: one routine merely traverses the alt list, and for each matching char in the pattern, it calls itself
  904. * on the corresponding next pointer, incrementing also the pointer of the string to be matched, and passing the total specificity and length.
  905. * We pass a pointer to a scoreboard down through, also.
  906. * The scoreboard isn't as necessary to the revised algorithm, but I kept it as a handy way to return the matched extension.
  907. * The first complete match ends the traversal, which should make this version of the pattern matcher faster
  908. * the previous. The same goes for "CANMATCH" or "MATCHMORE"; the first such match ends the traversal. In both
  909. * these cases, the reason we can stop immediately, is because the first pattern match found will be the "best"
  910. * according to the sort criteria.
  911. * Hope the limit on stack depth won't be a problem... this routine should
  912. * be pretty lean as far a stack usage goes. Any non-match terminates the recursion down a branch.
  913. *
  914. * In the above example, with the number "3077549999" as the pattern, the traversor could match extensions a, b and d. All are
  915. * of length 10; they have total specificities of 24580, 10246, and 25090, respectively, not that this matters
  916. * at all. (b) wins purely because the first character "3" is much more specific (lower specificity) than "N". I have
  917. * left the specificity totals in the code as an artifact; at some point, I will strip it out.
  918. *
  919. * Just how much time this algorithm might save over a plain linear traversal over all possible patterns is unknown,
  920. * because it's a function of how many extensions are stored in a context. With thousands of extensions, the speedup
  921. * can be very noticeable. The new matching algorithm can run several hundreds of times faster, if not a thousand or
  922. * more times faster in extreme cases.
  923. *
  924. * MatchCID patterns are also supported, and stored in the tree just as the extension pattern is. Thus, you
  925. * can have patterns in your CID field as well.
  926. *
  927. * */
  928. static void update_scoreboard(struct scoreboard *board, int length, int spec, struct ast_exten *exten, char last, const char *callerid, int deleted, struct match_char *node)
  929. {
  930. /* if this extension is marked as deleted, then skip this -- if it never shows
  931. on the scoreboard, it will never be found, nor will halt the traversal. */
  932. if (deleted)
  933. return;
  934. board->total_specificity = spec;
  935. board->total_length = length;
  936. board->exten = exten;
  937. board->last_char = last;
  938. board->node = node;
  939. #ifdef NEED_DEBUG_HERE
  940. ast_log(LOG_NOTICE,"Scoreboarding (LONGER) %s, len=%d, score=%d\n", exten->exten, length, spec);
  941. #endif
  942. }
  943. #ifdef NEED_DEBUG
  944. static void log_match_char_tree(struct match_char *node, char *prefix)
  945. {
  946. char extenstr[40];
  947. struct ast_str *my_prefix = ast_str_alloca(1024);
  948. extenstr[0] = '\0';
  949. if (node && node->exten)
  950. snprintf(extenstr, sizeof(extenstr), "(%p)", node->exten);
  951. if (strlen(node->x) > 1) {
  952. ast_debug(1, "%s[%s]:%c:%c:%d:%s%s%s\n", prefix, node->x, node->is_pattern ? 'Y':'N',
  953. node->deleted? 'D':'-', node->specificity, node->exten? "EXTEN:":"",
  954. node->exten ? node->exten->exten : "", extenstr);
  955. } else {
  956. ast_debug(1, "%s%s:%c:%c:%d:%s%s%s\n", prefix, node->x, node->is_pattern ? 'Y':'N',
  957. node->deleted? 'D':'-', node->specificity, node->exten? "EXTEN:":"",
  958. node->exten ? node->exten->exten : "", extenstr);
  959. }
  960. ast_str_set(&my_prefix, 0, "%s+ ", prefix);
  961. if (node->next_char)
  962. log_match_char_tree(node->next_char, ast_str_buffer(my_prefix));
  963. if (node->alt_char)
  964. log_match_char_tree(node->alt_char, prefix);
  965. }
  966. #endif
  967. static void cli_match_char_tree(struct match_char *node, char *prefix, int fd)
  968. {
  969. char extenstr[40];
  970. struct ast_str *my_prefix = ast_str_alloca(1024);
  971. extenstr[0] = '\0';
  972. if (node->exten) {
  973. snprintf(extenstr, sizeof(extenstr), "(%p)", node->exten);
  974. }
  975. if (strlen(node->x) > 1) {
  976. ast_cli(fd, "%s[%s]:%c:%c:%d:%s%s%s\n", prefix, node->x, node->is_pattern ? 'Y' : 'N',
  977. node->deleted ? 'D' : '-', node->specificity, node->exten? "EXTEN:" : "",
  978. node->exten ? node->exten->name : "", extenstr);
  979. } else {
  980. ast_cli(fd, "%s%s:%c:%c:%d:%s%s%s\n", prefix, node->x, node->is_pattern ? 'Y' : 'N',
  981. node->deleted ? 'D' : '-', node->specificity, node->exten? "EXTEN:" : "",
  982. node->exten ? node->exten->name : "", extenstr);
  983. }
  984. ast_str_set(&my_prefix, 0, "%s+ ", prefix);
  985. if (node->next_char)
  986. cli_match_char_tree(node->next_char, ast_str_buffer(my_prefix), fd);
  987. if (node->alt_char)
  988. cli_match_char_tree(node->alt_char, prefix, fd);
  989. }
  990. static struct ast_exten *get_canmatch_exten(struct match_char *node)
  991. {
  992. /* find the exten at the end of the rope */
  993. struct match_char *node2 = node;
  994. for (node2 = node; node2; node2 = node2->next_char) {
  995. if (node2->exten) {
  996. #ifdef NEED_DEBUG_HERE
  997. ast_log(LOG_NOTICE,"CanMatch_exten returns exten %s(%p)\n", node2->exten->exten, node2->exten);
  998. #endif
  999. return node2->exten;
  1000. }
  1001. }
  1002. #ifdef NEED_DEBUG_HERE
  1003. ast_log(LOG_NOTICE,"CanMatch_exten returns NULL, match_char=%s\n", node->x);
  1004. #endif
  1005. return 0;
  1006. }
  1007. static struct ast_exten *trie_find_next_match(struct match_char *node)
  1008. {
  1009. struct match_char *m3;
  1010. struct match_char *m4;
  1011. struct ast_exten *e3;
  1012. if (node && node->x[0] == '.' && !node->x[1]) { /* dot and ! will ALWAYS be next match in a matchmore */
  1013. return node->exten;
  1014. }
  1015. if (node && node->x[0] == '!' && !node->x[1]) {
  1016. return node->exten;
  1017. }
  1018. if (!node || !node->next_char) {
  1019. return NULL;
  1020. }
  1021. m3 = node->next_char;
  1022. if (m3->exten) {
  1023. return m3->exten;
  1024. }
  1025. for (m4 = m3->alt_char; m4; m4 = m4->alt_char) {
  1026. if (m4->exten) {
  1027. return m4->exten;
  1028. }
  1029. }
  1030. for (m4 = m3; m4; m4 = m4->alt_char) {
  1031. e3 = trie_find_next_match(m3);
  1032. if (e3) {
  1033. return e3;
  1034. }
  1035. }
  1036. return NULL;
  1037. }
  1038. #ifdef DEBUG_THIS
  1039. static char *action2str(enum ext_match_t action)
  1040. {
  1041. switch (action) {
  1042. case E_MATCH:
  1043. return "MATCH";
  1044. case E_CANMATCH:
  1045. return "CANMATCH";
  1046. case E_MATCHMORE:
  1047. return "MATCHMORE";
  1048. case E_FINDLABEL:
  1049. return "FINDLABEL";
  1050. case E_SPAWN:
  1051. return "SPAWN";
  1052. default:
  1053. return "?ACTION?";
  1054. }
  1055. }
  1056. #endif
  1057. static void new_find_extension(const char *str, struct scoreboard *score, struct match_char *tree, int length, int spec, const char *callerid, const char *label, enum ext_match_t action)
  1058. {
  1059. struct match_char *p; /* note minimal stack storage requirements */
  1060. struct ast_exten pattern = { .label = label };
  1061. #ifdef DEBUG_THIS
  1062. if (tree)
  1063. ast_log(LOG_NOTICE,"new_find_extension called with %s on (sub)tree %s action=%s\n", str, tree->x, action2str(action));
  1064. else
  1065. ast_log(LOG_NOTICE,"new_find_extension called with %s on (sub)tree NULL action=%s\n", str, action2str(action));
  1066. #endif
  1067. for (p = tree; p; p = p->alt_char) {
  1068. if (p->is_pattern) {
  1069. if (p->x[0] == 'N') {
  1070. if (p->x[1] == 0 && *str >= '2' && *str <= '9' ) {
  1071. #define NEW_MATCHER_CHK_MATCH \
  1072. if (p->exten && !(*(str + 1))) { /* if a shorter pattern matches along the way, might as well report it */ \
  1073. if (action == E_MATCH || action == E_SPAWN || action == E_FINDLABEL) { /* if in CANMATCH/MATCHMORE, don't let matches get in the way */ \
  1074. update_scoreboard(score, length + 1, spec + p->specificity, p->exten, 0, callerid, p->deleted, p); \
  1075. if (!p->deleted) { \
  1076. if (action == E_FINDLABEL) { \
  1077. if (ast_hashtab_lookup(score->exten->peer_label_table, &pattern)) { \
  1078. ast_debug(4, "Found label in preferred extension\n"); \
  1079. return; \
  1080. } \
  1081. } else { \
  1082. ast_debug(4, "returning an exact match-- first found-- %s\n", p->exten->name); \
  1083. return; /* the first match, by definition, will be the best, because of the sorted tree */ \
  1084. } \
  1085. } \
  1086. } \
  1087. }
  1088. #define NEW_MATCHER_RECURSE \
  1089. if (p->next_char && (*(str + 1) || (p->next_char->x[0] == '/' && p->next_char->x[1] == 0) \
  1090. || p->next_char->x[0] == '!')) { \
  1091. if (*(str + 1) || p->next_char->x[0] == '!') { \
  1092. new_find_extension(str + 1, score, p->next_char, length + 1, spec + p->specificity, callerid, label, action); \
  1093. if (score->exten) { \
  1094. ast_debug(4 ,"returning an exact match-- %s\n", score->exten->name); \
  1095. return; /* the first match is all we need */ \
  1096. } \
  1097. } else { \
  1098. new_find_extension("/", score, p->next_char, length + 1, spec + p->specificity, callerid, label, action); \
  1099. if (score->exten || ((action == E_CANMATCH || action == E_MATCHMORE) && score->canmatch)) { \
  1100. ast_debug(4,"returning a (can/more) match--- %s\n", score->exten ? score->exten->name : \
  1101. "NULL"); \
  1102. return; /* the first match is all we need */ \
  1103. } \
  1104. } \
  1105. } else if ((p->next_char || action == E_CANMATCH) && !*(str + 1)) { \
  1106. score->canmatch = 1; \
  1107. score->canmatch_exten = get_canmatch_exten(p); \
  1108. if (action == E_CANMATCH || action == E_MATCHMORE) { \
  1109. ast_debug(4, "returning a canmatch/matchmore--- str=%s\n", str); \
  1110. return; \
  1111. } \
  1112. }
  1113. NEW_MATCHER_CHK_MATCH;
  1114. NEW_MATCHER_RECURSE;
  1115. }
  1116. } else if (p->x[0] == 'Z') {
  1117. if (p->x[1] == 0 && *str >= '1' && *str <= '9' ) {
  1118. NEW_MATCHER_CHK_MATCH;
  1119. NEW_MATCHER_RECURSE;
  1120. }
  1121. } else if (p->x[0] == 'X') {
  1122. if (p->x[1] == 0 && *str >= '0' && *str <= '9' ) {
  1123. NEW_MATCHER_CHK_MATCH;
  1124. NEW_MATCHER_RECURSE;
  1125. }
  1126. } else if (p->x[0] == '.' && p->x[1] == 0) {
  1127. /* how many chars will the . match against? */
  1128. int i = 0;
  1129. const char *str2 = str;
  1130. while (*str2 && *str2 != '/') {
  1131. str2++;
  1132. i++;
  1133. }
  1134. if (p->exten && *str2 != '/') {
  1135. update_scoreboard(score, length + i, spec + (i * p->specificity), p->exten, '.', callerid, p->deleted, p);
  1136. if (score->exten) {
  1137. ast_debug(4, "return because scoreboard has a match with '/'--- %s\n",
  1138. score->exten->name);
  1139. return; /* the first match is all we need */
  1140. }
  1141. }
  1142. if (p->next_char && p->next_char->x[0] == '/' && p->next_char->x[1] == 0) {
  1143. new_find_extension("/", score, p->next_char, length + i, spec+(p->specificity*i), callerid, label, action);
  1144. if (score->exten || ((action == E_CANMATCH || action == E_MATCHMORE) && score->canmatch)) {
  1145. ast_debug(4, "return because scoreboard has exact match OR "
  1146. "CANMATCH/MATCHMORE & canmatch set--- %s\n",
  1147. score->exten ? score->exten->name : "NULL");
  1148. return; /* the first match is all we need */
  1149. }
  1150. }
  1151. } else if (p->x[0] == '!' && p->x[1] == 0) {
  1152. /* how many chars will the . match against? */
  1153. int i = 1;
  1154. const char *str2 = str;
  1155. while (*str2 && *str2 != '/') {
  1156. str2++;
  1157. i++;
  1158. }
  1159. if (p->exten && *str2 != '/') {
  1160. update_scoreboard(score, length + 1, spec + (p->specificity * i), p->exten, '!', callerid, p->deleted, p);
  1161. if (score->exten) {
  1162. ast_debug(4, "return because scoreboard has a '!' match--- %s\n",
  1163. score->exten->name);
  1164. return; /* the first match is all we need */
  1165. }
  1166. }
  1167. if (p->next_char && p->next_char->x[0] == '/' && p->next_char->x[1] == 0) {
  1168. new_find_extension("/", score, p->next_char, length + i, spec + (p->specificity * i), callerid, label, action);
  1169. if (score->exten || ((action == E_CANMATCH || action == E_MATCHMORE) && score->canmatch)) {
  1170. ast_debug(4, "return because scoreboard has exact match OR "
  1171. "CANMATCH/MATCHMORE & canmatch set with '/' and '!'--- %s\n",
  1172. score->exten ? score->exten->name : "NULL");
  1173. return; /* the first match is all we need */
  1174. }
  1175. }
  1176. } else if (p->x[0] == '/' && p->x[1] == 0) {
  1177. /* the pattern in the tree includes the cid match! */
  1178. if (p->next_char && callerid && *callerid) {
  1179. new_find_extension(callerid, score, p->next_char, length + 1, spec, callerid, label, action);
  1180. if (score->exten || ((action == E_CANMATCH || action == E_MATCHMORE) && score->canmatch)) {
  1181. ast_debug(4, "return because scoreboard has exact match OR "
  1182. "CANMATCH/MATCHMORE & canmatch set with '/'--- %s\n",
  1183. score->exten ? score->exten->name : "NULL");
  1184. return; /* the first match is all we need */
  1185. }
  1186. }
  1187. } else if (strchr(p->x, *str)) {
  1188. ast_debug(4, "Nothing strange about this match\n");
  1189. NEW_MATCHER_CHK_MATCH;
  1190. NEW_MATCHER_RECURSE;
  1191. }
  1192. } else if (strchr(p->x, *str)) {
  1193. ast_debug(4, "Nothing strange about this match\n");
  1194. NEW_MATCHER_CHK_MATCH;
  1195. NEW_MATCHER_RECURSE;
  1196. }
  1197. }
  1198. ast_debug(4, "return at end of func\n");
  1199. }
  1200. /* the algorithm for forming the extension pattern tree is also a bit simple; you
  1201. * traverse all the extensions in a context, and for each char of the extension,
  1202. * you see if it exists in the tree; if it doesn't, you add it at the appropriate
  1203. * spot. What more can I say? At the end of each exten, you cap it off by adding the
  1204. * address of the extension involved. Duplicate patterns will be complained about.
  1205. *
  1206. * Ideally, this would be done for each context after it is created and fully
  1207. * filled. It could be done as a finishing step after extensions.conf or .ael is
  1208. * loaded, or it could be done when the first search is encountered. It should only
  1209. * have to be done once, until the next unload or reload.
  1210. *
  1211. * I guess forming this pattern tree would be analogous to compiling a regex. Except
  1212. * that a regex only handles 1 pattern, really. This trie holds any number
  1213. * of patterns. Well, really, it **could** be considered a single pattern,
  1214. * where the "|" (or) operator is allowed, I guess, in a way, sort of...
  1215. */
  1216. static struct match_char *already_in_tree(struct match_char *current, char *pat, int is_pattern)
  1217. {
  1218. struct match_char *t;
  1219. if (!current) {
  1220. return 0;
  1221. }
  1222. for (t = current; t; t = t->alt_char) {
  1223. if (is_pattern == t->is_pattern && !strcmp(pat, t->x)) {/* uh, we may want to sort exploded [] contents to make matching easy */
  1224. return t;
  1225. }
  1226. }
  1227. return 0;
  1228. }
  1229. /* The first arg is the location of the tree ptr, or the
  1230. address of the next_char ptr in the node, so we can mess
  1231. with it, if we need to insert at the beginning of the list */
  1232. static void insert_in_next_chars_alt_char_list(struct match_char **parent_ptr, struct match_char *node)
  1233. {
  1234. struct match_char *curr, *lcurr;
  1235. /* insert node into the tree at "current", so the alt_char list from current is
  1236. sorted in increasing value as you go to the leaves */
  1237. if (!(*parent_ptr)) {
  1238. *parent_ptr = node;
  1239. return;
  1240. }
  1241. if ((*parent_ptr)->specificity > node->specificity) {
  1242. /* insert at head */
  1243. node->alt_char = (*parent_ptr);
  1244. *parent_ptr = node;
  1245. return;
  1246. }
  1247. lcurr = *parent_ptr;
  1248. for (curr = (*parent_ptr)->alt_char; curr; curr = curr->alt_char) {
  1249. if (curr->specificity > node->specificity) {
  1250. node->alt_char = curr;
  1251. lcurr->alt_char = node;
  1252. break;
  1253. }
  1254. lcurr = curr;
  1255. }
  1256. if (!curr) {
  1257. lcurr->alt_char = node;
  1258. }
  1259. }
  1260. struct pattern_node {
  1261. /*! Pattern node specificity */
  1262. int specif;
  1263. /*! Pattern node match characters. */
  1264. char buf[256];
  1265. };
  1266. static struct match_char *add_pattern_node(struct ast_context *con, struct match_char *current, const struct pattern_node *pattern, int is_pattern, int already, struct match_char **nextcharptr)
  1267. {
  1268. struct match_char *m;
  1269. if (!(m = ast_calloc(1, sizeof(*m) + strlen(pattern->buf)))) {
  1270. return NULL;
  1271. }
  1272. /* strcpy is safe here since we know its size and have allocated
  1273. * just enough space for when we allocated m
  1274. */
  1275. strcpy(m->x, pattern->buf);
  1276. /* the specificity scores are the same as used in the old
  1277. pattern matcher. */
  1278. m->is_pattern = is_pattern;
  1279. if (pattern->specif == 1 && is_pattern && pattern->buf[0] == 'N') {
  1280. m->specificity = 0x0832;
  1281. } else if (pattern->specif == 1 && is_pattern && pattern->buf[0] == 'Z') {
  1282. m->specificity = 0x0931;
  1283. } else if (pattern->specif == 1 && is_pattern && pattern->buf[0] == 'X') {
  1284. m->specificity = 0x0a30;
  1285. } else if (pattern->specif == 1 && is_pattern && pattern->buf[0] == '.') {
  1286. m->specificity = 0x18000;
  1287. } else if (pattern->specif == 1 && is_pattern && pattern->buf[0] == '!') {
  1288. m->specificity = 0x28000;
  1289. } else {
  1290. m->specificity = pattern->specif;
  1291. }
  1292. if (!con->pattern_tree) {
  1293. insert_in_next_chars_alt_char_list(&con->pattern_tree, m);
  1294. } else {
  1295. if (already) { /* switch to the new regime (traversing vs appending)*/
  1296. insert_in_next_chars_alt_char_list(nextcharptr, m);
  1297. } else {
  1298. insert_in_next_chars_alt_char_list(&current->next_char, m);
  1299. }
  1300. }
  1301. return m;
  1302. }
  1303. /*!
  1304. * \internal
  1305. * \brief Extract the next exten pattern node.
  1306. *
  1307. * \param node Pattern node to fill.
  1308. * \param src Next source character to read.
  1309. * \param pattern TRUE if the exten is a pattern.
  1310. * \param extenbuf Original exten buffer to use in diagnostic messages.
  1311. *
  1312. * \retval Ptr to next extenbuf pos to read.
  1313. */
  1314. static const char *get_pattern_node(struct pattern_node *node, const char *src, int pattern, const char *extenbuf)
  1315. {
  1316. #define INC_DST_OVERFLOW_CHECK \
  1317. do { \
  1318. if (dst - node->buf < sizeof(node->buf) - 1) { \
  1319. ++dst; \
  1320. } else { \
  1321. overflow = 1; \
  1322. } \
  1323. } while (0)
  1324. node->specif = 0;
  1325. node->buf[0] = '\0';
  1326. while (*src) {
  1327. if (*src == '[' && pattern) {
  1328. char *dst = node->buf;
  1329. const char *src_next;
  1330. int length;
  1331. int overflow = 0;
  1332. /* get past the '[' */
  1333. ++src;
  1334. for (;;) {
  1335. if (*src == '\\') {
  1336. /* Escaped character. */
  1337. ++src;
  1338. if (*src == '[' || *src == '\\' || *src == '-' || *src == ']') {
  1339. *dst = *src++;
  1340. INC_DST_OVERFLOW_CHECK;
  1341. }
  1342. } else if (*src == '-') {
  1343. unsigned char first;
  1344. unsigned char last;
  1345. src_next = src;
  1346. first = *(src_next - 1);
  1347. last = *++src_next;
  1348. if (last == '\\') {
  1349. /* Escaped character. */
  1350. last = *++src_next;
  1351. }
  1352. /* Possible char range. */
  1353. if (node->buf[0] && last) {
  1354. /* Expand the char range. */
  1355. while (++first <= last) {
  1356. *dst = first;
  1357. INC_DST_OVERFLOW_CHECK;
  1358. }
  1359. src = src_next + 1;
  1360. } else {
  1361. /*
  1362. * There was no left or right char for the range.
  1363. * It is just a '-'.
  1364. */
  1365. *dst = *src++;
  1366. INC_DST_OVERFLOW_CHECK;
  1367. }
  1368. } else if (*src == '\0') {
  1369. ast_log(LOG_WARNING,
  1370. "A matching ']' was not found for '[' in exten pattern '%s'\n",
  1371. extenbuf);
  1372. break;
  1373. } else if (*src == ']') {
  1374. ++src;
  1375. break;
  1376. } else {
  1377. *dst = *src++;
  1378. INC_DST_OVERFLOW_CHECK;
  1379. }
  1380. }
  1381. /* null terminate the exploded range */
  1382. *dst = '\0';
  1383. if (overflow) {
  1384. ast_log(LOG_ERROR,
  1385. "Expanded character set too large to deal with in exten pattern '%s'. Ignoring character set.\n",
  1386. extenbuf);
  1387. node->buf[0] = '\0';
  1388. continue;
  1389. }
  1390. /* Sort the characters in character set. */
  1391. length = strlen(node->buf);
  1392. if (!length) {
  1393. ast_log(LOG_WARNING, "Empty character set in exten pattern '%s'. Ignoring.\n",
  1394. extenbuf);
  1395. node->buf[0] = '\0';
  1396. continue;
  1397. }
  1398. qsort(node->buf, length, 1, compare_char);
  1399. /* Remove duplicate characters from character set. */
  1400. dst = node->buf;
  1401. src_next = node->buf;
  1402. while (*src_next++) {
  1403. if (*dst != *src_next) {
  1404. *++dst = *src_next;
  1405. }
  1406. }
  1407. length = strlen(node->buf);
  1408. length <<= 8;
  1409. node->specif = length | (unsigned char) node->buf[0];
  1410. break;
  1411. } else if (*src == '-') {
  1412. /* Skip dashes in all extensions. */
  1413. ++src;
  1414. } else {
  1415. if (*src == '\\') {
  1416. /*
  1417. * XXX The escape character here does not remove any special
  1418. * meaning to characters except the '[', '\\', and '-'
  1419. * characters since they are special only in this function.
  1420. */
  1421. node->buf[0] = *++src;
  1422. if (!node->buf[0]) {
  1423. break;
  1424. }
  1425. } else {
  1426. node->buf[0] = *src;
  1427. if (pattern) {
  1428. /* make sure n,x,z patterns are canonicalized to N,X,Z */
  1429. if (node->buf[0] == 'n') {
  1430. node->buf[0] = 'N';
  1431. } else if (node->buf[0] == 'x') {
  1432. node->buf[0] = 'X';
  1433. } else if (node->buf[0] == 'z') {
  1434. node->buf[0] = 'Z';
  1435. }
  1436. }
  1437. }
  1438. node->buf[1] = '\0';
  1439. node->specif = 1;
  1440. ++src;
  1441. break;
  1442. }
  1443. }
  1444. return src;
  1445. #undef INC_DST_OVERFLOW_CHECK
  1446. }
  1447. static struct match_char *add_exten_to_pattern_tree(struct ast_context *con, struct ast_exten *e1, int findonly)
  1448. {
  1449. struct match_char *m1 = NULL;
  1450. struct match_char *m2 = NULL;
  1451. struct match_char **m0;
  1452. const char *pos;
  1453. int already;
  1454. int pattern = 0;
  1455. int idx_cur;
  1456. int idx_next;
  1457. char extenbuf[512];
  1458. struct pattern_node pat_node[2];
  1459. if (e1->matchcid) {
  1460. if (sizeof(extenbuf) < strlen(e1->exten) + strlen(e1->cidmatch) + 2) {
  1461. ast_log(LOG_ERROR,
  1462. "The pattern %s/%s is too big to deal with: it will be ignored! Disaster!\n",
  1463. e1->exten, e1->cidmatch);
  1464. return NULL;
  1465. }
  1466. sprintf(extenbuf, "%s/%s", e1->exten, e1->cidmatch);/* Safe. We just checked. */
  1467. } else {
  1468. ast_copy_string(extenbuf, e1->exten, sizeof(extenbuf));
  1469. }
  1470. #ifdef NEED_DEBUG
  1471. ast_debug(1, "Adding exten %s to tree\n", extenbuf);
  1472. #endif
  1473. m1 = con->pattern_tree; /* each pattern starts over at the root of the pattern tree */
  1474. m0 = &con->pattern_tree;
  1475. already = 1;
  1476. pos = extenbuf;
  1477. if (*pos == '_') {
  1478. pattern = 1;
  1479. ++pos;
  1480. }
  1481. idx_cur = 0;
  1482. pos = get_pattern_node(&pat_node[idx_cur], pos, pattern, extenbuf);
  1483. for (; pat_node[idx_cur].buf[0]; idx_cur = idx_next) {
  1484. idx_next = (idx_cur + 1) % ARRAY_LEN(pat_node);
  1485. pos = get_pattern_node(&pat_node[idx_next], pos, pattern, extenbuf);
  1486. /* See about adding node to tree. */
  1487. m2 = NULL;
  1488. if (already && (m2 = already_in_tree(m1, pat_node[idx_cur].buf, pattern))
  1489. && m2->next_char) {
  1490. if (!pat_node[idx_next].buf[0]) {
  1491. /*
  1492. * This is the end of the pattern, but not the end of the tree.
  1493. * Mark this node with the exten... a shorter pattern might win
  1494. * if the longer one doesn't match.
  1495. */
  1496. if (findonly) {
  1497. return m2;
  1498. }
  1499. if (m2->exten) {
  1500. ast_log(LOG_WARNING, "Found duplicate exten. Had %s found %s\n",
  1501. m2->deleted ? "(deleted/invalid)" : m2->exten->name, e1->name);
  1502. }
  1503. m2->exten = e1;
  1504. m2->deleted = 0;
  1505. }
  1506. m1 = m2->next_char; /* m1 points to the node to compare against */
  1507. m0 = &m2->next_char; /* m0 points to the ptr that points to m1 */
  1508. } else { /* not already OR not m2 OR nor m2->next_char */
  1509. if (m2) {
  1510. if (findonly) {
  1511. return m2;
  1512. }
  1513. m1 = m2; /* while m0 stays the same */
  1514. } else {
  1515. if (findonly) {
  1516. return m1;
  1517. }
  1518. m1 = add_pattern_node(con, m1, &pat_node[idx_cur], pattern, already, m0);
  1519. if (!m1) { /* m1 is the node just added */
  1520. return NULL;
  1521. }
  1522. m0 = &m1->next_char;
  1523. }
  1524. if (!pat_node[idx_next].buf[0]) {
  1525. if (m2 && m2->exten) {
  1526. ast_log(LOG_WARNING, "Found duplicate exten. Had %s found %s\n",
  1527. m2->deleted ? "(deleted/invalid)" : m2->exten->name, e1->name);
  1528. }
  1529. m1->deleted = 0;
  1530. m1->exten = e1;
  1531. }
  1532. /* The 'already' variable is a mini-optimization designed to make it so that we
  1533. * don't have to call already_in_tree when we know it will return false.
  1534. */
  1535. already = 0;
  1536. }
  1537. }
  1538. return m1;
  1539. }
  1540. static void create_match_char_tree(struct ast_context *con)
  1541. {
  1542. struct ast_hashtab_iter *t1;
  1543. struct ast_exten *e1;
  1544. #ifdef NEED_DEBUG
  1545. int biggest_bucket, resizes, numobjs, numbucks;
  1546. ast_debug(1, "Creating Extension Trie for context %s(%p)\n", con->name, con);
  1547. ast_hashtab_get_stats(con->root_table, &biggest_bucket, &resizes, &numobjs, &numbucks);
  1548. ast_debug(1, "This tree has %d objects in %d bucket lists, longest list=%d objects, and has resized %d times\n",
  1549. numobjs, numbucks, biggest_bucket, resizes);
  1550. #endif
  1551. t1 = ast_hashtab_start_traversal(con->root_table);
  1552. while ((e1 = ast_hashtab_next(t1))) {
  1553. if (e1->exten) {
  1554. add_exten_to_pattern_tree(con, e1, 0);
  1555. } else {
  1556. ast_log(LOG_ERROR, "Attempt to create extension with no extension name.\n");
  1557. }
  1558. }
  1559. ast_hashtab_end_traversal(t1);
  1560. }
  1561. static void destroy_pattern_tree(struct match_char *pattern_tree) /* pattern tree is a simple binary tree, sort of, so the proper way to destroy it is... recursively! */
  1562. {
  1563. /* destroy all the alternates */
  1564. if (pattern_tree->alt_char) {
  1565. destroy_pattern_tree(pattern_tree->alt_char);
  1566. pattern_tree->alt_char = 0;
  1567. }
  1568. /* destroy all the nexts */
  1569. if (pattern_tree->next_char) {
  1570. destroy_pattern_tree(pattern_tree->next_char);
  1571. pattern_tree->next_char = 0;
  1572. }
  1573. pattern_tree->exten = 0; /* never hurts to make sure there's no pointers laying around */
  1574. ast_free(pattern_tree);
  1575. }
  1576. /*!
  1577. * \internal
  1578. * \brief Get the length of the exten string.
  1579. *
  1580. * \param str Exten to get length.
  1581. *
  1582. * \retval strlen of exten.
  1583. */
  1584. static int ext_cmp_exten_strlen(const char *str)
  1585. {
  1586. int len;
  1587. len = 0;
  1588. for (;;) {
  1589. /* Ignore '-' chars as eye candy fluff. */
  1590. while (*str == '-') {
  1591. ++str;
  1592. }
  1593. if (!*str) {
  1594. break;
  1595. }
  1596. ++str;
  1597. ++len;
  1598. }
  1599. return len;
  1600. }
  1601. /*!
  1602. * \internal
  1603. * \brief Partial comparison of non-pattern extens.
  1604. *
  1605. * \param left Exten to compare.
  1606. * \param right Exten to compare. Also matches if this string ends first.
  1607. *
  1608. * \retval <0 if left < right
  1609. * \retval =0 if left == right
  1610. * \retval >0 if left > right
  1611. */
  1612. static int ext_cmp_exten_partial(const char *left, const char *right)
  1613. {
  1614. int cmp;
  1615. for (;;) {
  1616. /* Ignore '-' chars as eye candy fluff. */
  1617. while (*left == '-') {
  1618. ++left;
  1619. }
  1620. while (*right == '-') {
  1621. ++right;
  1622. }
  1623. if (!*right) {
  1624. /*
  1625. * Right ended first for partial match or both ended at the same
  1626. * time for a match.
  1627. */
  1628. cmp = 0;
  1629. break;
  1630. }
  1631. cmp = *left - *right;
  1632. if (cmp) {
  1633. break;
  1634. }
  1635. ++left;
  1636. ++right;
  1637. }
  1638. return cmp;
  1639. }
  1640. /*!
  1641. * \internal
  1642. * \brief Comparison of non-pattern extens.
  1643. *
  1644. * \param left Exten to compare.
  1645. * \param right Exten to compare.
  1646. *
  1647. * \retval <0 if left < right
  1648. * \retval =0 if left == right
  1649. * \retval >0 if left > right
  1650. */
  1651. static int ext_cmp_exten(const char *left, const char *right)
  1652. {
  1653. int cmp;
  1654. for (;;) {
  1655. /* Ignore '-' chars as eye candy fluff. */
  1656. while (*left == '-') {
  1657. ++left;
  1658. }
  1659. while (*right == '-') {
  1660. ++right;
  1661. }
  1662. cmp = *left - *right;
  1663. if (cmp) {
  1664. break;
  1665. }
  1666. if (!*left) {
  1667. /*
  1668. * Get here only if both strings ended at the same time. cmp
  1669. * would be non-zero if only one string ended.
  1670. */
  1671. break;
  1672. }
  1673. ++left;
  1674. ++right;
  1675. }
  1676. return cmp;
  1677. }
  1678. /*
  1679. * Special characters used in patterns:
  1680. * '_' underscore is the leading character of a pattern.
  1681. * In other position it is treated as a regular char.
  1682. * '-' The '-' is a separator and ignored. Why? So patterns like NXX-XXX-XXXX work.
  1683. * . one or more of any character. Only allowed at the end of
  1684. * a pattern.
  1685. * ! zero or more of anything. Also impacts the result of CANMATCH
  1686. * and MATCHMORE. Only allowed at the end of a pattern.
  1687. * In the core routine, ! causes a match with a return code of 2.
  1688. * In turn, depending on the search mode: (XXX check if it is implemented)
  1689. * - E_MATCH retuns 1 (does match)
  1690. * - E_MATCHMORE returns 0 (no match)
  1691. * - E_CANMATCH returns 1 (does match)
  1692. *
  1693. * / should not appear as it is considered the separator of the CID info.
  1694. * XXX at the moment we may stop on this char.
  1695. *
  1696. * X Z N match ranges 0-9, 1-9, 2-9 respectively.
  1697. * [ denotes the start of a set of character. Everything inside
  1698. * is considered literally. We can have ranges a-d and individual
  1699. * characters. A '[' and '-' can be considered literally if they
  1700. * are just before ']'.
  1701. * XXX currently there is no way to specify ']' in a range, nor \ is
  1702. * considered specially.
  1703. *
  1704. * When we compare a pattern with a specific extension, all characters in the extension
  1705. * itself are considered literally.
  1706. * XXX do we want to consider space as a separator as well ?
  1707. * XXX do we want to consider the separators in non-patterns as well ?
  1708. */
  1709. /*!
  1710. * \brief helper functions to sort extension patterns in the desired way,
  1711. * so that more specific patterns appear first.
  1712. *
  1713. * \details
  1714. * The function compares individual characters (or sets of), returning
  1715. * an int where bits 0-7 are the ASCII code of the first char in the set,
  1716. * bits 8-15 are the number of characters in the set, and bits 16-20 are
  1717. * for special cases.
  1718. * This way more specific patterns (smaller character sets) appear first.
  1719. * Wildcards have a special value, so that we can directly compare them to
  1720. * sets by subtracting the two values. In particular:
  1721. * 0x001xx one character, character set starting with xx
  1722. * 0x0yyxx yy characters, character set starting with xx
  1723. * 0x18000 '.' (one or more of anything)
  1724. * 0x28000 '!' (zero or more of anything)
  1725. * 0x30000 NUL (end of string)
  1726. * 0x40000 error in set.
  1727. * The pointer to the string is advanced according to needs.
  1728. * NOTES:
  1729. * 1. the empty set is ignored.
  1730. * 2. given that a full set has always 0 as the first element,
  1731. * we could encode the special cases as 0xffXX where XX
  1732. * is 1, 2, 3, 4 as used above.
  1733. */
  1734. static int ext_cmp_pattern_pos(const char **p, unsigned char *bitwise)
  1735. {
  1736. #define BITS_PER 8 /* Number of bits per unit (byte). */
  1737. unsigned char c;
  1738. unsigned char cmin;
  1739. int count;
  1740. const char *end;
  1741. do {
  1742. /* Get character and advance. (Ignore '-' chars as eye candy fluff.) */
  1743. do {
  1744. c = *(*p)++;
  1745. } while (c == '-');
  1746. /* always return unless we have a set of chars */
  1747. switch (c) {
  1748. default:
  1749. /* ordinary character */
  1750. bitwise[c / BITS_PER] = 1 << ((BITS_PER - 1) - (c % BITS_PER));
  1751. return 0x0100 | c;
  1752. case 'n':
  1753. case 'N':
  1754. /* 2..9 */
  1755. bitwise[6] = 0x3f;
  1756. bitwise[7] = 0xc0;
  1757. return 0x0800 | '2';
  1758. case 'x':
  1759. case 'X':
  1760. /* 0..9 */
  1761. bitwise[6] = 0xff;
  1762. bitwise[7] = 0xc0;
  1763. return 0x0A00 | '0';
  1764. case 'z':
  1765. case 'Z':
  1766. /* 1..9 */
  1767. bitwise[6] = 0x7f;
  1768. bitwise[7] = 0xc0;
  1769. return 0x0900 | '1';
  1770. case '.':
  1771. /* wildcard */
  1772. return 0x18000;
  1773. case '!':
  1774. /* earlymatch */
  1775. return 0x28000; /* less specific than '.' */
  1776. case '\0':
  1777. /* empty string */
  1778. *p = NULL;
  1779. return 0x30000;
  1780. case '[':
  1781. /* char set */
  1782. break;
  1783. }
  1784. /* locate end of set */
  1785. end = strchr(*p, ']');
  1786. if (!end) {
  1787. ast_log(LOG_WARNING, "Wrong usage of [] in the extension\n");
  1788. return 0x40000; /* XXX make this entry go last... */
  1789. }
  1790. count = 0;
  1791. cmin = 0xFF;
  1792. for (; *p < end; ++*p) {
  1793. unsigned char c1; /* first char in range */
  1794. unsigned char c2; /* last char in range */
  1795. c1 = (*p)[0];
  1796. if (*p + 2 < end && (*p)[1] == '-') { /* this is a range */
  1797. c2 = (*p)[2];
  1798. *p += 2; /* skip a total of 3 chars */
  1799. } else { /* individual character */
  1800. c2 = c1;
  1801. }
  1802. if (c1 < cmin) {
  1803. cmin = c1;
  1804. }
  1805. for (; c1 <= c2; ++c1) {
  1806. unsigned char mask = 1 << ((BITS_PER - 1) - (c1 % BITS_PER));
  1807. /*
  1808. * Note: If two character sets score the same, the one with the
  1809. * lowest ASCII values will compare as coming first. Must fill
  1810. * in most significant bits for lower ASCII values to accomplish
  1811. * the desired sort order.
  1812. */
  1813. if (!(bitwise[c1 / BITS_PER] & mask)) {
  1814. /* Add the character to the set. */
  1815. bitwise[c1 / BITS_PER] |= mask;
  1816. count += 0x100;
  1817. }
  1818. }
  1819. }
  1820. ++*p;
  1821. } while (!count);/* While the char set was empty. */
  1822. return count | cmin;
  1823. }
  1824. /*!
  1825. * \internal
  1826. * \brief Comparison of exten patterns.
  1827. *
  1828. * \param left Pattern to compare.
  1829. * \param right Pattern to compare.
  1830. *
  1831. * \retval <0 if left < right
  1832. * \retval =0 if left == right
  1833. * \retval >0 if left > right
  1834. */
  1835. static int ext_cmp_pattern(const char *left, const char *right)
  1836. {
  1837. int cmp;
  1838. int left_pos;
  1839. int right_pos;
  1840. for (;;) {
  1841. unsigned char left_bitwise[32] = { 0, };
  1842. unsigned char right_bitwise[32] = { 0, };
  1843. left_pos = ext_cmp_pattern_pos(&left, left_bitwise);
  1844. right_pos = ext_cmp_pattern_pos(&right, right_bitwise);
  1845. cmp = left_pos - right_pos;
  1846. if (!cmp) {
  1847. /*
  1848. * Are the character sets different, even though they score the same?
  1849. *
  1850. * Note: Must swap left and right to get the sense of the
  1851. * comparison correct. Otherwise, we would need to multiply by
  1852. * -1 instead.
  1853. */
  1854. cmp = memcmp(right_bitwise, left_bitwise, ARRAY_LEN(left_bitwise));
  1855. }
  1856. if (cmp) {
  1857. break;
  1858. }
  1859. if (!left) {
  1860. /*
  1861. * Get here only if both patterns ended at the same time. cmp
  1862. * would be non-zero if only one pattern ended.
  1863. */
  1864. break;
  1865. }
  1866. }
  1867. return cmp;
  1868. }
  1869. /*!
  1870. * \internal
  1871. * \brief Comparison of dialplan extens for sorting purposes.
  1872. *
  1873. * \param left Exten/pattern to compare.
  1874. * \param right Exten/pattern to compare.
  1875. *
  1876. * \retval <0 if left < right
  1877. * \retval =0 if left == right
  1878. * \retval >0 if left > right
  1879. */
  1880. static int ext_cmp(const char *left, const char *right)
  1881. {
  1882. /* Make sure non-pattern extens come first. */
  1883. if (left[0] != '_') {
  1884. if (right[0] == '_') {
  1885. return -1;
  1886. }
  1887. /* Compare two non-pattern extens. */
  1888. return ext_cmp_exten(left, right);
  1889. }
  1890. if (right[0] != '_') {
  1891. return 1;
  1892. }
  1893. /*
  1894. * OK, we need full pattern sorting routine.
  1895. *
  1896. * Skip past the underscores
  1897. */
  1898. return ext_cmp_pattern(left + 1, right + 1);
  1899. }
  1900. static int ext_fluff_count(const char *exten)
  1901. {
  1902. int fluff = 0;
  1903. if (*exten != '_') {
  1904. /* not a pattern, simple check. */
  1905. while (*exten) {
  1906. if (*exten == '-') {
  1907. fluff++;
  1908. }
  1909. exten++;
  1910. }
  1911. return fluff;
  1912. }
  1913. /* do pattern check */
  1914. while (*exten) {
  1915. if (*exten == '-') {
  1916. fluff++;
  1917. } else if (*exten == '[') {
  1918. /* skip set, dashes here matter. */
  1919. exten = strchr(exten, ']');
  1920. if (!exten) {
  1921. /* we'll end up warning about this later, don't spam logs */
  1922. return fluff;
  1923. }
  1924. }
  1925. exten++;
  1926. }
  1927. return fluff;
  1928. }
  1929. int ast_extension_cmp(const char *a, const char *b)
  1930. {
  1931. int cmp;
  1932. cmp = ext_cmp(a, b);
  1933. if (cmp < 0) {
  1934. return -1;
  1935. }
  1936. if (cmp > 0) {
  1937. return 1;
  1938. }
  1939. return 0;
  1940. }
  1941. /*!
  1942. * \internal
  1943. * \brief used ast_extension_{match|close}
  1944. * mode is as follows:
  1945. * E_MATCH success only on exact match
  1946. * E_MATCHMORE success only on partial match (i.e. leftover digits in pattern)
  1947. * E_CANMATCH either of the above.
  1948. * \retval 0 on no-match
  1949. * \retval 1 on match
  1950. * \retval 2 on early match.
  1951. */
  1952. static int _extension_match_core(const char *pattern, const char *data, enum ext_match_t mode)
  1953. {
  1954. mode &= E_MATCH_MASK; /* only consider the relevant bits */
  1955. #ifdef NEED_DEBUG_HERE
  1956. ast_log(LOG_NOTICE,"match core: pat: '%s', dat: '%s', mode=%d\n", pattern, data, (int)mode);
  1957. #endif
  1958. if (pattern[0] != '_') { /* not a pattern, try exact or partial match */
  1959. int lp = ext_cmp_exten_strlen(pattern);
  1960. int ld = ext_cmp_exten_strlen(data);
  1961. if (lp < ld) { /* pattern too short, cannot match */
  1962. #ifdef NEED_DEBUG_HERE
  1963. ast_log(LOG_NOTICE,"return (0) - pattern too short, cannot match\n");
  1964. #endif
  1965. return 0;
  1966. }
  1967. /* depending on the mode, accept full or partial match or both */
  1968. if (mode == E_MATCH) {
  1969. #ifdef NEED_DEBUG_HERE
  1970. ast_log(LOG_NOTICE,"return (!ext_cmp_exten(%s,%s) when mode== E_MATCH)\n", pattern, data);
  1971. #endif
  1972. return !ext_cmp_exten(pattern, data); /* 1 on match, 0 on fail */
  1973. }
  1974. if (ld == 0 || !ext_cmp_exten_partial(pattern, data)) { /* partial or full match */
  1975. #ifdef NEED_DEBUG_HERE
  1976. ast_log(LOG_NOTICE,"return (mode(%d) == E_MATCHMORE ? lp(%d) > ld(%d) : 1)\n", mode, lp, ld);
  1977. #endif
  1978. return (mode == E_MATCHMORE) ? lp > ld : 1; /* XXX should consider '!' and '/' ? */
  1979. } else {
  1980. #ifdef NEED_DEBUG_HERE
  1981. ast_log(LOG_NOTICE,"return (0) when ld(%d) > 0 && pattern(%s) != data(%s)\n", ld, pattern, data);
  1982. #endif
  1983. return 0;
  1984. }
  1985. }
  1986. if (mode == E_MATCH && data[0] == '_') {
  1987. /*
  1988. * XXX It is bad design that we don't know if we should be
  1989. * comparing data and pattern as patterns or comparing data if
  1990. * it conforms to pattern when the function is called. First,
  1991. * assume they are both patterns. If they don't match then try
  1992. * to see if data conforms to the given pattern.
  1993. *
  1994. * note: if this test is left out, then _x. will not match _x. !!!
  1995. */
  1996. #ifdef NEED_DEBUG_HERE
  1997. ast_log(LOG_NOTICE, "Comparing as patterns first. pattern:%s data:%s\n", pattern, data);
  1998. #endif
  1999. if (!ext_cmp_pattern(pattern + 1, data + 1)) {
  2000. #ifdef NEED_DEBUG_HERE
  2001. ast_log(LOG_NOTICE,"return (1) - pattern matches pattern\n");
  2002. #endif
  2003. return 1;
  2004. }
  2005. }
  2006. ++pattern; /* skip leading _ */
  2007. /*
  2008. * XXX below we stop at '/' which is a separator for the CID info. However we should
  2009. * not store '/' in the pattern at all. When we insure it, we can remove the checks.
  2010. */
  2011. for (;;) {
  2012. const char *end;
  2013. /* Ignore '-' chars as eye candy fluff. */
  2014. while (*data == '-') {
  2015. ++data;
  2016. }
  2017. while (*pattern == '-') {
  2018. ++pattern;
  2019. }
  2020. if (!*data || !*pattern || *pattern == '/') {
  2021. break;
  2022. }
  2023. switch (*pattern) {
  2024. case '[': /* a range */
  2025. ++pattern;
  2026. end = strchr(pattern, ']'); /* XXX should deal with escapes ? */
  2027. if (!end) {
  2028. ast_log(LOG_WARNING, "Wrong usage of [] in the extension\n");
  2029. return 0; /* unconditional failure */
  2030. }
  2031. if (pattern == end) {
  2032. /* Ignore empty character sets. */
  2033. ++pattern;
  2034. continue;
  2035. }
  2036. for (; pattern < end; ++pattern) {
  2037. if (pattern+2 < end && pattern[1] == '-') { /* this is a range */
  2038. if (*data >= pattern[0] && *data <= pattern[2])
  2039. break; /* match found */
  2040. else {
  2041. pattern += 2; /* skip a total of 3 chars */
  2042. continue;
  2043. }
  2044. } else if (*data == pattern[0])
  2045. break; /* match found */
  2046. }
  2047. if (pattern >= end) {
  2048. #ifdef NEED_DEBUG_HERE
  2049. ast_log(LOG_NOTICE,"return (0) when pattern>=end\n");
  2050. #endif
  2051. return 0;
  2052. }
  2053. pattern = end; /* skip and continue */
  2054. break;
  2055. case 'n':
  2056. case 'N':
  2057. if (*data < '2' || *data > '9') {
  2058. #ifdef NEED_DEBUG_HERE
  2059. ast_log(LOG_NOTICE,"return (0) N is not matched\n");
  2060. #endif
  2061. return 0;
  2062. }
  2063. break;
  2064. case 'x':
  2065. case 'X':
  2066. if (*data < '0' || *data > '9') {
  2067. #ifdef NEED_DEBUG_HERE
  2068. ast_log(LOG_NOTICE,"return (0) X is not matched\n");
  2069. #endif
  2070. return 0;
  2071. }
  2072. break;
  2073. case 'z':
  2074. case 'Z':
  2075. if (*data < '1' || *data > '9') {
  2076. #ifdef NEED_DEBUG_HERE
  2077. ast_log(LOG_NOTICE,"return (0) Z is not matched\n");
  2078. #endif
  2079. return 0;
  2080. }
  2081. break;
  2082. case '.': /* Must match, even with more digits */
  2083. #ifdef NEED_DEBUG_HERE
  2084. ast_log(LOG_NOTICE, "return (1) when '.' is matched\n");
  2085. #endif
  2086. return 1;
  2087. case '!': /* Early match */
  2088. #ifdef NEED_DEBUG_HERE
  2089. ast_log(LOG_NOTICE, "return (2) when '!' is matched\n");
  2090. #endif
  2091. return 2;
  2092. default:
  2093. if (*data != *pattern) {
  2094. #ifdef NEED_DEBUG_HERE
  2095. ast_log(LOG_NOTICE, "return (0) when *data(%c) != *pattern(%c)\n", *data, *pattern);
  2096. #endif
  2097. return 0;
  2098. }
  2099. break;
  2100. }
  2101. ++data;
  2102. ++pattern;
  2103. }
  2104. if (*data) /* data longer than pattern, no match */ {
  2105. #ifdef NEED_DEBUG_HERE
  2106. ast_log(LOG_NOTICE, "return (0) when data longer than pattern\n");
  2107. #endif
  2108. return 0;
  2109. }
  2110. /*
  2111. * match so far, but ran off the end of data.
  2112. * Depending on what is next, determine match or not.
  2113. */
  2114. if (*pattern == '\0' || *pattern == '/') { /* exact match */
  2115. #ifdef NEED_DEBUG_HERE
  2116. ast_log(LOG_NOTICE, "at end, return (%d) in 'exact match'\n", (mode==E_MATCHMORE) ? 0 : 1);
  2117. #endif
  2118. return (mode == E_MATCHMORE) ? 0 : 1; /* this is a failure for E_MATCHMORE */
  2119. } else if (*pattern == '!') { /* early match */
  2120. #ifdef NEED_DEBUG_HERE
  2121. ast_log(LOG_NOTICE, "at end, return (2) when '!' is matched\n");
  2122. #endif
  2123. return 2;
  2124. } else { /* partial match */
  2125. #ifdef NEED_DEBUG_HERE
  2126. ast_log(LOG_NOTICE, "at end, return (%d) which deps on E_MATCH\n", (mode == E_MATCH) ? 0 : 1);
  2127. #endif
  2128. return (mode == E_MATCH) ? 0 : 1; /* this is a failure for E_MATCH */
  2129. }
  2130. }
  2131. /*
  2132. * Wrapper around _extension_match_core() to do performance measurement
  2133. * using the profiling code.
  2134. */
  2135. static int extension_match_core(const char *pattern, const char *data, enum ext_match_t mode)
  2136. {
  2137. int i;
  2138. static int prof_id = -2; /* marker for 'unallocated' id */
  2139. if (prof_id == -2) {
  2140. prof_id = ast_add_profile("ext_match", 0);
  2141. }
  2142. ast_mark(prof_id, 1);
  2143. i = _extension_match_core(ast_strlen_zero(pattern) ? "" : pattern, ast_strlen_zero(data) ? "" : data, mode);
  2144. ast_mark(prof_id, 0);
  2145. return i;
  2146. }
  2147. int ast_extension_match(const char *pattern, const char *data)
  2148. {
  2149. return extension_match_core(pattern, data, E_MATCH);
  2150. }
  2151. int ast_extension_close(const char *pattern, const char *data, int needmore)
  2152. {
  2153. if (needmore != E_MATCHMORE && needmore != E_CANMATCH)
  2154. ast_log(LOG_WARNING, "invalid argument %d\n", needmore);
  2155. return extension_match_core(pattern, data, needmore);
  2156. }
  2157. struct fake_context /* this struct is purely for matching in the hashtab */
  2158. {
  2159. ast_rwlock_t lock;
  2160. struct ast_exten *root;
  2161. struct ast_hashtab *root_table;
  2162. struct match_char *pattern_tree;
  2163. struct ast_context *next;
  2164. struct ast_include *includes;
  2165. struct ast_ignorepat *ignorepats;
  2166. const char *registrar;
  2167. int refcount;
  2168. AST_LIST_HEAD_NOLOCK(, ast_sw) alts;
  2169. ast_mutex_t macrolock;
  2170. char name[256];
  2171. };
  2172. struct ast_context *ast_context_find(const char *name)
  2173. {
  2174. struct ast_context *tmp;
  2175. struct fake_context item;
  2176. if (!name) {
  2177. return NULL;
  2178. }
  2179. ast_rdlock_contexts();
  2180. if (contexts_table) {
  2181. ast_copy_string(item.name, name, sizeof(item.name));
  2182. tmp = ast_hashtab_lookup(contexts_table, &item);
  2183. } else {
  2184. tmp = NULL;
  2185. while ((tmp = ast_walk_contexts(tmp))) {
  2186. if (!strcasecmp(name, tmp->name)) {
  2187. break;
  2188. }
  2189. }
  2190. }
  2191. ast_unlock_contexts();
  2192. return tmp;
  2193. }
  2194. #define STATUS_NO_CONTEXT 1
  2195. #define STATUS_NO_EXTENSION 2
  2196. #define STATUS_NO_PRIORITY 3
  2197. #define STATUS_NO_LABEL 4
  2198. #define STATUS_SUCCESS 5
  2199. static int matchcid(const char *cidpattern, const char *callerid)
  2200. {
  2201. /* If the Caller*ID pattern is empty, then we're matching NO Caller*ID, so
  2202. failing to get a number should count as a match, otherwise not */
  2203. if (ast_strlen_zero(callerid)) {
  2204. return ast_strlen_zero(cidpattern) ? 1 : 0;
  2205. }
  2206. return ast_extension_match(cidpattern, callerid);
  2207. }
  2208. struct ast_exten *pbx_find_extension(struct ast_channel *chan,
  2209. struct ast_context *bypass, struct pbx_find_info *q,
  2210. const char *context, const char *exten, int priority,
  2211. const char *label, const char *callerid, enum ext_match_t action)
  2212. {
  2213. int x, res;
  2214. struct ast_context *tmp = NULL;
  2215. struct ast_exten *e = NULL, *eroot = NULL;
  2216. struct ast_include *i = NULL;
  2217. struct ast_sw *sw = NULL;
  2218. struct ast_exten pattern = {NULL, };
  2219. struct scoreboard score = {0, };
  2220. struct ast_str *tmpdata = NULL;
  2221. pattern.label = label;
  2222. pattern.priority = priority;
  2223. #ifdef NEED_DEBUG_HERE
  2224. ast_log(LOG_NOTICE, "Looking for cont/ext/prio/label/action = %s/%s/%d/%s/%d\n", context, exten, priority, label, (int) action);
  2225. #endif
  2226. /* Initialize status if appropriate */
  2227. if (q->stacklen == 0) {
  2228. q->status = STATUS_NO_CONTEXT;
  2229. q->swo = NULL;
  2230. q->data = NULL;
  2231. q->foundcontext = NULL;
  2232. } else if (q->stacklen >= AST_PBX_MAX_STACK) {
  2233. ast_log(LOG_WARNING, "Maximum PBX stack exceeded\n");
  2234. return NULL;
  2235. }
  2236. /* Check first to see if we've already been checked */
  2237. for (x = 0; x < q->stacklen; x++) {
  2238. if (!strcasecmp(q->incstack[x], context))
  2239. return NULL;
  2240. }
  2241. if (bypass) { /* bypass means we only look there */
  2242. tmp = bypass;
  2243. } else { /* look in contexts */
  2244. tmp = find_context(context);
  2245. if (!tmp) {
  2246. return NULL;
  2247. }
  2248. }
  2249. if (q->status < STATUS_NO_EXTENSION)
  2250. q->status = STATUS_NO_EXTENSION;
  2251. /* Do a search for matching extension */
  2252. eroot = NULL;
  2253. score.total_specificity = 0;
  2254. score.exten = 0;
  2255. score.total_length = 0;
  2256. if (!tmp->pattern_tree && tmp->root_table) {
  2257. create_match_char_tree(tmp);
  2258. #ifdef NEED_DEBUG
  2259. ast_debug(1, "Tree Created in context %s:\n", context);
  2260. log_match_char_tree(tmp->pattern_tree," ");
  2261. #endif
  2262. }
  2263. #ifdef NEED_DEBUG
  2264. ast_log(LOG_NOTICE, "The Trie we are searching in:\n");
  2265. log_match_char_tree(tmp->pattern_tree, ":: ");
  2266. #endif
  2267. do {
  2268. if (!ast_strlen_zero(overrideswitch)) {
  2269. char *osw = ast_strdupa(overrideswitch), *name;
  2270. struct ast_switch *asw;
  2271. ast_switch_f *aswf = NULL;
  2272. char *datap;
  2273. int eval = 0;
  2274. name = strsep(&osw, "/");
  2275. asw = pbx_findswitch(name);
  2276. if (!asw) {
  2277. ast_log(LOG_WARNING, "No such switch '%s'\n", name);
  2278. break;
  2279. }
  2280. if (osw && strchr(osw, '$')) {
  2281. eval = 1;
  2282. }
  2283. if (eval && !(tmpdata = ast_str_thread_get(&switch_data, 512))) {
  2284. ast_log(LOG_WARNING, "Can't evaluate overrideswitch?!\n");
  2285. break;
  2286. } else if (eval) {
  2287. /* Substitute variables now */
  2288. pbx_substitute_variables_helper(chan, osw, ast_str_buffer(tmpdata), ast_str_size(tmpdata));
  2289. datap = ast_str_buffer(tmpdata);
  2290. } else {
  2291. datap = osw;
  2292. }
  2293. /* equivalent of extension_match_core() at the switch level */
  2294. if (action == E_CANMATCH)
  2295. aswf = asw->canmatch;
  2296. else if (action == E_MATCHMORE)
  2297. aswf = asw->matchmore;
  2298. else /* action == E_MATCH */
  2299. aswf = asw->exists;
  2300. if (!aswf) {
  2301. res = 0;
  2302. } else {
  2303. if (chan) {
  2304. ast_autoservice_start(chan);
  2305. }
  2306. res = aswf(chan, context, exten, priority, callerid, datap);
  2307. if (chan) {
  2308. ast_autoservice_stop(chan);
  2309. }
  2310. }
  2311. if (res) { /* Got a match */
  2312. q->swo = asw;
  2313. q->data = datap;
  2314. q->foundcontext = context;
  2315. /* XXX keep status = STATUS_NO_CONTEXT ? */
  2316. return NULL;
  2317. }
  2318. }
  2319. } while (0);
  2320. if (extenpatternmatchnew) {
  2321. new_find_extension(exten, &score, tmp->pattern_tree, 0, 0, callerid, label, action);
  2322. eroot = score.exten;
  2323. if (score.last_char == '!' && action == E_MATCHMORE) {
  2324. /* We match an extension ending in '!'.
  2325. * The decision in this case is final and is NULL (no match).
  2326. */
  2327. #ifdef NEED_DEBUG_HERE
  2328. ast_log(LOG_NOTICE,"Returning MATCHMORE NULL with exclamation point.\n");
  2329. #endif
  2330. return NULL;
  2331. }
  2332. if (!eroot && (action == E_CANMATCH || action == E_MATCHMORE) && score.canmatch_exten) {
  2333. q->status = STATUS_SUCCESS;
  2334. #ifdef NEED_DEBUG_HERE
  2335. ast_log(LOG_NOTICE,"Returning CANMATCH exten %s\n", score.canmatch_exten->exten);
  2336. #endif
  2337. return score.canmatch_exten;
  2338. }
  2339. if ((action == E_MATCHMORE || action == E_CANMATCH) && eroot) {
  2340. if (score.node) {
  2341. struct ast_exten *z = trie_find_next_match(score.node);
  2342. if (z) {
  2343. #ifdef NEED_DEBUG_HERE
  2344. ast_log(LOG_NOTICE,"Returning CANMATCH/MATCHMORE next_match exten %s\n", z->exten);
  2345. #endif
  2346. } else {
  2347. if (score.canmatch_exten) {
  2348. #ifdef NEED_DEBUG_HERE
  2349. ast_log(LOG_NOTICE,"Returning CANMATCH/MATCHMORE canmatchmatch exten %s(%p)\n", score.canmatch_exten->exten, score.canmatch_exten);
  2350. #endif
  2351. return score.canmatch_exten;
  2352. } else {
  2353. #ifdef NEED_DEBUG_HERE
  2354. ast_log(LOG_NOTICE,"Returning CANMATCH/MATCHMORE next_match exten NULL\n");
  2355. #endif
  2356. }
  2357. }
  2358. return z;
  2359. }
  2360. #ifdef NEED_DEBUG_HERE
  2361. ast_log(LOG_NOTICE, "Returning CANMATCH/MATCHMORE NULL (no next_match)\n");
  2362. #endif
  2363. return NULL; /* according to the code, complete matches are null matches in MATCHMORE mode */
  2364. }
  2365. if (eroot) {
  2366. /* found entry, now look for the right priority */
  2367. if (q->status < STATUS_NO_PRIORITY)
  2368. q->status = STATUS_NO_PRIORITY;
  2369. e = NULL;
  2370. if (action == E_FINDLABEL && label ) {
  2371. if (q->status < STATUS_NO_LABEL)
  2372. q->status = STATUS_NO_LABEL;
  2373. e = ast_hashtab_lookup(eroot->peer_label_table, &pattern);
  2374. } else {
  2375. e = ast_hashtab_lookup(eroot->peer_table, &pattern);
  2376. }
  2377. if (e) { /* found a valid match */
  2378. q->status = STATUS_SUCCESS;
  2379. q->foundcontext = context;
  2380. #ifdef NEED_DEBUG_HERE
  2381. ast_log(LOG_NOTICE,"Returning complete match of exten %s\n", e->exten);
  2382. #endif
  2383. return e;
  2384. }
  2385. }
  2386. } else { /* the old/current default exten pattern match algorithm */
  2387. /* scan the list trying to match extension and CID */
  2388. eroot = NULL;
  2389. while ( (eroot = ast_walk_context_extensions(tmp, eroot)) ) {
  2390. int match = extension_match_core(eroot->exten, exten, action);
  2391. /* 0 on fail, 1 on match, 2 on earlymatch */
  2392. if (!match || (eroot->matchcid && !matchcid(eroot->cidmatch, callerid)))
  2393. continue; /* keep trying */
  2394. if (match == 2 && action == E_MATCHMORE) {
  2395. /* We match an extension ending in '!'.
  2396. * The decision in this case is final and is NULL (no match).
  2397. */
  2398. return NULL;
  2399. }
  2400. /* found entry, now look for the right priority */
  2401. if (q->status < STATUS_NO_PRIORITY)
  2402. q->status = STATUS_NO_PRIORITY;
  2403. e = NULL;
  2404. if (action == E_FINDLABEL && label ) {
  2405. if (q->status < STATUS_NO_LABEL)
  2406. q->status = STATUS_NO_LABEL;
  2407. e = ast_hashtab_lookup(eroot->peer_label_table, &pattern);
  2408. } else {
  2409. e = ast_hashtab_lookup(eroot->peer_table, &pattern);
  2410. }
  2411. if (e) { /* found a valid match */
  2412. q->status = STATUS_SUCCESS;
  2413. q->foundcontext = context;
  2414. return e;
  2415. }
  2416. }
  2417. }
  2418. /* Check alternative switches */
  2419. AST_LIST_TRAVERSE(&tmp->alts, sw, list) {
  2420. struct ast_switch *asw = pbx_findswitch(sw->name);
  2421. ast_switch_f *aswf = NULL;
  2422. char *datap;
  2423. if (!asw) {
  2424. ast_log(LOG_WARNING, "No such switch '%s'\n", sw->name);
  2425. continue;
  2426. }
  2427. /* Substitute variables now */
  2428. if (sw->eval) {
  2429. if (!(tmpdata = ast_str_thread_get(&switch_data, 512))) {
  2430. ast_log(LOG_WARNING, "Can't evaluate switch?!\n");
  2431. continue;
  2432. }
  2433. pbx_substitute_variables_helper(chan, sw->data, ast_str_buffer(tmpdata), ast_str_size(tmpdata));
  2434. }
  2435. /* equivalent of extension_match_core() at the switch level */
  2436. if (action == E_CANMATCH)
  2437. aswf = asw->canmatch;
  2438. else if (action == E_MATCHMORE)
  2439. aswf = asw->matchmore;
  2440. else /* action == E_MATCH */
  2441. aswf = asw->exists;
  2442. datap = sw->eval ? ast_str_buffer(tmpdata) : sw->data;
  2443. if (!aswf)
  2444. res = 0;
  2445. else {
  2446. if (chan)
  2447. ast_autoservice_start(chan);
  2448. res = aswf(chan, context, exten, priority, callerid, datap);
  2449. if (chan)
  2450. ast_autoservice_stop(chan);
  2451. }
  2452. if (res) { /* Got a match */
  2453. q->swo = asw;
  2454. q->data = datap;
  2455. q->foundcontext = context;
  2456. /* XXX keep status = STATUS_NO_CONTEXT ? */
  2457. return NULL;
  2458. }
  2459. }
  2460. q->incstack[q->stacklen++] = tmp->name; /* Setup the stack */
  2461. /* Now try any includes we have in this context */
  2462. for (i = tmp->includes; i; i = i->next) {
  2463. if (include_valid(i)) {
  2464. if ((e = pbx_find_extension(chan, bypass, q, i->rname, exten, priority, label, callerid, action))) {
  2465. #ifdef NEED_DEBUG_HERE
  2466. ast_log(LOG_NOTICE,"Returning recursive match of %s\n", e->exten);
  2467. #endif
  2468. return e;
  2469. }
  2470. if (q->swo)
  2471. return NULL;
  2472. }
  2473. }
  2474. return NULL;
  2475. }
  2476. static void exception_store_free(void *data)
  2477. {
  2478. struct pbx_exception *exception = data;
  2479. ast_string_field_free_memory(exception);
  2480. ast_free(exception);
  2481. }
  2482. static const struct ast_datastore_info exception_store_info = {
  2483. .type = "EXCEPTION",
  2484. .destroy = exception_store_free,
  2485. };
  2486. /*!
  2487. * \internal
  2488. * \brief Set the PBX to execute the exception extension.
  2489. *
  2490. * \param chan Channel to raise the exception on.
  2491. * \param reason Reason exception is raised.
  2492. * \param priority Dialplan priority to set.
  2493. *
  2494. * \retval 0 on success.
  2495. * \retval -1 on error.
  2496. */
  2497. int raise_exception(struct ast_channel *chan, const char *reason, int priority)
  2498. {
  2499. struct ast_datastore *ds = ast_channel_datastore_find(chan, &exception_store_info, NULL);
  2500. struct pbx_exception *exception = NULL;
  2501. if (!ds) {
  2502. ds = ast_datastore_alloc(&exception_store_info, NULL);
  2503. if (!ds)
  2504. return -1;
  2505. if (!(exception = ast_calloc_with_stringfields(1, struct pbx_exception, 128))) {
  2506. ast_datastore_free(ds);
  2507. return -1;
  2508. }
  2509. ds->data = exception;
  2510. ast_channel_datastore_add(chan, ds);
  2511. } else
  2512. exception = ds->data;
  2513. ast_string_field_set(exception, reason, reason);
  2514. ast_string_field_set(exception, context, ast_channel_context(chan));
  2515. ast_string_field_set(exception, exten, ast_channel_exten(chan));
  2516. exception->priority = ast_channel_priority(chan);
  2517. set_ext_pri(chan, "e", priority);
  2518. return 0;
  2519. }
  2520. static int acf_exception_read(struct ast_channel *chan, const char *name, char *data, char *buf, size_t buflen)
  2521. {
  2522. struct ast_datastore *ds = ast_channel_datastore_find(chan, &exception_store_info, NULL);
  2523. struct pbx_exception *exception = NULL;
  2524. if (!ds || !ds->data)
  2525. return -1;
  2526. exception = ds->data;
  2527. if (!strcasecmp(data, "REASON"))
  2528. ast_copy_string(buf, exception->reason, buflen);
  2529. else if (!strcasecmp(data, "CONTEXT"))
  2530. ast_copy_string(buf, exception->context, buflen);
  2531. else if (!strncasecmp(data, "EXTEN", 5))
  2532. ast_copy_string(buf, exception->exten, buflen);
  2533. else if (!strcasecmp(data, "PRIORITY"))
  2534. snprintf(buf, buflen, "%d", exception->priority);
  2535. else
  2536. return -1;
  2537. return 0;
  2538. }
  2539. static struct ast_custom_function exception_function = {
  2540. .name = "EXCEPTION",
  2541. .read = acf_exception_read,
  2542. };
  2543. /*!
  2544. * \brief The return value depends on the action:
  2545. *
  2546. * E_MATCH, E_CANMATCH, E_MATCHMORE require a real match,
  2547. * and return 0 on failure, -1 on match;
  2548. * E_FINDLABEL maps the label to a priority, and returns
  2549. * the priority on success, ... XXX
  2550. * E_SPAWN, spawn an application,
  2551. *
  2552. * \retval 0 on success.
  2553. * \retval -1 on failure.
  2554. *
  2555. * \note The channel is auto-serviced in this function, because doing an extension
  2556. * match may block for a long time. For example, if the lookup has to use a network
  2557. * dialplan switch, such as DUNDi or IAX2, it may take a while. However, the channel
  2558. * auto-service code will queue up any important signalling frames to be processed
  2559. * after this is done.
  2560. */
  2561. static int pbx_extension_helper(struct ast_channel *c, struct ast_context *con,
  2562. const char *context, const char *exten, int priority,
  2563. const char *label, const char *callerid, enum ext_match_t action, int *found, int combined_find_spawn)
  2564. {
  2565. struct ast_exten *e;
  2566. struct ast_app *app;
  2567. char *substitute = NULL;
  2568. struct pbx_find_info q = { .stacklen = 0 }; /* the rest is reset in pbx_find_extension */
  2569. char passdata[EXT_DATA_SIZE];
  2570. int matching_action = (action == E_MATCH || action == E_CANMATCH || action == E_MATCHMORE);
  2571. ast_rdlock_contexts();
  2572. if (found)
  2573. *found = 0;
  2574. e = pbx_find_extension(c, con, &q, context, exten, priority, label, callerid, action);
  2575. if (e) {
  2576. if (found)
  2577. *found = 1;
  2578. if (matching_action) {
  2579. ast_unlock_contexts();
  2580. return -1; /* success, we found it */
  2581. } else if (action == E_FINDLABEL) { /* map the label to a priority */
  2582. int res = e->priority;
  2583. ast_unlock_contexts();
  2584. /* the priority we were looking for */
  2585. return res;
  2586. } else { /* spawn */
  2587. if (!e->cached_app)
  2588. e->cached_app = pbx_findapp(e->app);
  2589. app = e->cached_app;
  2590. if (ast_strlen_zero(e->data)) {
  2591. *passdata = '\0';
  2592. } else {
  2593. const char *tmp;
  2594. if ((!(tmp = strchr(e->data, '$'))) || (!strstr(tmp, "${") && !strstr(tmp, "$["))) {
  2595. /* no variables to substitute, copy on through */
  2596. ast_copy_string(passdata, e->data, sizeof(passdata));
  2597. } else {
  2598. /* save e->data on stack for later processing after lock released */
  2599. substitute = ast_strdupa(e->data);
  2600. }
  2601. }
  2602. ast_unlock_contexts();
  2603. if (!app) {
  2604. ast_log(LOG_WARNING, "No application '%s' for extension (%s, %s, %d)\n", e->app, context, exten, priority);
  2605. return -1;
  2606. }
  2607. if (ast_channel_context(c) != context)
  2608. ast_channel_context_set(c, context);
  2609. if (ast_channel_exten(c) != exten)
  2610. ast_channel_exten_set(c, exten);
  2611. ast_channel_priority_set(c, priority);
  2612. if (substitute) {
  2613. pbx_substitute_variables_helper(c, substitute, passdata, sizeof(passdata)-1);
  2614. }
  2615. ast_debug(1, "Launching '%s'\n", app_name(app));
  2616. if (VERBOSITY_ATLEAST(3)) {
  2617. ast_verb(3, "Executing [%s@%s:%d] " COLORIZE_FMT "(\"" COLORIZE_FMT "\", \"" COLORIZE_FMT "\") %s\n",
  2618. exten, context, priority,
  2619. COLORIZE(COLOR_BRCYAN, 0, app_name(app)),
  2620. COLORIZE(COLOR_BRMAGENTA, 0, ast_channel_name(c)),
  2621. COLORIZE(COLOR_BRMAGENTA, 0, passdata),
  2622. "in new stack");
  2623. }
  2624. return pbx_exec(c, app, passdata); /* 0 on success, -1 on failure */
  2625. }
  2626. } else if (q.swo) { /* not found here, but in another switch */
  2627. if (found)
  2628. *found = 1;
  2629. ast_unlock_contexts();
  2630. if (matching_action) {
  2631. return -1;
  2632. } else {
  2633. if (!q.swo->exec) {
  2634. ast_log(LOG_WARNING, "No execution engine for switch %s\n", q.swo->name);
  2635. return -1;
  2636. }
  2637. return q.swo->exec(c, q.foundcontext ? q.foundcontext : context, exten, priority, callerid, q.data);
  2638. }
  2639. } else { /* not found anywhere, see what happened */
  2640. ast_unlock_contexts();
  2641. /* Using S_OR here because Solaris doesn't like NULL being passed to ast_log */
  2642. switch (q.status) {
  2643. case STATUS_NO_CONTEXT:
  2644. if (!matching_action && !combined_find_spawn)
  2645. ast_log(LOG_NOTICE, "Cannot find extension context '%s'\n", S_OR(context, ""));
  2646. break;
  2647. case STATUS_NO_EXTENSION:
  2648. if (!matching_action && !combined_find_spawn)
  2649. ast_log(LOG_NOTICE, "Cannot find extension '%s' in context '%s'\n", exten, S_OR(context, ""));
  2650. break;
  2651. case STATUS_NO_PRIORITY:
  2652. if (!matching_action && !combined_find_spawn)
  2653. ast_log(LOG_NOTICE, "No such priority %d in extension '%s' in context '%s'\n", priority, exten, S_OR(context, ""));
  2654. break;
  2655. case STATUS_NO_LABEL:
  2656. if (context && !combined_find_spawn)
  2657. ast_log(LOG_NOTICE, "No such label '%s' in extension '%s' in context '%s'\n", label, exten, S_OR(context, ""));
  2658. break;
  2659. default:
  2660. ast_debug(1, "Shouldn't happen!\n");
  2661. }
  2662. return (matching_action) ? 0 : -1;
  2663. }
  2664. }
  2665. /*! \brief Find hint for given extension in context */
  2666. static struct ast_exten *ast_hint_extension_nolock(struct ast_channel *c, const char *context, const char *exten)
  2667. {
  2668. struct pbx_find_info q = { .stacklen = 0 }; /* the rest is set in pbx_find_context */
  2669. return pbx_find_extension(c, NULL, &q, context, exten, PRIORITY_HINT, NULL, "", E_MATCH);
  2670. }
  2671. static struct ast_exten *ast_hint_extension(struct ast_channel *c, const char *context, const char *exten)
  2672. {
  2673. struct ast_exten *e;
  2674. ast_rdlock_contexts();
  2675. e = ast_hint_extension_nolock(c, context, exten);
  2676. ast_unlock_contexts();
  2677. return e;
  2678. }
  2679. enum ast_extension_states ast_devstate_to_extenstate(enum ast_device_state devstate)
  2680. {
  2681. switch (devstate) {
  2682. case AST_DEVICE_ONHOLD:
  2683. return AST_EXTENSION_ONHOLD;
  2684. case AST_DEVICE_BUSY:
  2685. return AST_EXTENSION_BUSY;
  2686. case AST_DEVICE_UNKNOWN:
  2687. return AST_EXTENSION_NOT_INUSE;
  2688. case AST_DEVICE_UNAVAILABLE:
  2689. case AST_DEVICE_INVALID:
  2690. return AST_EXTENSION_UNAVAILABLE;
  2691. case AST_DEVICE_RINGINUSE:
  2692. return (AST_EXTENSION_INUSE | AST_EXTENSION_RINGING);
  2693. case AST_DEVICE_RINGING:
  2694. return AST_EXTENSION_RINGING;
  2695. case AST_DEVICE_INUSE:
  2696. return AST_EXTENSION_INUSE;
  2697. case AST_DEVICE_NOT_INUSE:
  2698. return AST_EXTENSION_NOT_INUSE;
  2699. case AST_DEVICE_TOTAL: /* not a device state, included for completeness */
  2700. break;
  2701. }
  2702. return AST_EXTENSION_NOT_INUSE;
  2703. }
  2704. /*!
  2705. * \internal
  2706. * \brief Parse out the presence portion of the hint string
  2707. */
  2708. static char *parse_hint_presence(struct ast_str *hint_args)
  2709. {
  2710. char *copy = ast_strdupa(ast_str_buffer(hint_args));
  2711. char *tmp = "";
  2712. if ((tmp = strrchr(copy, ','))) {
  2713. *tmp = '\0';
  2714. tmp++;
  2715. } else {
  2716. return NULL;
  2717. }
  2718. ast_str_set(&hint_args, 0, "%s", tmp);
  2719. return ast_str_buffer(hint_args);
  2720. }
  2721. /*!
  2722. * \internal
  2723. * \brief Parse out the device portion of the hint string
  2724. */
  2725. static char *parse_hint_device(struct ast_str *hint_args)
  2726. {
  2727. char *copy = ast_strdupa(ast_str_buffer(hint_args));
  2728. char *tmp;
  2729. if ((tmp = strrchr(copy, ','))) {
  2730. *tmp = '\0';
  2731. }
  2732. ast_str_set(&hint_args, 0, "%s", copy);
  2733. return ast_str_buffer(hint_args);
  2734. }
  2735. static void device_state_info_dt(void *obj)
  2736. {
  2737. struct ast_device_state_info *info = obj;
  2738. ao2_cleanup(info->causing_channel);
  2739. }
  2740. static struct ao2_container *alloc_device_state_info(void)
  2741. {
  2742. return ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL);
  2743. }
  2744. static int ast_extension_state3(struct ast_str *hint_app, struct ao2_container *device_state_info)
  2745. {
  2746. char *cur;
  2747. char *rest;
  2748. struct ast_devstate_aggregate agg;
  2749. /* One or more devices separated with a & character */
  2750. rest = parse_hint_device(hint_app);
  2751. ast_devstate_aggregate_init(&agg);
  2752. while ((cur = strsep(&rest, "&"))) {
  2753. enum ast_device_state state = ast_device_state(cur);
  2754. ast_devstate_aggregate_add(&agg, state);
  2755. if (device_state_info) {
  2756. struct ast_device_state_info *obj;
  2757. obj = ao2_alloc_options(sizeof(*obj) + strlen(cur), device_state_info_dt, AO2_ALLOC_OPT_LOCK_NOLOCK);
  2758. /* if failed we cannot add this device */
  2759. if (obj) {
  2760. obj->device_state = state;
  2761. strcpy(obj->device_name, cur);
  2762. ao2_link(device_state_info, obj);
  2763. ao2_ref(obj, -1);
  2764. }
  2765. }
  2766. }
  2767. return ast_devstate_to_extenstate(ast_devstate_aggregate_result(&agg));
  2768. }
  2769. /*! \brief Check state of extension by using hints */
  2770. static int ast_extension_state2(struct ast_exten *e, struct ao2_container *device_state_info)
  2771. {
  2772. struct ast_str *hint_app = ast_str_thread_get(&extensionstate_buf, 32);
  2773. if (!e || !hint_app) {
  2774. return -1;
  2775. }
  2776. ast_str_set(&hint_app, 0, "%s", ast_get_extension_app(e));
  2777. return ast_extension_state3(hint_app, device_state_info);
  2778. }
  2779. /*! \brief Return extension_state as string */
  2780. const char *ast_extension_state2str(int extension_state)
  2781. {
  2782. int i;
  2783. for (i = 0; (i < ARRAY_LEN(extension_states)); i++) {
  2784. if (extension_states[i].extension_state == extension_state)
  2785. return extension_states[i].text;
  2786. }
  2787. return "Unknown";
  2788. }
  2789. /*!
  2790. * \internal
  2791. * \brief Check extension state for an extension by using hint
  2792. */
  2793. static int internal_extension_state_extended(struct ast_channel *c, const char *context, const char *exten,
  2794. struct ao2_container *device_state_info)
  2795. {
  2796. struct ast_exten *e;
  2797. if (!(e = ast_hint_extension(c, context, exten))) { /* Do we have a hint for this extension ? */
  2798. return -1; /* No hint, return -1 */
  2799. }
  2800. if (e->exten[0] == '_') {
  2801. /* Create this hint on-the-fly */
  2802. ast_add_extension(e->parent->name, 0, exten, e->priority, e->label,
  2803. e->matchcid ? e->cidmatch : NULL, e->app, ast_strdup(e->data), ast_free_ptr,
  2804. e->registrar);
  2805. if (!(e = ast_hint_extension(c, context, exten))) {
  2806. /* Improbable, but not impossible */
  2807. return -1;
  2808. }
  2809. }
  2810. return ast_extension_state2(e, device_state_info); /* Check all devices in the hint */
  2811. }
  2812. /*! \brief Check extension state for an extension by using hint */
  2813. int ast_extension_state(struct ast_channel *c, const char *context, const char *exten)
  2814. {
  2815. return internal_extension_state_extended(c, context, exten, NULL);
  2816. }
  2817. /*! \brief Check extended extension state for an extension by using hint */
  2818. int ast_extension_state_extended(struct ast_channel *c, const char *context, const char *exten,
  2819. struct ao2_container **device_state_info)
  2820. {
  2821. struct ao2_container *container = NULL;
  2822. int ret;
  2823. if (device_state_info) {
  2824. container = alloc_device_state_info();
  2825. }
  2826. ret = internal_extension_state_extended(c, context, exten, container);
  2827. if (ret < 0 && container) {
  2828. ao2_ref(container, -1);
  2829. container = NULL;
  2830. }
  2831. if (device_state_info) {
  2832. get_device_state_causing_channels(container);
  2833. *device_state_info = container;
  2834. }
  2835. return ret;
  2836. }
  2837. static int extension_presence_state_helper(struct ast_exten *e, char **subtype, char **message)
  2838. {
  2839. struct ast_str *hint_app = ast_str_thread_get(&extensionstate_buf, 32);
  2840. char *presence_provider;
  2841. const char *app;
  2842. if (!e || !hint_app) {
  2843. return -1;
  2844. }
  2845. app = ast_get_extension_app(e);
  2846. if (ast_strlen_zero(app)) {
  2847. return -1;
  2848. }
  2849. ast_str_set(&hint_app, 0, "%s", app);
  2850. presence_provider = parse_hint_presence(hint_app);
  2851. if (ast_strlen_zero(presence_provider)) {
  2852. /* No presence string in the hint */
  2853. return 0;
  2854. }
  2855. return ast_presence_state(presence_provider, subtype, message);
  2856. }
  2857. int ast_hint_presence_state(struct ast_channel *c, const char *context, const char *exten, char **subtype, char **message)
  2858. {
  2859. struct ast_exten *e;
  2860. if (!(e = ast_hint_extension(c, context, exten))) { /* Do we have a hint for this extension ? */
  2861. return -1; /* No hint, return -1 */
  2862. }
  2863. if (e->exten[0] == '_') {
  2864. /* Create this hint on-the-fly */
  2865. ast_add_extension(e->parent->name, 0, exten, e->priority, e->label,
  2866. e->matchcid ? e->cidmatch : NULL, e->app, ast_strdup(e->data), ast_free_ptr,
  2867. e->registrar);
  2868. if (!(e = ast_hint_extension(c, context, exten))) {
  2869. /* Improbable, but not impossible */
  2870. return -1;
  2871. }
  2872. }
  2873. return extension_presence_state_helper(e, subtype, message);
  2874. }
  2875. static int execute_state_callback(ast_state_cb_type cb,
  2876. const char *context,
  2877. const char *exten,
  2878. void *data,
  2879. enum ast_state_cb_update_reason reason,
  2880. struct ast_hint *hint,
  2881. struct ao2_container *device_state_info)
  2882. {
  2883. int res = 0;
  2884. struct ast_state_cb_info info = { 0, };
  2885. info.reason = reason;
  2886. /* Copy over current hint data */
  2887. if (hint) {
  2888. ao2_lock(hint);
  2889. info.exten_state = hint->laststate;
  2890. info.device_state_info = device_state_info;
  2891. info.presence_state = hint->last_presence_state;
  2892. if (!(ast_strlen_zero(hint->last_presence_subtype))) {
  2893. info.presence_subtype = ast_strdupa(hint->last_presence_subtype);
  2894. } else {
  2895. info.presence_subtype = "";
  2896. }
  2897. if (!(ast_strlen_zero(hint->last_presence_message))) {
  2898. info.presence_message = ast_strdupa(hint->last_presence_message);
  2899. } else {
  2900. info.presence_message = "";
  2901. }
  2902. ao2_unlock(hint);
  2903. } else {
  2904. info.exten_state = AST_EXTENSION_REMOVED;
  2905. }
  2906. /* NOTE: The casts will not be needed for v10 and later */
  2907. res = cb((char *) context, (char *) exten, &info, data);
  2908. return res;
  2909. }
  2910. /*!
  2911. * /internal
  2912. * /brief Identify a channel for every device which is supposedly responsible for the device state.
  2913. *
  2914. * Especially when the device is ringing, the oldest ringing channel is chosen.
  2915. * For all other cases the first encountered channel in the specific state is chosen.
  2916. */
  2917. static void get_device_state_causing_channels(struct ao2_container *c)
  2918. {
  2919. struct ao2_iterator iter;
  2920. struct ast_device_state_info *info;
  2921. struct ast_channel *chan;
  2922. if (!c || !ao2_container_count(c)) {
  2923. return;
  2924. }
  2925. iter = ao2_iterator_init(c, 0);
  2926. for (; (info = ao2_iterator_next(&iter)); ao2_ref(info, -1)) {
  2927. enum ast_channel_state search_state = 0; /* prevent false uninit warning */
  2928. char match[AST_CHANNEL_NAME];
  2929. struct ast_channel_iterator *chan_iter;
  2930. struct timeval chantime = {0, }; /* prevent false uninit warning */
  2931. switch (info->device_state) {
  2932. case AST_DEVICE_RINGING:
  2933. case AST_DEVICE_RINGINUSE:
  2934. /* find ringing channel */
  2935. search_state = AST_STATE_RINGING;
  2936. break;
  2937. case AST_DEVICE_BUSY:
  2938. /* find busy channel */
  2939. search_state = AST_STATE_BUSY;
  2940. break;
  2941. case AST_DEVICE_ONHOLD:
  2942. case AST_DEVICE_INUSE:
  2943. /* find up channel */
  2944. search_state = AST_STATE_UP;
  2945. break;
  2946. case AST_DEVICE_UNKNOWN:
  2947. case AST_DEVICE_NOT_INUSE:
  2948. case AST_DEVICE_INVALID:
  2949. case AST_DEVICE_UNAVAILABLE:
  2950. case AST_DEVICE_TOTAL /* not a state */:
  2951. /* no channels are of interest */
  2952. continue;
  2953. }
  2954. /* iterate over all channels of the device */
  2955. snprintf(match, sizeof(match), "%s-", info->device_name);
  2956. chan_iter = ast_channel_iterator_by_name_new(match, strlen(match));
  2957. for (; (chan = ast_channel_iterator_next(chan_iter)); ast_channel_unref(chan)) {
  2958. ast_channel_lock(chan);
  2959. /* this channel's state doesn't match */
  2960. if (search_state != ast_channel_state(chan)) {
  2961. ast_channel_unlock(chan);
  2962. continue;
  2963. }
  2964. /* any non-ringing channel will fit */
  2965. if (search_state != AST_STATE_RINGING) {
  2966. ast_channel_unlock(chan);
  2967. info->causing_channel = chan; /* is kept ref'd! */
  2968. break;
  2969. }
  2970. /* but we need the oldest ringing channel of the device to match with undirected pickup */
  2971. if (!info->causing_channel) {
  2972. chantime = ast_channel_creationtime(chan);
  2973. ast_channel_ref(chan); /* must ref it! */
  2974. info->causing_channel = chan;
  2975. } else if (ast_tvcmp(ast_channel_creationtime(chan), chantime) < 0) {
  2976. chantime = ast_channel_creationtime(chan);
  2977. ast_channel_unref(info->causing_channel);
  2978. ast_channel_ref(chan); /* must ref it! */
  2979. info->causing_channel = chan;
  2980. }
  2981. ast_channel_unlock(chan);
  2982. }
  2983. ast_channel_iterator_destroy(chan_iter);
  2984. }
  2985. ao2_iterator_destroy(&iter);
  2986. }
  2987. static void device_state_notify_callbacks(struct ast_hint *hint, struct ast_str **hint_app)
  2988. {
  2989. struct ao2_iterator cb_iter;
  2990. struct ast_state_cb *state_cb;
  2991. int state;
  2992. int same_state;
  2993. struct ao2_container *device_state_info;
  2994. int first_extended_cb_call = 1;
  2995. char context_name[AST_MAX_CONTEXT];
  2996. char exten_name[AST_MAX_EXTENSION];
  2997. ao2_lock(hint);
  2998. if (!hint->exten) {
  2999. /* The extension has already been destroyed */
  3000. ao2_unlock(hint);
  3001. return;
  3002. }
  3003. /*
  3004. * Save off strings in case the hint extension gets destroyed
  3005. * while we are notifying the watchers.
  3006. */
  3007. ast_copy_string(context_name,
  3008. ast_get_context_name(ast_get_extension_context(hint->exten)),
  3009. sizeof(context_name));
  3010. ast_copy_string(exten_name, ast_get_extension_name(hint->exten),
  3011. sizeof(exten_name));
  3012. ast_str_set(hint_app, 0, "%s", ast_get_extension_app(hint->exten));
  3013. ao2_unlock(hint);
  3014. /*
  3015. * Get device state for this hint.
  3016. *
  3017. * NOTE: We cannot hold any locks while determining the hint
  3018. * device state or notifying the watchers without causing a
  3019. * deadlock. (conlock, hints, and hint)
  3020. */
  3021. /* Make a container so state3 can fill it if we wish.
  3022. * If that failed we simply do not provide the extended state info.
  3023. */
  3024. device_state_info = alloc_device_state_info();
  3025. state = ast_extension_state3(*hint_app, device_state_info);
  3026. same_state = state == hint->laststate;
  3027. if (same_state && (~state & AST_EXTENSION_RINGING)) {
  3028. ao2_cleanup(device_state_info);
  3029. return;
  3030. }
  3031. /* Device state changed since last check - notify the watchers. */
  3032. hint->laststate = state; /* record we saw the change */
  3033. /* For general callbacks */
  3034. if (!same_state) {
  3035. cb_iter = ao2_iterator_init(statecbs, 0);
  3036. for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) {
  3037. execute_state_callback(state_cb->change_cb,
  3038. context_name,
  3039. exten_name,
  3040. state_cb->data,
  3041. AST_HINT_UPDATE_DEVICE,
  3042. hint,
  3043. NULL);
  3044. }
  3045. ao2_iterator_destroy(&cb_iter);
  3046. }
  3047. /* For extension callbacks */
  3048. /* extended callbacks are called when the state changed or when AST_STATE_RINGING is
  3049. * included. Normal callbacks are only called when the state changed.
  3050. */
  3051. cb_iter = ao2_iterator_init(hint->callbacks, 0);
  3052. for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) {
  3053. if (state_cb->extended && first_extended_cb_call) {
  3054. /* Fill detailed device_state_info now that we know it is used by extd. callback */
  3055. first_extended_cb_call = 0;
  3056. get_device_state_causing_channels(device_state_info);
  3057. }
  3058. if (state_cb->extended || !same_state) {
  3059. execute_state_callback(state_cb->change_cb,
  3060. context_name,
  3061. exten_name,
  3062. state_cb->data,
  3063. AST_HINT_UPDATE_DEVICE,
  3064. hint,
  3065. state_cb->extended ? device_state_info : NULL);
  3066. }
  3067. }
  3068. ao2_iterator_destroy(&cb_iter);
  3069. ao2_cleanup(device_state_info);
  3070. }
  3071. static void presence_state_notify_callbacks(
  3072. struct stasis_message *msg, struct ast_hint *hint, struct ast_str **hint_app,
  3073. struct ast_presence_state_message *presence_state)
  3074. {
  3075. struct ao2_iterator cb_iter;
  3076. struct ast_state_cb *state_cb;
  3077. char context_name[AST_MAX_CONTEXT];
  3078. char exten_name[AST_MAX_EXTENSION];
  3079. ao2_lock(hint);
  3080. if (!hint->exten) {
  3081. /* The extension has already been destroyed */
  3082. ao2_unlock(hint);
  3083. return;
  3084. }
  3085. if (hint_change_message_type() != stasis_message_type(msg)) {
  3086. const char *app;
  3087. char *parse;
  3088. /* Does this hint monitor the device that changed state? */
  3089. app = ast_get_extension_app(hint->exten);
  3090. if (ast_strlen_zero(app)) {
  3091. /* The hint does not monitor presence at all. */
  3092. ao2_unlock(hint);
  3093. return;
  3094. }
  3095. ast_str_set(hint_app, 0, "%s", app);
  3096. parse = parse_hint_presence(*hint_app);
  3097. if (ast_strlen_zero(parse)) {
  3098. ao2_unlock(hint);
  3099. return;
  3100. }
  3101. if (strcasecmp(parse, presence_state->provider)) {
  3102. /* The hint does not monitor the presence provider. */
  3103. ao2_unlock(hint);
  3104. return;
  3105. }
  3106. }
  3107. /*
  3108. * Save off strings in case the hint extension gets destroyed
  3109. * while we are notifying the watchers.
  3110. */
  3111. ast_copy_string(context_name,
  3112. ast_get_context_name(ast_get_extension_context(hint->exten)),
  3113. sizeof(context_name));
  3114. ast_copy_string(exten_name, ast_get_extension_name(hint->exten),
  3115. sizeof(exten_name));
  3116. ast_str_set(hint_app, 0, "%s", ast_get_extension_app(hint->exten));
  3117. /* Check to see if update is necessary */
  3118. if ((hint->last_presence_state == presence_state->state) &&
  3119. ((hint->last_presence_subtype && presence_state->subtype &&
  3120. !strcmp(hint->last_presence_subtype, presence_state->subtype)) ||
  3121. (!hint->last_presence_subtype && !presence_state->subtype)) &&
  3122. ((hint->last_presence_message && presence_state->message &&
  3123. !strcmp(hint->last_presence_message, presence_state->message)) ||
  3124. (!hint->last_presence_message && !presence_state->message))) {
  3125. /* this update is the same as the last, do nothing */
  3126. ao2_unlock(hint);
  3127. return;
  3128. }
  3129. /* update new values */
  3130. ast_free(hint->last_presence_subtype);
  3131. ast_free(hint->last_presence_message);
  3132. hint->last_presence_state = presence_state->state;
  3133. hint->last_presence_subtype = presence_state->subtype ? ast_strdup(presence_state->subtype) : NULL;
  3134. hint->last_presence_message = presence_state->message ? ast_strdup(presence_state->message) : NULL;
  3135. /*
  3136. * NOTE: We cannot hold any locks while notifying
  3137. * the watchers without causing a deadlock.
  3138. * (conlock, hints, and hint)
  3139. */
  3140. ao2_unlock(hint);
  3141. /* For general callbacks */
  3142. cb_iter = ao2_iterator_init(statecbs, 0);
  3143. for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) {
  3144. execute_state_callback(state_cb->change_cb,
  3145. context_name,
  3146. exten_name,
  3147. state_cb->data,
  3148. AST_HINT_UPDATE_PRESENCE,
  3149. hint,
  3150. NULL);
  3151. }
  3152. ao2_iterator_destroy(&cb_iter);
  3153. /* For extension callbacks */
  3154. cb_iter = ao2_iterator_init(hint->callbacks, 0);
  3155. for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_cleanup(state_cb)) {
  3156. execute_state_callback(state_cb->change_cb,
  3157. context_name,
  3158. exten_name,
  3159. state_cb->data,
  3160. AST_HINT_UPDATE_PRESENCE,
  3161. hint,
  3162. NULL);
  3163. }
  3164. ao2_iterator_destroy(&cb_iter);
  3165. }
  3166. static int handle_hint_change_message_type(struct stasis_message *msg, enum ast_state_cb_update_reason reason)
  3167. {
  3168. struct ast_hint *hint;
  3169. struct ast_str *hint_app;
  3170. if (hint_change_message_type() != stasis_message_type(msg)) {
  3171. return 0;
  3172. }
  3173. if (!(hint_app = ast_str_create(1024))) {
  3174. return -1;
  3175. }
  3176. hint = stasis_message_data(msg);
  3177. switch (reason) {
  3178. case AST_HINT_UPDATE_DEVICE:
  3179. device_state_notify_callbacks(hint, &hint_app);
  3180. break;
  3181. case AST_HINT_UPDATE_PRESENCE:
  3182. {
  3183. char *presence_subtype = NULL;
  3184. char *presence_message = NULL;
  3185. int state;
  3186. state = extension_presence_state_helper(
  3187. hint->exten, &presence_subtype, &presence_message);
  3188. {
  3189. struct ast_presence_state_message presence_state = {
  3190. .state = state > 0 ? state : AST_PRESENCE_INVALID,
  3191. .subtype = presence_subtype,
  3192. .message = presence_message
  3193. };
  3194. presence_state_notify_callbacks(msg, hint, &hint_app, &presence_state);
  3195. }
  3196. ast_free(presence_subtype);
  3197. ast_free(presence_message);
  3198. }
  3199. break;
  3200. }
  3201. ast_free(hint_app);
  3202. return 1;
  3203. }
  3204. static void device_state_cb(void *unused, struct stasis_subscription *sub, struct stasis_message *msg)
  3205. {
  3206. struct ast_device_state_message *dev_state;
  3207. struct ast_str *hint_app;
  3208. struct ast_hintdevice *device;
  3209. struct ast_hintdevice *cmpdevice;
  3210. struct ao2_iterator *dev_iter;
  3211. if (handle_hint_change_message_type(msg, AST_HINT_UPDATE_DEVICE)) {
  3212. return;
  3213. }
  3214. if (hint_remove_message_type() == stasis_message_type(msg)) {
  3215. /* The extension has already been destroyed */
  3216. struct ast_state_cb *state_cb;
  3217. struct ao2_iterator cb_iter;
  3218. struct ast_hint *hint = stasis_message_data(msg);
  3219. ao2_lock(hint);
  3220. hint->laststate = AST_EXTENSION_DEACTIVATED;
  3221. ao2_unlock(hint);
  3222. cb_iter = ao2_iterator_init(hint->callbacks, 0);
  3223. for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) {
  3224. execute_state_callback(state_cb->change_cb,
  3225. hint->context_name,
  3226. hint->exten_name,
  3227. state_cb->data,
  3228. AST_HINT_UPDATE_DEVICE,
  3229. hint,
  3230. NULL);
  3231. }
  3232. ao2_iterator_destroy(&cb_iter);
  3233. return;
  3234. }
  3235. if (ast_device_state_message_type() != stasis_message_type(msg)) {
  3236. return;
  3237. }
  3238. dev_state = stasis_message_data(msg);
  3239. if (dev_state->eid) {
  3240. /* ignore non-aggregate states */
  3241. return;
  3242. }
  3243. if (ao2_container_count(hintdevices) == 0) {
  3244. /* There are no hints monitoring devices. */
  3245. return;
  3246. }
  3247. hint_app = ast_str_create(1024);
  3248. if (!hint_app) {
  3249. return;
  3250. }
  3251. cmpdevice = ast_alloca(sizeof(*cmpdevice) + strlen(dev_state->device));
  3252. strcpy(cmpdevice->hintdevice, dev_state->device);
  3253. ast_mutex_lock(&context_merge_lock);/* Hold off ast_merge_contexts_and_delete */
  3254. dev_iter = ao2_t_callback(hintdevices,
  3255. OBJ_SEARCH_OBJECT | OBJ_MULTIPLE,
  3256. hintdevice_cmp_multiple,
  3257. cmpdevice,
  3258. "find devices in container");
  3259. if (!dev_iter) {
  3260. ast_mutex_unlock(&context_merge_lock);
  3261. ast_free(hint_app);
  3262. return;
  3263. }
  3264. for (; (device = ao2_iterator_next(dev_iter)); ao2_t_ref(device, -1, "Next device")) {
  3265. if (device->hint) {
  3266. device_state_notify_callbacks(device->hint, &hint_app);
  3267. }
  3268. }
  3269. ast_mutex_unlock(&context_merge_lock);
  3270. ao2_iterator_destroy(dev_iter);
  3271. ast_free(hint_app);
  3272. return;
  3273. }
  3274. /*!
  3275. * \internal
  3276. * \brief Destroy the given state callback object.
  3277. *
  3278. * \param doomed State callback to destroy.
  3279. *
  3280. * \return Nothing
  3281. */
  3282. static void destroy_state_cb(void *doomed)
  3283. {
  3284. struct ast_state_cb *state_cb = doomed;
  3285. if (state_cb->destroy_cb) {
  3286. state_cb->destroy_cb(state_cb->id, state_cb->data);
  3287. }
  3288. }
  3289. /*!
  3290. * \internal
  3291. * \brief Add watcher for extension states with destructor
  3292. */
  3293. static int extension_state_add_destroy(const char *context, const char *exten,
  3294. ast_state_cb_type change_cb, ast_state_cb_destroy_type destroy_cb, void *data, int extended)
  3295. {
  3296. struct ast_hint *hint;
  3297. struct ast_state_cb *state_cb;
  3298. struct ast_exten *e;
  3299. int id;
  3300. /* If there's no context and extension: add callback to statecbs list */
  3301. if (!context && !exten) {
  3302. /* Prevent multiple adds from adding the same change_cb at the same time. */
  3303. ao2_lock(statecbs);
  3304. /* Remove any existing change_cb. */
  3305. ao2_find(statecbs, change_cb, OBJ_UNLINK | OBJ_NODATA);
  3306. /* Now insert the change_cb */
  3307. if (!(state_cb = ao2_alloc(sizeof(*state_cb), destroy_state_cb))) {
  3308. ao2_unlock(statecbs);
  3309. return -1;
  3310. }
  3311. state_cb->id = 0;
  3312. state_cb->change_cb = change_cb;
  3313. state_cb->destroy_cb = destroy_cb;
  3314. state_cb->data = data;
  3315. state_cb->extended = extended;
  3316. ao2_link(statecbs, state_cb);
  3317. ao2_ref(state_cb, -1);
  3318. ao2_unlock(statecbs);
  3319. return 0;
  3320. }
  3321. if (!context || !exten)
  3322. return -1;
  3323. /* This callback type is for only one hint, so get the hint */
  3324. e = ast_hint_extension(NULL, context, exten);
  3325. if (!e) {
  3326. return -1;
  3327. }
  3328. /* If this is a pattern, dynamically create a new extension for this
  3329. * particular match. Note that this will only happen once for each
  3330. * individual extension, because the pattern will no longer match first.
  3331. */
  3332. if (e->exten[0] == '_') {
  3333. ast_add_extension(e->parent->name, 0, exten, e->priority, e->label,
  3334. e->matchcid ? e->cidmatch : NULL, e->app, ast_strdup(e->data), ast_free_ptr,
  3335. e->registrar);
  3336. e = ast_hint_extension(NULL, context, exten);
  3337. if (!e || e->exten[0] == '_') {
  3338. return -1;
  3339. }
  3340. }
  3341. /* Find the hint in the hints container */
  3342. ao2_lock(hints);/* Locked to hold off ast_merge_contexts_and_delete */
  3343. hint = ao2_find(hints, e, 0);
  3344. if (!hint) {
  3345. ao2_unlock(hints);
  3346. return -1;
  3347. }
  3348. /* Now insert the callback in the callback list */
  3349. if (!(state_cb = ao2_alloc(sizeof(*state_cb), destroy_state_cb))) {
  3350. ao2_ref(hint, -1);
  3351. ao2_unlock(hints);
  3352. return -1;
  3353. }
  3354. do {
  3355. id = stateid++; /* Unique ID for this callback */
  3356. /* Do not allow id to ever be -1 or 0. */
  3357. } while (id == -1 || id == 0);
  3358. state_cb->id = id;
  3359. state_cb->change_cb = change_cb; /* Pointer to callback routine */
  3360. state_cb->destroy_cb = destroy_cb;
  3361. state_cb->data = data; /* Data for the callback */
  3362. state_cb->extended = extended;
  3363. ao2_link(hint->callbacks, state_cb);
  3364. ao2_ref(state_cb, -1);
  3365. ao2_ref(hint, -1);
  3366. ao2_unlock(hints);
  3367. return id;
  3368. }
  3369. int ast_extension_state_add_destroy(const char *context, const char *exten,
  3370. ast_state_cb_type change_cb, ast_state_cb_destroy_type destroy_cb, void *data)
  3371. {
  3372. return extension_state_add_destroy(context, exten, change_cb, destroy_cb, data, 0);
  3373. }
  3374. int ast_extension_state_add(const char *context, const char *exten,
  3375. ast_state_cb_type change_cb, void *data)
  3376. {
  3377. return extension_state_add_destroy(context, exten, change_cb, NULL, data, 0);
  3378. }
  3379. int ast_extension_state_add_destroy_extended(const char *context, const char *exten,
  3380. ast_state_cb_type change_cb, ast_state_cb_destroy_type destroy_cb, void *data)
  3381. {
  3382. return extension_state_add_destroy(context, exten, change_cb, destroy_cb, data, 1);
  3383. }
  3384. int ast_extension_state_add_extended(const char *context, const char *exten,
  3385. ast_state_cb_type change_cb, void *data)
  3386. {
  3387. return extension_state_add_destroy(context, exten, change_cb, NULL, data, 1);
  3388. }
  3389. /*! \brief Find Hint by callback id */
  3390. static int find_hint_by_cb_id(void *obj, void *arg, int flags)
  3391. {
  3392. struct ast_state_cb *state_cb;
  3393. const struct ast_hint *hint = obj;
  3394. int *id = arg;
  3395. if ((state_cb = ao2_find(hint->callbacks, id, 0))) {
  3396. ao2_ref(state_cb, -1);
  3397. return CMP_MATCH | CMP_STOP;
  3398. }
  3399. return 0;
  3400. }
  3401. int ast_extension_state_del(int id, ast_state_cb_type change_cb)
  3402. {
  3403. struct ast_state_cb *p_cur;
  3404. int ret = -1;
  3405. if (!id) { /* id == 0 is a callback without extension */
  3406. if (!change_cb) {
  3407. return ret;
  3408. }
  3409. p_cur = ao2_find(statecbs, change_cb, OBJ_UNLINK);
  3410. if (p_cur) {
  3411. ret = 0;
  3412. ao2_ref(p_cur, -1);
  3413. }
  3414. } else { /* callback with extension, find the callback based on ID */
  3415. struct ast_hint *hint;
  3416. ao2_lock(hints);/* Locked to hold off ast_merge_contexts_and_delete */
  3417. hint = ao2_callback(hints, 0, find_hint_by_cb_id, &id);
  3418. if (hint) {
  3419. p_cur = ao2_find(hint->callbacks, &id, OBJ_UNLINK);
  3420. if (p_cur) {
  3421. ret = 0;
  3422. ao2_ref(p_cur, -1);
  3423. }
  3424. ao2_ref(hint, -1);
  3425. }
  3426. ao2_unlock(hints);
  3427. }
  3428. return ret;
  3429. }
  3430. static int hint_id_cmp(void *obj, void *arg, int flags)
  3431. {
  3432. const struct ast_state_cb *cb = obj;
  3433. int *id = arg;
  3434. return (cb->id == *id) ? CMP_MATCH | CMP_STOP : 0;
  3435. }
  3436. /*!
  3437. * \internal
  3438. * \brief Destroy the given hint object.
  3439. *
  3440. * \param obj Hint to destroy.
  3441. *
  3442. * \return Nothing
  3443. */
  3444. static void destroy_hint(void *obj)
  3445. {
  3446. struct ast_hint *hint = obj;
  3447. int i;
  3448. ao2_cleanup(hint->callbacks);
  3449. for (i = 0; i < AST_VECTOR_SIZE(&hint->devices); i++) {
  3450. char *device = AST_VECTOR_GET(&hint->devices, i);
  3451. ast_free(device);
  3452. }
  3453. AST_VECTOR_FREE(&hint->devices);
  3454. ast_free(hint->last_presence_subtype);
  3455. ast_free(hint->last_presence_message);
  3456. }
  3457. /*! \brief Publish a hint removed event */
  3458. static int publish_hint_remove(struct ast_hint *hint)
  3459. {
  3460. struct stasis_message *message;
  3461. if (!hint_remove_message_type()) {
  3462. return -1;
  3463. }
  3464. if (!(message = stasis_message_create(hint_remove_message_type(), hint))) {
  3465. ao2_ref(hint, -1);
  3466. return -1;
  3467. }
  3468. stasis_publish(ast_device_state_topic_all(), message);
  3469. ao2_ref(message, -1);
  3470. return 0;
  3471. }
  3472. /*! \brief Remove hint from extension */
  3473. static int ast_remove_hint(struct ast_exten *e)
  3474. {
  3475. /* Cleanup the Notifys if hint is removed */
  3476. struct ast_hint *hint;
  3477. if (!e) {
  3478. return -1;
  3479. }
  3480. hint = ao2_find(hints, e, OBJ_UNLINK);
  3481. if (!hint) {
  3482. return -1;
  3483. }
  3484. remove_hintdevice(hint);
  3485. /*
  3486. * The extension is being destroyed so we must save some
  3487. * information to notify that the extension is deactivated.
  3488. */
  3489. ao2_lock(hint);
  3490. ast_copy_string(hint->context_name,
  3491. ast_get_context_name(ast_get_extension_context(hint->exten)),
  3492. sizeof(hint->context_name));
  3493. ast_copy_string(hint->exten_name, ast_get_extension_name(hint->exten),
  3494. sizeof(hint->exten_name));
  3495. hint->exten = NULL;
  3496. ao2_unlock(hint);
  3497. publish_hint_remove(hint);
  3498. ao2_ref(hint, -1);
  3499. return 0;
  3500. }
  3501. /*! \brief Add hint to hint list, check initial extension state */
  3502. static int ast_add_hint(struct ast_exten *e)
  3503. {
  3504. struct ast_hint *hint_new;
  3505. struct ast_hint *hint_found;
  3506. char *message = NULL;
  3507. char *subtype = NULL;
  3508. int presence_state;
  3509. if (!e) {
  3510. return -1;
  3511. }
  3512. /*
  3513. * We must create the hint we wish to add before determining if
  3514. * it is already in the hints container to avoid possible
  3515. * deadlock when getting the current extension state.
  3516. */
  3517. hint_new = ao2_alloc(sizeof(*hint_new), destroy_hint);
  3518. if (!hint_new) {
  3519. return -1;
  3520. }
  3521. AST_VECTOR_INIT(&hint_new->devices, 8);
  3522. /* Initialize new hint. */
  3523. hint_new->callbacks = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, hint_id_cmp);
  3524. if (!hint_new->callbacks) {
  3525. ao2_ref(hint_new, -1);
  3526. return -1;
  3527. }
  3528. hint_new->exten = e;
  3529. if (strstr(e->app, "${") && e->exten[0] == '_') {
  3530. /* The hint is dynamic and hasn't been evaluted yet */
  3531. hint_new->laststate = AST_DEVICE_INVALID;
  3532. hint_new->last_presence_state = AST_PRESENCE_INVALID;
  3533. } else {
  3534. hint_new->laststate = ast_extension_state2(e, NULL);
  3535. if ((presence_state = extension_presence_state_helper(e, &subtype, &message)) > 0) {
  3536. hint_new->last_presence_state = presence_state;
  3537. hint_new->last_presence_subtype = subtype;
  3538. hint_new->last_presence_message = message;
  3539. }
  3540. }
  3541. /* Prevent multiple add hints from adding the same hint at the same time. */
  3542. ao2_lock(hints);
  3543. /* Search if hint exists, do nothing */
  3544. hint_found = ao2_find(hints, e, 0);
  3545. if (hint_found) {
  3546. ao2_ref(hint_found, -1);
  3547. ao2_unlock(hints);
  3548. ao2_ref(hint_new, -1);
  3549. ast_debug(2, "HINTS: Not re-adding existing hint %s: %s\n",
  3550. ast_get_extension_name(e), ast_get_extension_app(e));
  3551. return -1;
  3552. }
  3553. /* Add new hint to the hints container */
  3554. ast_debug(2, "HINTS: Adding hint %s: %s\n",
  3555. ast_get_extension_name(e), ast_get_extension_app(e));
  3556. ao2_link(hints, hint_new);
  3557. if (add_hintdevice(hint_new, ast_get_extension_app(e))) {
  3558. ast_log(LOG_WARNING, "Could not add devices for hint: %s@%s.\n",
  3559. ast_get_extension_name(e),
  3560. ast_get_context_name(ast_get_extension_context(e)));
  3561. }
  3562. /* if not dynamic */
  3563. if (!(strstr(e->app, "${") && e->exten[0] == '_')) {
  3564. struct ast_state_cb *state_cb;
  3565. struct ao2_iterator cb_iter;
  3566. /* For general callbacks */
  3567. cb_iter = ao2_iterator_init(statecbs, 0);
  3568. for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) {
  3569. execute_state_callback(state_cb->change_cb,
  3570. ast_get_context_name(ast_get_extension_context(e)),
  3571. ast_get_extension_name(e),
  3572. state_cb->data,
  3573. AST_HINT_UPDATE_DEVICE,
  3574. hint_new,
  3575. NULL);
  3576. }
  3577. ao2_iterator_destroy(&cb_iter);
  3578. }
  3579. ao2_unlock(hints);
  3580. ao2_ref(hint_new, -1);
  3581. return 0;
  3582. }
  3583. /*! \brief Publish a hint changed event */
  3584. static int publish_hint_change(struct ast_hint *hint, struct ast_exten *ne)
  3585. {
  3586. struct stasis_message *message;
  3587. if (!hint_change_message_type()) {
  3588. return -1;
  3589. }
  3590. if (!(message = stasis_message_create(hint_change_message_type(), hint))) {
  3591. ao2_ref(hint, -1);
  3592. return -1;
  3593. }
  3594. stasis_publish(ast_device_state_topic_all(), message);
  3595. stasis_publish(ast_presence_state_topic_all(), message);
  3596. ao2_ref(message, -1);
  3597. return 0;
  3598. }
  3599. /*! \brief Change hint for an extension */
  3600. static int ast_change_hint(struct ast_exten *oe, struct ast_exten *ne)
  3601. {
  3602. struct ast_hint *hint;
  3603. if (!oe || !ne) {
  3604. return -1;
  3605. }
  3606. ao2_lock(hints);/* Locked to hold off others while we move the hint around. */
  3607. /*
  3608. * Unlink the hint from the hints container as the extension
  3609. * name (which is the hash value) could change.
  3610. */
  3611. hint = ao2_find(hints, oe, OBJ_UNLINK);
  3612. if (!hint) {
  3613. ao2_unlock(hints);
  3614. ast_mutex_unlock(&context_merge_lock);
  3615. return -1;
  3616. }
  3617. remove_hintdevice(hint);
  3618. /* Update the hint and put it back in the hints container. */
  3619. ao2_lock(hint);
  3620. hint->exten = ne;
  3621. ao2_unlock(hint);
  3622. ao2_link(hints, hint);
  3623. if (add_hintdevice(hint, ast_get_extension_app(ne))) {
  3624. ast_log(LOG_WARNING, "Could not add devices for hint: %s@%s.\n",
  3625. ast_get_extension_name(ne),
  3626. ast_get_context_name(ast_get_extension_context(ne)));
  3627. }
  3628. ao2_unlock(hints);
  3629. publish_hint_change(hint, ne);
  3630. ao2_ref(hint, -1);
  3631. return 0;
  3632. }
  3633. /*! \brief Get hint for channel */
  3634. int ast_get_hint(char *hint, int hintsize, char *name, int namesize, struct ast_channel *c, const char *context, const char *exten)
  3635. {
  3636. struct ast_exten *e = ast_hint_extension(c, context, exten);
  3637. if (e) {
  3638. if (hint)
  3639. ast_copy_string(hint, ast_get_extension_app(e), hintsize);
  3640. if (name) {
  3641. const char *tmp = ast_get_extension_app_data(e);
  3642. if (tmp)
  3643. ast_copy_string(name, tmp, namesize);
  3644. }
  3645. return -1;
  3646. }
  3647. return 0;
  3648. }
  3649. /*! \brief Get hint for channel */
  3650. int ast_str_get_hint(struct ast_str **hint, ssize_t hintsize, struct ast_str **name, ssize_t namesize, struct ast_channel *c, const char *context, const char *exten)
  3651. {
  3652. struct ast_exten *e = ast_hint_extension(c, context, exten);
  3653. if (!e) {
  3654. return 0;
  3655. }
  3656. if (hint) {
  3657. ast_str_set(hint, hintsize, "%s", ast_get_extension_app(e));
  3658. }
  3659. if (name) {
  3660. const char *tmp = ast_get_extension_app_data(e);
  3661. if (tmp) {
  3662. ast_str_set(name, namesize, "%s", tmp);
  3663. }
  3664. }
  3665. return -1;
  3666. }
  3667. int ast_exists_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
  3668. {
  3669. return pbx_extension_helper(c, NULL, context, exten, priority, NULL, callerid, E_MATCH, 0, 0);
  3670. }
  3671. int ast_findlabel_extension(struct ast_channel *c, const char *context, const char *exten, const char *label, const char *callerid)
  3672. {
  3673. return pbx_extension_helper(c, NULL, context, exten, 0, label, callerid, E_FINDLABEL, 0, 0);
  3674. }
  3675. int ast_findlabel_extension2(struct ast_channel *c, struct ast_context *con, const char *exten, const char *label, const char *callerid)
  3676. {
  3677. return pbx_extension_helper(c, con, NULL, exten, 0, label, callerid, E_FINDLABEL, 0, 0);
  3678. }
  3679. int ast_canmatch_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
  3680. {
  3681. return pbx_extension_helper(c, NULL, context, exten, priority, NULL, callerid, E_CANMATCH, 0, 0);
  3682. }
  3683. int ast_matchmore_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
  3684. {
  3685. return pbx_extension_helper(c, NULL, context, exten, priority, NULL, callerid, E_MATCHMORE, 0, 0);
  3686. }
  3687. int ast_spawn_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid, int *found, int combined_find_spawn)
  3688. {
  3689. return pbx_extension_helper(c, NULL, context, exten, priority, NULL, callerid, E_SPAWN, found, combined_find_spawn);
  3690. }
  3691. void ast_pbx_h_exten_run(struct ast_channel *chan, const char *context)
  3692. {
  3693. int autoloopflag;
  3694. int found;
  3695. int spawn_error;
  3696. ast_channel_lock(chan);
  3697. /*
  3698. * Make sure that the channel is marked as hungup since we are
  3699. * going to run the h exten on it.
  3700. */
  3701. ast_softhangup_nolock(chan, AST_SOFTHANGUP_HANGUP_EXEC);
  3702. /* Set h exten location */
  3703. if (context != ast_channel_context(chan)) {
  3704. ast_channel_context_set(chan, context);
  3705. }
  3706. ast_channel_exten_set(chan, "h");
  3707. ast_channel_priority_set(chan, 1);
  3708. /* Save autoloop flag */
  3709. autoloopflag = ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP);
  3710. ast_set_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP);
  3711. ast_channel_unlock(chan);
  3712. for (;;) {
  3713. spawn_error = ast_spawn_extension(chan, ast_channel_context(chan),
  3714. ast_channel_exten(chan), ast_channel_priority(chan),
  3715. S_COR(ast_channel_caller(chan)->id.number.valid,
  3716. ast_channel_caller(chan)->id.number.str, NULL), &found, 1);
  3717. ast_channel_lock(chan);
  3718. if (spawn_error) {
  3719. /* The code after the loop needs the channel locked. */
  3720. break;
  3721. }
  3722. ast_channel_priority_set(chan, ast_channel_priority(chan) + 1);
  3723. ast_channel_unlock(chan);
  3724. }
  3725. if (found && spawn_error) {
  3726. /* Something bad happened, or a hangup has been requested. */
  3727. ast_debug(1, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n",
  3728. ast_channel_context(chan), ast_channel_exten(chan),
  3729. ast_channel_priority(chan), ast_channel_name(chan));
  3730. ast_verb(2, "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n",
  3731. ast_channel_context(chan), ast_channel_exten(chan),
  3732. ast_channel_priority(chan), ast_channel_name(chan));
  3733. }
  3734. /* An "h" exten has been run, so indicate that one has been run. */
  3735. ast_set_flag(ast_channel_flags(chan), AST_FLAG_BRIDGE_HANGUP_RUN);
  3736. /* Restore autoloop flag */
  3737. ast_set2_flag(ast_channel_flags(chan), autoloopflag, AST_FLAG_IN_AUTOLOOP);
  3738. ast_channel_unlock(chan);
  3739. }
  3740. /*! helper function to set extension and priority */
  3741. void set_ext_pri(struct ast_channel *c, const char *exten, int pri)
  3742. {
  3743. ast_channel_lock(c);
  3744. ast_channel_exten_set(c, exten);
  3745. ast_channel_priority_set(c, pri);
  3746. ast_channel_unlock(c);
  3747. }
  3748. /*!
  3749. * \brief collect digits from the channel into the buffer.
  3750. * \param c, buf, buflen, pos
  3751. * \param waittime is in milliseconds
  3752. * \retval 0 on timeout or done.
  3753. * \retval -1 on error.
  3754. */
  3755. static int collect_digits(struct ast_channel *c, int waittime, char *buf, int buflen, int pos)
  3756. {
  3757. int digit;
  3758. buf[pos] = '\0'; /* make sure it is properly terminated */
  3759. while (ast_matchmore_extension(c, ast_channel_context(c), buf, 1,
  3760. S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) {
  3761. /* As long as we're willing to wait, and as long as it's not defined,
  3762. keep reading digits until we can't possibly get a right answer anymore. */
  3763. digit = ast_waitfordigit(c, waittime);
  3764. if (ast_channel_softhangup_internal_flag(c) & AST_SOFTHANGUP_ASYNCGOTO) {
  3765. ast_channel_clear_softhangup(c, AST_SOFTHANGUP_ASYNCGOTO);
  3766. } else {
  3767. if (!digit) /* No entry */
  3768. break;
  3769. if (digit < 0) /* Error, maybe a hangup */
  3770. return -1;
  3771. if (pos < buflen - 1) { /* XXX maybe error otherwise ? */
  3772. buf[pos++] = digit;
  3773. buf[pos] = '\0';
  3774. }
  3775. waittime = ast_channel_pbx(c)->dtimeoutms;
  3776. }
  3777. }
  3778. return 0;
  3779. }
  3780. static enum ast_pbx_result __ast_pbx_run(struct ast_channel *c,
  3781. struct ast_pbx_args *args)
  3782. {
  3783. int found = 0; /* set if we find at least one match */
  3784. int res = 0;
  3785. int autoloopflag;
  3786. int error = 0; /* set an error conditions */
  3787. struct ast_pbx *pbx;
  3788. struct ast_callid *callid;
  3789. /* A little initial setup here */
  3790. if (ast_channel_pbx(c)) {
  3791. ast_log(LOG_WARNING, "%s already has PBX structure??\n", ast_channel_name(c));
  3792. /* XXX and now what ? */
  3793. ast_free(ast_channel_pbx(c));
  3794. }
  3795. if (!(pbx = ast_calloc(1, sizeof(*pbx)))) {
  3796. return AST_PBX_FAILED;
  3797. }
  3798. callid = ast_read_threadstorage_callid();
  3799. /* If the thread isn't already associated with a callid, we should create that association. */
  3800. if (!callid) {
  3801. /* Associate new PBX thread with the channel call id if it is availble.
  3802. * If not, create a new one instead.
  3803. */
  3804. callid = ast_channel_callid(c);
  3805. if (!callid) {
  3806. callid = ast_create_callid();
  3807. if (callid) {
  3808. ast_channel_lock(c);
  3809. ast_channel_callid_set(c, callid);
  3810. ast_channel_unlock(c);
  3811. }
  3812. }
  3813. ast_callid_threadassoc_add(callid);
  3814. callid = ast_callid_unref(callid);
  3815. } else {
  3816. /* Nothing to do here, The thread is already bound to a callid. Let's just get rid of the reference. */
  3817. ast_callid_unref(callid);
  3818. }
  3819. ast_channel_pbx_set(c, pbx);
  3820. /* Set reasonable defaults */
  3821. ast_channel_pbx(c)->rtimeoutms = 10000;
  3822. ast_channel_pbx(c)->dtimeoutms = 5000;
  3823. ast_channel_lock(c);
  3824. autoloopflag = ast_test_flag(ast_channel_flags(c), AST_FLAG_IN_AUTOLOOP); /* save value to restore at the end */
  3825. ast_set_flag(ast_channel_flags(c), AST_FLAG_IN_AUTOLOOP);
  3826. ast_channel_unlock(c);
  3827. if (ast_strlen_zero(ast_channel_exten(c))) {
  3828. /* If not successful fall back to 's' - but only if there is no given exten */
  3829. ast_verb(2, "Starting %s at %s,%s,%d failed so falling back to exten 's'\n", ast_channel_name(c), ast_channel_context(c), ast_channel_exten(c), ast_channel_priority(c));
  3830. /* XXX the original code used the existing priority in the call to
  3831. * ast_exists_extension(), and reset it to 1 afterwards.
  3832. * I believe the correct thing is to set it to 1 immediately.
  3833. */
  3834. set_ext_pri(c, "s", 1);
  3835. }
  3836. for (;;) {
  3837. char dst_exten[256]; /* buffer to accumulate digits */
  3838. int pos = 0; /* XXX should check bounds */
  3839. int digit = 0;
  3840. int invalid = 0;
  3841. int timeout = 0;
  3842. /* No digits pressed yet */
  3843. dst_exten[pos] = '\0';
  3844. /* loop on priorities in this context/exten */
  3845. while (!(res = ast_spawn_extension(c, ast_channel_context(c), ast_channel_exten(c), ast_channel_priority(c),
  3846. S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL),
  3847. &found, 1))) {
  3848. if (!ast_check_hangup(c)) {
  3849. ast_channel_priority_set(c, ast_channel_priority(c) + 1);
  3850. continue;
  3851. }
  3852. /* Check softhangup flags. */
  3853. if (ast_channel_softhangup_internal_flag(c) & AST_SOFTHANGUP_ASYNCGOTO) {
  3854. ast_channel_clear_softhangup(c, AST_SOFTHANGUP_ASYNCGOTO);
  3855. continue;
  3856. }
  3857. if (ast_channel_softhangup_internal_flag(c) & AST_SOFTHANGUP_TIMEOUT) {
  3858. if (ast_exists_extension(c, ast_channel_context(c), "T", 1,
  3859. S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) {
  3860. set_ext_pri(c, "T", 1);
  3861. /* If the AbsoluteTimeout is not reset to 0, we'll get an infinite loop */
  3862. memset(ast_channel_whentohangup(c), 0, sizeof(*ast_channel_whentohangup(c)));
  3863. ast_channel_clear_softhangup(c, AST_SOFTHANGUP_TIMEOUT);
  3864. continue;
  3865. } else if (ast_exists_extension(c, ast_channel_context(c), "e", 1,
  3866. S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) {
  3867. raise_exception(c, "ABSOLUTETIMEOUT", 1);
  3868. /* If the AbsoluteTimeout is not reset to 0, we'll get an infinite loop */
  3869. memset(ast_channel_whentohangup(c), 0, sizeof(*ast_channel_whentohangup(c)));
  3870. ast_channel_clear_softhangup(c, AST_SOFTHANGUP_TIMEOUT);
  3871. continue;
  3872. }
  3873. /* Call timed out with no special extension to jump to. */
  3874. error = 1;
  3875. break;
  3876. }
  3877. ast_debug(1, "Extension %s, priority %d returned normally even though call was hung up\n",
  3878. ast_channel_exten(c), ast_channel_priority(c));
  3879. error = 1;
  3880. break;
  3881. } /* end while - from here on we can use 'break' to go out */
  3882. if (found && res) {
  3883. /* Something bad happened, or a hangup has been requested. */
  3884. if (strchr("0123456789ABCDEF*#", res)) {
  3885. ast_debug(1, "Oooh, got something to jump out with ('%c')!\n", res);
  3886. pos = 0;
  3887. dst_exten[pos++] = digit = res;
  3888. dst_exten[pos] = '\0';
  3889. } else if (res == AST_PBX_INCOMPLETE) {
  3890. ast_debug(1, "Spawn extension (%s,%s,%d) exited INCOMPLETE on '%s'\n", ast_channel_context(c), ast_channel_exten(c), ast_channel_priority(c), ast_channel_name(c));
  3891. ast_verb(2, "Spawn extension (%s, %s, %d) exited INCOMPLETE on '%s'\n", ast_channel_context(c), ast_channel_exten(c), ast_channel_priority(c), ast_channel_name(c));
  3892. /* Don't cycle on incomplete - this will happen if the only extension that matches is our "incomplete" extension */
  3893. if (!ast_matchmore_extension(c, ast_channel_context(c), ast_channel_exten(c), 1,
  3894. S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) {
  3895. invalid = 1;
  3896. } else {
  3897. ast_copy_string(dst_exten, ast_channel_exten(c), sizeof(dst_exten));
  3898. digit = 1;
  3899. pos = strlen(dst_exten);
  3900. }
  3901. } else {
  3902. ast_debug(1, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", ast_channel_context(c), ast_channel_exten(c), ast_channel_priority(c), ast_channel_name(c));
  3903. ast_verb(2, "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", ast_channel_context(c), ast_channel_exten(c), ast_channel_priority(c), ast_channel_name(c));
  3904. if ((res == AST_PBX_ERROR)
  3905. && ast_exists_extension(c, ast_channel_context(c), "e", 1,
  3906. S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) {
  3907. /* if we are already on the 'e' exten, don't jump to it again */
  3908. if (!strcmp(ast_channel_exten(c), "e")) {
  3909. ast_verb(2, "Spawn extension (%s, %s, %d) exited ERROR while already on 'e' exten on '%s'\n", ast_channel_context(c), ast_channel_exten(c), ast_channel_priority(c), ast_channel_name(c));
  3910. error = 1;
  3911. } else {
  3912. raise_exception(c, "ERROR", 1);
  3913. continue;
  3914. }
  3915. }
  3916. if (ast_channel_softhangup_internal_flag(c) & AST_SOFTHANGUP_ASYNCGOTO) {
  3917. ast_channel_clear_softhangup(c, AST_SOFTHANGUP_ASYNCGOTO);
  3918. continue;
  3919. }
  3920. if (ast_channel_softhangup_internal_flag(c) & AST_SOFTHANGUP_TIMEOUT) {
  3921. if (ast_exists_extension(c, ast_channel_context(c), "T", 1,
  3922. S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) {
  3923. set_ext_pri(c, "T", 1);
  3924. /* If the AbsoluteTimeout is not reset to 0, we'll get an infinite loop */
  3925. memset(ast_channel_whentohangup(c), 0, sizeof(*ast_channel_whentohangup(c)));
  3926. ast_channel_clear_softhangup(c, AST_SOFTHANGUP_TIMEOUT);
  3927. continue;
  3928. } else if (ast_exists_extension(c, ast_channel_context(c), "e", 1,
  3929. S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) {
  3930. raise_exception(c, "ABSOLUTETIMEOUT", 1);
  3931. /* If the AbsoluteTimeout is not reset to 0, we'll get an infinite loop */
  3932. memset(ast_channel_whentohangup(c), 0, sizeof(*ast_channel_whentohangup(c)));
  3933. ast_channel_clear_softhangup(c, AST_SOFTHANGUP_TIMEOUT);
  3934. continue;
  3935. }
  3936. /* Call timed out with no special extension to jump to. */
  3937. }
  3938. error = 1;
  3939. break;
  3940. }
  3941. }
  3942. if (error)
  3943. break;
  3944. /*!\note
  3945. * We get here on a failure of some kind: non-existing extension or
  3946. * hangup. We have options, here. We can either catch the failure
  3947. * and continue, or we can drop out entirely. */
  3948. if (invalid
  3949. || (ast_strlen_zero(dst_exten) &&
  3950. !ast_exists_extension(c, ast_channel_context(c), ast_channel_exten(c), 1,
  3951. S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL)))) {
  3952. /*!\note
  3953. * If there is no match at priority 1, it is not a valid extension anymore.
  3954. * Try to continue at "i" (for invalid) or "e" (for exception) or exit if
  3955. * neither exist.
  3956. */
  3957. if (ast_exists_extension(c, ast_channel_context(c), "i", 1,
  3958. S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) {
  3959. ast_verb(3, "Channel '%s' sent to invalid extension: context,exten,priority=%s,%s,%d\n",
  3960. ast_channel_name(c), ast_channel_context(c), ast_channel_exten(c), ast_channel_priority(c));
  3961. pbx_builtin_setvar_helper(c, "INVALID_EXTEN", ast_channel_exten(c));
  3962. set_ext_pri(c, "i", 1);
  3963. } else if (ast_exists_extension(c, ast_channel_context(c), "e", 1,
  3964. S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) {
  3965. raise_exception(c, "INVALID", 1);
  3966. } else {
  3967. ast_log(LOG_WARNING, "Channel '%s' sent to invalid extension but no invalid handler: context,exten,priority=%s,%s,%d\n",
  3968. ast_channel_name(c), ast_channel_context(c), ast_channel_exten(c), ast_channel_priority(c));
  3969. error = 1; /* we know what to do with it */
  3970. break;
  3971. }
  3972. } else if (ast_channel_softhangup_internal_flag(c) & AST_SOFTHANGUP_TIMEOUT) {
  3973. /* If we get this far with AST_SOFTHANGUP_TIMEOUT, then we know that the "T" extension is next. */
  3974. ast_channel_clear_softhangup(c, AST_SOFTHANGUP_TIMEOUT);
  3975. } else { /* keypress received, get more digits for a full extension */
  3976. int waittime = 0;
  3977. if (digit)
  3978. waittime = ast_channel_pbx(c)->dtimeoutms;
  3979. else if (!autofallthrough)
  3980. waittime = ast_channel_pbx(c)->rtimeoutms;
  3981. if (!waittime) {
  3982. const char *status = pbx_builtin_getvar_helper(c, "DIALSTATUS");
  3983. if (!status)
  3984. status = "UNKNOWN";
  3985. ast_verb(3, "Auto fallthrough, channel '%s' status is '%s'\n", ast_channel_name(c), status);
  3986. if (!strcasecmp(status, "CONGESTION"))
  3987. res = indicate_congestion(c, "10");
  3988. else if (!strcasecmp(status, "CHANUNAVAIL"))
  3989. res = indicate_congestion(c, "10");
  3990. else if (!strcasecmp(status, "BUSY"))
  3991. res = indicate_busy(c, "10");
  3992. error = 1; /* XXX disable message */
  3993. break; /* exit from the 'for' loop */
  3994. }
  3995. if (collect_digits(c, waittime, dst_exten, sizeof(dst_exten), pos))
  3996. break;
  3997. if (res == AST_PBX_INCOMPLETE && ast_strlen_zero(&dst_exten[pos]))
  3998. timeout = 1;
  3999. if (!timeout
  4000. && ast_exists_extension(c, ast_channel_context(c), dst_exten, 1,
  4001. S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) { /* Prepare the next cycle */
  4002. set_ext_pri(c, dst_exten, 1);
  4003. } else {
  4004. /* No such extension */
  4005. if (!timeout && !ast_strlen_zero(dst_exten)) {
  4006. /* An invalid extension */
  4007. if (ast_exists_extension(c, ast_channel_context(c), "i", 1,
  4008. S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) {
  4009. ast_verb(3, "Invalid extension '%s' in context '%s' on %s\n", dst_exten, ast_channel_context(c), ast_channel_name(c));
  4010. pbx_builtin_setvar_helper(c, "INVALID_EXTEN", dst_exten);
  4011. set_ext_pri(c, "i", 1);
  4012. } else if (ast_exists_extension(c, ast_channel_context(c), "e", 1,
  4013. S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) {
  4014. raise_exception(c, "INVALID", 1);
  4015. } else {
  4016. ast_log(LOG_WARNING,
  4017. "Invalid extension '%s', but no rule 'i' or 'e' in context '%s'\n",
  4018. dst_exten, ast_channel_context(c));
  4019. found = 1; /* XXX disable message */
  4020. break;
  4021. }
  4022. } else {
  4023. /* A simple timeout */
  4024. if (ast_exists_extension(c, ast_channel_context(c), "t", 1,
  4025. S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) {
  4026. ast_verb(3, "Timeout on %s\n", ast_channel_name(c));
  4027. set_ext_pri(c, "t", 1);
  4028. } else if (ast_exists_extension(c, ast_channel_context(c), "e", 1,
  4029. S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) {
  4030. raise_exception(c, "RESPONSETIMEOUT", 1);
  4031. } else {
  4032. ast_log(LOG_WARNING,
  4033. "Timeout, but no rule 't' or 'e' in context '%s'\n",
  4034. ast_channel_context(c));
  4035. found = 1; /* XXX disable message */
  4036. break;
  4037. }
  4038. }
  4039. }
  4040. }
  4041. }
  4042. if (!found && !error) {
  4043. ast_log(LOG_WARNING, "Don't know what to do with '%s'\n", ast_channel_name(c));
  4044. }
  4045. if (!args || !args->no_hangup_chan) {
  4046. ast_softhangup(c, AST_SOFTHANGUP_APPUNLOAD);
  4047. if (!ast_test_flag(ast_channel_flags(c), AST_FLAG_BRIDGE_HANGUP_RUN)
  4048. && ast_exists_extension(c, ast_channel_context(c), "h", 1,
  4049. S_COR(ast_channel_caller(c)->id.number.valid,
  4050. ast_channel_caller(c)->id.number.str, NULL))) {
  4051. ast_pbx_h_exten_run(c, ast_channel_context(c));
  4052. }
  4053. ast_pbx_hangup_handler_run(c);
  4054. }
  4055. ast_channel_lock(c);
  4056. ast_set2_flag(ast_channel_flags(c), autoloopflag, AST_FLAG_IN_AUTOLOOP);
  4057. ast_clear_flag(ast_channel_flags(c), AST_FLAG_BRIDGE_HANGUP_RUN); /* from one round to the next, make sure this gets cleared */
  4058. ast_channel_unlock(c);
  4059. pbx_destroy(ast_channel_pbx(c));
  4060. ast_channel_pbx_set(c, NULL);
  4061. if (!args || !args->no_hangup_chan) {
  4062. ast_hangup(c);
  4063. }
  4064. return AST_PBX_SUCCESS;
  4065. }
  4066. /*!
  4067. * \brief Increase call count for channel
  4068. * \retval 0 on success
  4069. * \retval non-zero if a configured limit (maxcalls, maxload, minmemfree) was reached
  4070. */
  4071. static int increase_call_count(const struct ast_channel *c)
  4072. {
  4073. int failed = 0;
  4074. double curloadavg;
  4075. #if defined(HAVE_SYSINFO)
  4076. long curfreemem;
  4077. struct sysinfo sys_info;
  4078. #endif
  4079. ast_mutex_lock(&maxcalllock);
  4080. if (ast_option_maxcalls) {
  4081. if (countcalls >= ast_option_maxcalls) {
  4082. ast_log(LOG_WARNING, "Maximum call limit of %d calls exceeded by '%s'!\n", ast_option_maxcalls, ast_channel_name(c));
  4083. failed = -1;
  4084. }
  4085. }
  4086. if (ast_option_maxload) {
  4087. getloadavg(&curloadavg, 1);
  4088. if (curloadavg >= ast_option_maxload) {
  4089. ast_log(LOG_WARNING, "Maximum loadavg limit of %f load exceeded by '%s' (currently %f)!\n", ast_option_maxload, ast_channel_name(c), curloadavg);
  4090. failed = -1;
  4091. }
  4092. }
  4093. #if defined(HAVE_SYSINFO)
  4094. if (option_minmemfree) {
  4095. if (!sysinfo(&sys_info)) {
  4096. /* make sure that the free system memory is above the configured low watermark
  4097. * convert the amount of freeram from mem_units to MB */
  4098. curfreemem = sys_info.freeram * sys_info.mem_unit;
  4099. curfreemem /= 1024 * 1024;
  4100. if (curfreemem < option_minmemfree) {
  4101. ast_log(LOG_WARNING, "Available system memory (~%ldMB) is below the configured low watermark (%ldMB)\n", curfreemem, option_minmemfree);
  4102. failed = -1;
  4103. }
  4104. }
  4105. }
  4106. #endif
  4107. if (!failed) {
  4108. countcalls++;
  4109. totalcalls++;
  4110. }
  4111. ast_mutex_unlock(&maxcalllock);
  4112. return failed;
  4113. }
  4114. static void decrease_call_count(void)
  4115. {
  4116. ast_mutex_lock(&maxcalllock);
  4117. if (countcalls > 0)
  4118. countcalls--;
  4119. ast_mutex_unlock(&maxcalllock);
  4120. }
  4121. static void destroy_exten(struct ast_exten *e)
  4122. {
  4123. if (e->priority == PRIORITY_HINT)
  4124. ast_remove_hint(e);
  4125. if (e->peer_table)
  4126. ast_hashtab_destroy(e->peer_table,0);
  4127. if (e->peer_label_table)
  4128. ast_hashtab_destroy(e->peer_label_table, 0);
  4129. if (e->datad)
  4130. e->datad(e->data);
  4131. ast_free(e);
  4132. }
  4133. static void *pbx_thread(void *data)
  4134. {
  4135. /* Oh joyeous kernel, we're a new thread, with nothing to do but
  4136. answer this channel and get it going.
  4137. */
  4138. /* NOTE:
  4139. The launcher of this function _MUST_ increment 'countcalls'
  4140. before invoking the function; it will be decremented when the
  4141. PBX has finished running on the channel
  4142. */
  4143. struct ast_channel *c = data;
  4144. __ast_pbx_run(c, NULL);
  4145. decrease_call_count();
  4146. pthread_exit(NULL);
  4147. return NULL;
  4148. }
  4149. enum ast_pbx_result ast_pbx_start(struct ast_channel *c)
  4150. {
  4151. pthread_t t;
  4152. if (!c) {
  4153. ast_log(LOG_WARNING, "Asked to start thread on NULL channel?\n");
  4154. return AST_PBX_FAILED;
  4155. }
  4156. if (!ast_test_flag(&ast_options, AST_OPT_FLAG_FULLY_BOOTED)) {
  4157. ast_log(LOG_WARNING, "PBX requires Asterisk to be fully booted\n");
  4158. return AST_PBX_FAILED;
  4159. }
  4160. if (increase_call_count(c))
  4161. return AST_PBX_CALL_LIMIT;
  4162. /* Start a new thread, and get something handling this channel. */
  4163. if (ast_pthread_create_detached(&t, NULL, pbx_thread, c)) {
  4164. ast_log(LOG_WARNING, "Failed to create new channel thread\n");
  4165. decrease_call_count();
  4166. return AST_PBX_FAILED;
  4167. }
  4168. return AST_PBX_SUCCESS;
  4169. }
  4170. enum ast_pbx_result ast_pbx_run_args(struct ast_channel *c, struct ast_pbx_args *args)
  4171. {
  4172. enum ast_pbx_result res = AST_PBX_SUCCESS;
  4173. if (!ast_test_flag(&ast_options, AST_OPT_FLAG_FULLY_BOOTED)) {
  4174. ast_log(LOG_WARNING, "PBX requires Asterisk to be fully booted\n");
  4175. return AST_PBX_FAILED;
  4176. }
  4177. if (increase_call_count(c)) {
  4178. return AST_PBX_CALL_LIMIT;
  4179. }
  4180. res = __ast_pbx_run(c, args);
  4181. decrease_call_count();
  4182. return res;
  4183. }
  4184. enum ast_pbx_result ast_pbx_run(struct ast_channel *c)
  4185. {
  4186. return ast_pbx_run_args(c, NULL);
  4187. }
  4188. int ast_active_calls(void)
  4189. {
  4190. return countcalls;
  4191. }
  4192. int ast_processed_calls(void)
  4193. {
  4194. return totalcalls;
  4195. }
  4196. int pbx_set_autofallthrough(int newval)
  4197. {
  4198. int oldval = autofallthrough;
  4199. autofallthrough = newval;
  4200. return oldval;
  4201. }
  4202. int pbx_set_extenpatternmatchnew(int newval)
  4203. {
  4204. int oldval = extenpatternmatchnew;
  4205. extenpatternmatchnew = newval;
  4206. return oldval;
  4207. }
  4208. void pbx_set_overrideswitch(const char *newval)
  4209. {
  4210. if (overrideswitch) {
  4211. ast_free(overrideswitch);
  4212. }
  4213. if (!ast_strlen_zero(newval)) {
  4214. overrideswitch = ast_strdup(newval);
  4215. } else {
  4216. overrideswitch = NULL;
  4217. }
  4218. }
  4219. /*!
  4220. * \brief lookup for a context with a given name,
  4221. * \retval found context or NULL if not found.
  4222. */
  4223. static struct ast_context *find_context(const char *context)
  4224. {
  4225. struct fake_context item;
  4226. ast_copy_string(item.name, context, sizeof(item.name));
  4227. return ast_hashtab_lookup(contexts_table, &item);
  4228. }
  4229. /*!
  4230. * \brief lookup for a context with a given name,
  4231. * \retval with conlock held if found.
  4232. * \retval NULL if not found.
  4233. */
  4234. static struct ast_context *find_context_locked(const char *context)
  4235. {
  4236. struct ast_context *c;
  4237. struct fake_context item;
  4238. ast_copy_string(item.name, context, sizeof(item.name));
  4239. ast_rdlock_contexts();
  4240. c = ast_hashtab_lookup(contexts_table, &item);
  4241. if (!c) {
  4242. ast_unlock_contexts();
  4243. }
  4244. return c;
  4245. }
  4246. /*! \brief Free an ast_include and associated data. */
  4247. static void include_free(struct ast_include *include)
  4248. {
  4249. ast_destroy_timing(&(include->timing));
  4250. ast_free(include);
  4251. }
  4252. /*!
  4253. * \brief Remove included contexts.
  4254. * This function locks contexts list by &conlist, search for the right context
  4255. * structure, leave context list locked and call ast_context_remove_include2
  4256. * which removes include, unlock contexts list and return ...
  4257. */
  4258. int ast_context_remove_include(const char *context, const char *include, const char *registrar)
  4259. {
  4260. int ret = -1;
  4261. struct ast_context *c;
  4262. c = find_context_locked(context);
  4263. if (c) {
  4264. /* found, remove include from this context ... */
  4265. ret = ast_context_remove_include2(c, include, registrar);
  4266. ast_unlock_contexts();
  4267. }
  4268. return ret;
  4269. }
  4270. /*!
  4271. * \brief Locks context, remove included contexts, unlocks context.
  4272. * When we call this function, &conlock lock must be locked, because when
  4273. * we giving *con argument, some process can remove/change this context
  4274. * and after that there can be segfault.
  4275. *
  4276. * \retval 0 on success.
  4277. * \retval -1 on failure.
  4278. */
  4279. int ast_context_remove_include2(struct ast_context *con, const char *include, const char *registrar)
  4280. {
  4281. struct ast_include *i, *pi = NULL;
  4282. int ret = -1;
  4283. ast_wrlock_context(con);
  4284. /* find our include */
  4285. for (i = con->includes; i; pi = i, i = i->next) {
  4286. if (!strcmp(i->name, include) &&
  4287. (!registrar || !strcmp(i->registrar, registrar))) {
  4288. /* remove from list */
  4289. ast_verb(3, "Removing inclusion of context '%s' in context '%s; registrar=%s'\n", include, ast_get_context_name(con), registrar);
  4290. if (pi)
  4291. pi->next = i->next;
  4292. else
  4293. con->includes = i->next;
  4294. /* free include and return */
  4295. include_free(i);
  4296. ret = 0;
  4297. break;
  4298. }
  4299. }
  4300. ast_unlock_context(con);
  4301. return ret;
  4302. }
  4303. /*!
  4304. * \note This function locks contexts list by &conlist, search for the rigt context
  4305. * structure, leave context list locked and call ast_context_remove_switch2
  4306. * which removes switch, unlock contexts list and return ...
  4307. */
  4308. int ast_context_remove_switch(const char *context, const char *sw, const char *data, const char *registrar)
  4309. {
  4310. int ret = -1; /* default error return */
  4311. struct ast_context *c;
  4312. c = find_context_locked(context);
  4313. if (c) {
  4314. /* remove switch from this context ... */
  4315. ret = ast_context_remove_switch2(c, sw, data, registrar);
  4316. ast_unlock_contexts();
  4317. }
  4318. return ret;
  4319. }
  4320. /*!
  4321. * \brief This function locks given context, removes switch, unlock context and
  4322. * return.
  4323. * \note When we call this function, &conlock lock must be locked, because when
  4324. * we giving *con argument, some process can remove/change this context
  4325. * and after that there can be segfault.
  4326. *
  4327. */
  4328. int ast_context_remove_switch2(struct ast_context *con, const char *sw, const char *data, const char *registrar)
  4329. {
  4330. struct ast_sw *i;
  4331. int ret = -1;
  4332. ast_wrlock_context(con);
  4333. /* walk switches */
  4334. AST_LIST_TRAVERSE_SAFE_BEGIN(&con->alts, i, list) {
  4335. if (!strcmp(i->name, sw) && !strcmp(i->data, data) &&
  4336. (!registrar || !strcmp(i->registrar, registrar))) {
  4337. /* found, remove from list */
  4338. ast_verb(3, "Removing switch '%s' from context '%s; registrar=%s'\n", sw, ast_get_context_name(con), registrar);
  4339. AST_LIST_REMOVE_CURRENT(list);
  4340. ast_free(i); /* free switch and return */
  4341. ret = 0;
  4342. break;
  4343. }
  4344. }
  4345. AST_LIST_TRAVERSE_SAFE_END;
  4346. ast_unlock_context(con);
  4347. return ret;
  4348. }
  4349. /*! \note This function will lock conlock. */
  4350. int ast_context_remove_extension(const char *context, const char *extension, int priority, const char *registrar)
  4351. {
  4352. return ast_context_remove_extension_callerid(context, extension, priority, NULL, AST_EXT_MATCHCID_ANY, registrar);
  4353. }
  4354. int ast_context_remove_extension_callerid(const char *context, const char *extension, int priority, const char *callerid, int matchcallerid, const char *registrar)
  4355. {
  4356. int ret = -1; /* default error return */
  4357. struct ast_context *c;
  4358. c = find_context_locked(context);
  4359. if (c) { /* ... remove extension ... */
  4360. ret = ast_context_remove_extension_callerid2(c, extension, priority, callerid,
  4361. matchcallerid, registrar, 0);
  4362. ast_unlock_contexts();
  4363. }
  4364. return ret;
  4365. }
  4366. /*!
  4367. * \brief This functionc locks given context, search for the right extension and
  4368. * fires out all peer in this extensions with given priority. If priority
  4369. * is set to 0, all peers are removed. After that, unlock context and
  4370. * return.
  4371. * \note When do you want to call this function, make sure that &conlock is locked,
  4372. * because some process can handle with your *con context before you lock
  4373. * it.
  4374. *
  4375. */
  4376. int ast_context_remove_extension2(struct ast_context *con, const char *extension, int priority, const char *registrar, int already_locked)
  4377. {
  4378. return ast_context_remove_extension_callerid2(con, extension, priority, NULL, AST_EXT_MATCHCID_ANY, registrar, already_locked);
  4379. }
  4380. int ast_context_remove_extension_callerid2(struct ast_context *con, const char *extension, int priority, const char *callerid, int matchcallerid, const char *registrar, int already_locked)
  4381. {
  4382. struct ast_exten *exten, *prev_exten = NULL;
  4383. struct ast_exten *peer;
  4384. struct ast_exten ex, *exten2, *exten3;
  4385. char dummy_name[1024];
  4386. char dummy_cid[1024];
  4387. struct ast_exten *previous_peer = NULL;
  4388. struct ast_exten *next_peer = NULL;
  4389. int found = 0;
  4390. if (!already_locked)
  4391. ast_wrlock_context(con);
  4392. #ifdef NEED_DEBUG
  4393. ast_verb(3,"Removing %s/%s/%d%s%s from trees, registrar=%s\n", con->name, extension, priority, matchcallerid ? "/" : "", matchcallerid ? callerid : "", registrar);
  4394. #endif
  4395. #ifdef CONTEXT_DEBUG
  4396. check_contexts(__FILE__, __LINE__);
  4397. #endif
  4398. /* find this particular extension */
  4399. ex.exten = dummy_name;
  4400. ext_strncpy(dummy_name, extension, sizeof(dummy_name), 1);
  4401. ex.matchcid = matchcallerid;
  4402. if (callerid) {
  4403. ex.cidmatch = dummy_cid;
  4404. ext_strncpy(dummy_cid, callerid, sizeof(dummy_cid), 1);
  4405. } else {
  4406. ex.cidmatch = NULL;
  4407. }
  4408. exten = ast_hashtab_lookup(con->root_table, &ex);
  4409. if (exten) {
  4410. if (priority == 0) {
  4411. exten2 = ast_hashtab_remove_this_object(con->root_table, exten);
  4412. if (!exten2)
  4413. ast_log(LOG_ERROR,"Trying to delete the exten %s from context %s, but could not remove from the root_table\n", extension, con->name);
  4414. if (con->pattern_tree) {
  4415. struct match_char *x = add_exten_to_pattern_tree(con, exten, 1);
  4416. if (x->exten) { /* this test for safety purposes */
  4417. x->deleted = 1; /* with this marked as deleted, it will never show up in the scoreboard, and therefore never be found */
  4418. x->exten = 0; /* get rid of what will become a bad pointer */
  4419. } else {
  4420. ast_log(LOG_WARNING,"Trying to delete an exten from a context, but the pattern tree node returned isn't a full extension\n");
  4421. }
  4422. }
  4423. } else {
  4424. ex.priority = priority;
  4425. exten2 = ast_hashtab_lookup(exten->peer_table, &ex);
  4426. if (exten2) {
  4427. if (exten2->label) { /* if this exten has a label, remove that, too */
  4428. exten3 = ast_hashtab_remove_this_object(exten->peer_label_table,exten2);
  4429. if (!exten3) {
  4430. ast_log(LOG_ERROR, "Did not remove this priority label (%d/%s) "
  4431. "from the peer_label_table of context %s, extension %s!\n",
  4432. priority, exten2->label, con->name, exten2->name);
  4433. }
  4434. }
  4435. exten3 = ast_hashtab_remove_this_object(exten->peer_table, exten2);
  4436. if (!exten3) {
  4437. ast_log(LOG_ERROR, "Did not remove this priority (%d) from the "
  4438. "peer_table of context %s, extension %s!\n",
  4439. priority, con->name, exten2->name);
  4440. }
  4441. if (exten2 == exten && exten2->peer) {
  4442. exten2 = ast_hashtab_remove_this_object(con->root_table, exten);
  4443. ast_hashtab_insert_immediate(con->root_table, exten2->peer);
  4444. }
  4445. if (ast_hashtab_size(exten->peer_table) == 0) {
  4446. /* well, if the last priority of an exten is to be removed,
  4447. then, the extension is removed, too! */
  4448. exten3 = ast_hashtab_remove_this_object(con->root_table, exten);
  4449. if (!exten3) {
  4450. ast_log(LOG_ERROR, "Did not remove this exten (%s) from the "
  4451. "context root_table (%s) (priority %d)\n",
  4452. exten->name, con->name, priority);
  4453. }
  4454. if (con->pattern_tree) {
  4455. struct match_char *x = add_exten_to_pattern_tree(con, exten, 1);
  4456. if (x->exten) { /* this test for safety purposes */
  4457. x->deleted = 1; /* with this marked as deleted, it will never show up in the scoreboard, and therefore never be found */
  4458. x->exten = 0; /* get rid of what will become a bad pointer */
  4459. }
  4460. }
  4461. }
  4462. } else {
  4463. ast_log(LOG_ERROR,"Could not find priority %d of exten %s in context %s!\n",
  4464. priority, exten->name, con->name);
  4465. }
  4466. }
  4467. } else {
  4468. /* hmmm? this exten is not in this pattern tree? */
  4469. ast_log(LOG_WARNING,"Cannot find extension %s in root_table in context %s\n",
  4470. extension, con->name);
  4471. }
  4472. #ifdef NEED_DEBUG
  4473. if (con->pattern_tree) {
  4474. ast_log(LOG_NOTICE,"match char tree after exten removal:\n");
  4475. log_match_char_tree(con->pattern_tree, " ");
  4476. }
  4477. #endif
  4478. /* scan the extension list to find first matching extension-registrar */
  4479. for (exten = con->root; exten; prev_exten = exten, exten = exten->next) {
  4480. if (!strcmp(exten->exten, ex.exten) &&
  4481. (!matchcallerid ||
  4482. (!ast_strlen_zero(ex.cidmatch) && !ast_strlen_zero(exten->cidmatch) && !strcmp(exten->cidmatch, ex.cidmatch)) ||
  4483. (ast_strlen_zero(ex.cidmatch) && ast_strlen_zero(exten->cidmatch)))) {
  4484. break;
  4485. }
  4486. }
  4487. if (!exten) {
  4488. /* we can't find right extension */
  4489. if (!already_locked)
  4490. ast_unlock_context(con);
  4491. return -1;
  4492. }
  4493. /* scan the priority list to remove extension with exten->priority == priority */
  4494. for (peer = exten, next_peer = exten->peer ? exten->peer : exten->next;
  4495. peer && !strcmp(peer->exten, ex.exten) &&
  4496. (!callerid || (!matchcallerid && !peer->matchcid) || (matchcallerid && peer->matchcid && !strcmp(peer->cidmatch, ex.cidmatch))) ;
  4497. peer = next_peer, next_peer = next_peer ? (next_peer->peer ? next_peer->peer : next_peer->next) : NULL) {
  4498. if ((priority == 0 || peer->priority == priority) &&
  4499. (!registrar || !strcmp(peer->registrar, registrar) )) {
  4500. found = 1;
  4501. /* we are first priority extension? */
  4502. if (!previous_peer) {
  4503. /*
  4504. * We are first in the priority chain, so must update the extension chain.
  4505. * The next node is either the next priority or the next extension
  4506. */
  4507. struct ast_exten *next_node = peer->peer ? peer->peer : peer->next;
  4508. if (peer->peer) {
  4509. /* move the peer_table and peer_label_table down to the next peer, if
  4510. it is there */
  4511. peer->peer->peer_table = peer->peer_table;
  4512. peer->peer->peer_label_table = peer->peer_label_table;
  4513. peer->peer_table = NULL;
  4514. peer->peer_label_table = NULL;
  4515. }
  4516. if (!prev_exten) { /* change the root... */
  4517. con->root = next_node;
  4518. } else {
  4519. prev_exten->next = next_node; /* unlink */
  4520. }
  4521. if (peer->peer) { /* update the new head of the pri list */
  4522. peer->peer->next = peer->next;
  4523. }
  4524. } else { /* easy, we are not first priority in extension */
  4525. previous_peer->peer = peer->peer;
  4526. }
  4527. /* now, free whole priority extension */
  4528. destroy_exten(peer);
  4529. } else {
  4530. previous_peer = peer;
  4531. }
  4532. }
  4533. if (!already_locked)
  4534. ast_unlock_context(con);
  4535. return found ? 0 : -1;
  4536. }
  4537. /*!
  4538. * \note This function locks contexts list by &conlist, searches for the right context
  4539. * structure, and locks the macrolock mutex in that context.
  4540. * macrolock is used to limit a macro to be executed by one call at a time.
  4541. * \param context The context
  4542. */
  4543. int ast_context_lockmacro(const char *context)
  4544. {
  4545. struct ast_context *c;
  4546. int ret = -1;
  4547. c = find_context_locked(context);
  4548. if (c) {
  4549. ast_unlock_contexts();
  4550. /* if we found context, lock macrolock */
  4551. ret = ast_mutex_lock(&c->macrolock);
  4552. }
  4553. return ret;
  4554. }
  4555. /*!
  4556. * \note This function locks contexts list by &conlist, searches for the right context
  4557. * structure, and unlocks the macrolock mutex in that context.
  4558. * macrolock is used to limit a macro to be executed by one call at a time.
  4559. * \param context The context
  4560. */
  4561. int ast_context_unlockmacro(const char *context)
  4562. {
  4563. struct ast_context *c;
  4564. int ret = -1;
  4565. c = find_context_locked(context);
  4566. if (c) {
  4567. ast_unlock_contexts();
  4568. /* if we found context, unlock macrolock */
  4569. ret = ast_mutex_unlock(&c->macrolock);
  4570. }
  4571. return ret;
  4572. }
  4573. /*
  4574. * Help for CLI commands ...
  4575. */
  4576. /*! \brief handle_show_hints: CLI support for listing registered dial plan hints */
  4577. static char *handle_show_hints(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4578. {
  4579. struct ast_hint *hint;
  4580. int num = 0;
  4581. int watchers;
  4582. struct ao2_iterator i;
  4583. char buf[AST_MAX_EXTENSION+AST_MAX_CONTEXT+2];
  4584. switch (cmd) {
  4585. case CLI_INIT:
  4586. e->command = "core show hints";
  4587. e->usage =
  4588. "Usage: core show hints\n"
  4589. " List registered hints.\n"
  4590. " Hint details are shown in five columns. In order from left to right, they are:\n"
  4591. " 1. Hint extension URI.\n"
  4592. " 2. List of mapped device or presence state identifiers.\n"
  4593. " 3. Current extension state. The aggregate of mapped device states.\n"
  4594. " 4. Current presence state for the mapped presence state provider.\n"
  4595. " 5. Watchers - number of subscriptions and other entities watching this hint.\n";
  4596. return NULL;
  4597. case CLI_GENERATE:
  4598. return NULL;
  4599. }
  4600. if (ao2_container_count(hints) == 0) {
  4601. ast_cli(a->fd, "There are no registered dialplan hints\n");
  4602. return CLI_SUCCESS;
  4603. }
  4604. /* ... we have hints ... */
  4605. ast_cli(a->fd, "\n -= Registered Asterisk Dial Plan Hints =-\n");
  4606. i = ao2_iterator_init(hints, 0);
  4607. for (; (hint = ao2_iterator_next(&i)); ao2_ref(hint, -1)) {
  4608. ao2_lock(hint);
  4609. if (!hint->exten) {
  4610. /* The extension has already been destroyed */
  4611. ao2_unlock(hint);
  4612. continue;
  4613. }
  4614. watchers = ao2_container_count(hint->callbacks);
  4615. snprintf(buf, sizeof(buf), "%s@%s",
  4616. ast_get_extension_name(hint->exten),
  4617. ast_get_context_name(ast_get_extension_context(hint->exten)));
  4618. ast_cli(a->fd, "%-20.20s: %-20.20s State:%-15.15s Presence:%-15.15s Watchers %2d\n",
  4619. buf,
  4620. ast_get_extension_app(hint->exten),
  4621. ast_extension_state2str(hint->laststate),
  4622. ast_presence_state2str(hint->last_presence_state),
  4623. watchers);
  4624. ao2_unlock(hint);
  4625. num++;
  4626. }
  4627. ao2_iterator_destroy(&i);
  4628. ast_cli(a->fd, "----------------\n");
  4629. ast_cli(a->fd, "- %d hints registered\n", num);
  4630. return CLI_SUCCESS;
  4631. }
  4632. /*! \brief autocomplete for CLI command 'core show hint' */
  4633. static char *complete_core_show_hint(const char *line, const char *word, int pos, int state)
  4634. {
  4635. struct ast_hint *hint;
  4636. char *ret = NULL;
  4637. int which = 0;
  4638. int wordlen;
  4639. struct ao2_iterator i;
  4640. if (pos != 3)
  4641. return NULL;
  4642. wordlen = strlen(word);
  4643. /* walk through all hints */
  4644. i = ao2_iterator_init(hints, 0);
  4645. for (; (hint = ao2_iterator_next(&i)); ao2_ref(hint, -1)) {
  4646. ao2_lock(hint);
  4647. if (!hint->exten) {
  4648. /* The extension has already been destroyed */
  4649. ao2_unlock(hint);
  4650. continue;
  4651. }
  4652. if (!strncasecmp(word, ast_get_extension_name(hint->exten), wordlen) && ++which > state) {
  4653. ret = ast_strdup(ast_get_extension_name(hint->exten));
  4654. ao2_unlock(hint);
  4655. ao2_ref(hint, -1);
  4656. break;
  4657. }
  4658. ao2_unlock(hint);
  4659. }
  4660. ao2_iterator_destroy(&i);
  4661. return ret;
  4662. }
  4663. /*! \brief handle_show_hint: CLI support for listing registered dial plan hint */
  4664. static char *handle_show_hint(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4665. {
  4666. struct ast_hint *hint;
  4667. int watchers;
  4668. int num = 0, extenlen;
  4669. struct ao2_iterator i;
  4670. char buf[AST_MAX_EXTENSION+AST_MAX_CONTEXT+2];
  4671. switch (cmd) {
  4672. case CLI_INIT:
  4673. e->command = "core show hint";
  4674. e->usage =
  4675. "Usage: core show hint <exten>\n"
  4676. " List registered hint.\n"
  4677. " Hint details are shown in five columns. In order from left to right, they are:\n"
  4678. " 1. Hint extension URI.\n"
  4679. " 2. List of mapped device or presence state identifiers.\n"
  4680. " 3. Current extension state. The aggregate of mapped device states.\n"
  4681. " 4. Current presence state for the mapped presence state provider.\n"
  4682. " 5. Watchers - number of subscriptions and other entities watching this hint.\n";
  4683. return NULL;
  4684. case CLI_GENERATE:
  4685. return complete_core_show_hint(a->line, a->word, a->pos, a->n);
  4686. }
  4687. if (a->argc < 4)
  4688. return CLI_SHOWUSAGE;
  4689. if (ao2_container_count(hints) == 0) {
  4690. ast_cli(a->fd, "There are no registered dialplan hints\n");
  4691. return CLI_SUCCESS;
  4692. }
  4693. extenlen = strlen(a->argv[3]);
  4694. i = ao2_iterator_init(hints, 0);
  4695. for (; (hint = ao2_iterator_next(&i)); ao2_ref(hint, -1)) {
  4696. ao2_lock(hint);
  4697. if (!hint->exten) {
  4698. /* The extension has already been destroyed */
  4699. ao2_unlock(hint);
  4700. continue;
  4701. }
  4702. if (!strncasecmp(ast_get_extension_name(hint->exten), a->argv[3], extenlen)) {
  4703. watchers = ao2_container_count(hint->callbacks);
  4704. sprintf(buf, "%s@%s",
  4705. ast_get_extension_name(hint->exten),
  4706. ast_get_context_name(ast_get_extension_context(hint->exten)));
  4707. ast_cli(a->fd, "%-20.20s: %-20.20s State:%-15.15s Presence:%-15.15s Watchers %2d\n",
  4708. buf,
  4709. ast_get_extension_app(hint->exten),
  4710. ast_extension_state2str(hint->laststate),
  4711. ast_presence_state2str(hint->last_presence_state),
  4712. watchers);
  4713. num++;
  4714. }
  4715. ao2_unlock(hint);
  4716. }
  4717. ao2_iterator_destroy(&i);
  4718. if (!num)
  4719. ast_cli(a->fd, "No hints matching extension %s\n", a->argv[3]);
  4720. else
  4721. ast_cli(a->fd, "%d hint%s matching extension %s\n", num, (num!=1 ? "s":""), a->argv[3]);
  4722. return CLI_SUCCESS;
  4723. }
  4724. #if 0
  4725. /* This code can be used to test if the system survives running out of memory.
  4726. * It might be an idea to put this in only if ENABLE_AUTODESTRUCT_TESTS is enabled.
  4727. *
  4728. * If you want to test this, these Linux sysctl flags might be appropriate:
  4729. * vm.overcommit_memory = 2
  4730. * vm.swappiness = 0
  4731. *
  4732. * <@Corydon76-home> I envision 'core eat disk space' and 'core eat file descriptors' now
  4733. * <@mjordan> egads
  4734. * <@mjordan> it's literally the 'big red' auto-destruct button
  4735. * <@mjordan> if you were wondering who even builds such a thing.... well, now you know
  4736. * ...
  4737. * <@Corydon76-home> What about if they lived only if you defined TEST_FRAMEWORK? Shouldn't have those on production machines
  4738. * <@mjordan> I think accompanied with an update to one of our README files that "no, really, TEST_FRAMEWORK isn't for you", I'd be fine
  4739. */
  4740. static char *handle_eat_memory(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4741. {
  4742. void **blocks;
  4743. int blocks_pos = 0;
  4744. const int blocks_max = 50000;
  4745. long long int allocated = 0;
  4746. int sizes[] = {
  4747. 100 * 1024 * 1024,
  4748. 100 * 1024,
  4749. 2 * 1024,
  4750. 400,
  4751. 0
  4752. };
  4753. int i;
  4754. switch (cmd) {
  4755. case CLI_INIT:
  4756. /* To do: add method to free memory again? 5 minutes? */
  4757. e->command = "core eat memory";
  4758. e->usage =
  4759. "Usage: core eat memory\n"
  4760. " Eats all available memory so you can test if the system survives\n";
  4761. return NULL;
  4762. case CLI_GENERATE:
  4763. return NULL;
  4764. }
  4765. blocks = ast_malloc(sizeof(void*) * blocks_max);
  4766. if (!blocks) {
  4767. ast_log(LOG_ERROR, "Already out of mem?\n");
  4768. return CLI_SUCCESS;
  4769. }
  4770. for (i = 0; sizes[i]; ++i) {
  4771. int alloc_size = sizes[i];
  4772. ast_log(LOG_WARNING, "Allocating %d sized blocks (got %d blocks already)\n", alloc_size, blocks_pos);
  4773. while (1) {
  4774. void *block;
  4775. if (blocks_pos >= blocks_max) {
  4776. ast_log(LOG_ERROR, "Memory buffer too small? Run me again :)\n");
  4777. break;
  4778. }
  4779. block = ast_malloc(alloc_size);
  4780. if (!block) {
  4781. break;
  4782. }
  4783. blocks[blocks_pos++] = block;
  4784. allocated += alloc_size;
  4785. }
  4786. }
  4787. /* No freeing of the mem! */
  4788. ast_log(LOG_WARNING, "Allocated %lld bytes total!\n", allocated);
  4789. return CLI_SUCCESS;
  4790. }
  4791. #endif
  4792. /*
  4793. * 'show dialplan' CLI command implementation functions ...
  4794. */
  4795. static char *complete_show_dialplan_context(const char *line, const char *word, int pos,
  4796. int state)
  4797. {
  4798. struct ast_context *c = NULL;
  4799. char *ret = NULL;
  4800. int which = 0;
  4801. int wordlen;
  4802. /* we are do completion of [exten@]context on second position only */
  4803. if (pos != 2)
  4804. return NULL;
  4805. ast_rdlock_contexts();
  4806. wordlen = strlen(word);
  4807. /* walk through all contexts and return the n-th match */
  4808. while ( (c = ast_walk_contexts(c)) ) {
  4809. if (!strncasecmp(word, ast_get_context_name(c), wordlen) && ++which > state) {
  4810. ret = ast_strdup(ast_get_context_name(c));
  4811. break;
  4812. }
  4813. }
  4814. ast_unlock_contexts();
  4815. return ret;
  4816. }
  4817. /*! \brief Counters for the show dialplan manager command */
  4818. struct dialplan_counters {
  4819. int total_items;
  4820. int total_context;
  4821. int total_exten;
  4822. int total_prio;
  4823. int context_existence;
  4824. int extension_existence;
  4825. };
  4826. /*! \brief helper function to print an extension */
  4827. static void print_ext(struct ast_exten *e, char * buf, int buflen)
  4828. {
  4829. int prio = ast_get_extension_priority(e);
  4830. if (prio == PRIORITY_HINT) {
  4831. snprintf(buf, buflen, "hint: %s",
  4832. ast_get_extension_app(e));
  4833. } else {
  4834. snprintf(buf, buflen, "%d. %s(%s)",
  4835. prio, ast_get_extension_app(e),
  4836. (!ast_strlen_zero(ast_get_extension_app_data(e)) ? (char *)ast_get_extension_app_data(e) : ""));
  4837. }
  4838. }
  4839. /* XXX not verified */
  4840. static int show_dialplan_helper(int fd, const char *context, const char *exten, struct dialplan_counters *dpc, struct ast_include *rinclude, int includecount, const char *includes[])
  4841. {
  4842. struct ast_context *c = NULL;
  4843. int res = 0, old_total_exten = dpc->total_exten;
  4844. ast_rdlock_contexts();
  4845. /* walk all contexts ... */
  4846. while ( (c = ast_walk_contexts(c)) ) {
  4847. struct ast_exten *e;
  4848. struct ast_include *i;
  4849. struct ast_ignorepat *ip;
  4850. #ifndef LOW_MEMORY
  4851. char buf[1024], buf2[1024];
  4852. #else
  4853. char buf[256], buf2[256];
  4854. #endif
  4855. int context_info_printed = 0;
  4856. if (context && strcmp(ast_get_context_name(c), context))
  4857. continue; /* skip this one, name doesn't match */
  4858. dpc->context_existence = 1;
  4859. ast_rdlock_context(c);
  4860. /* are we looking for exten too? if yes, we print context
  4861. * only if we find our extension.
  4862. * Otherwise print context even if empty ?
  4863. * XXX i am not sure how the rinclude is handled.
  4864. * I think it ought to go inside.
  4865. */
  4866. if (!exten) {
  4867. dpc->total_context++;
  4868. ast_cli(fd, "[ Context '%s' created by '%s' ]\n",
  4869. ast_get_context_name(c), ast_get_context_registrar(c));
  4870. context_info_printed = 1;
  4871. }
  4872. /* walk extensions ... */
  4873. e = NULL;
  4874. while ( (e = ast_walk_context_extensions(c, e)) ) {
  4875. struct ast_exten *p;
  4876. if (exten && !ast_extension_match(ast_get_extension_name(e), exten))
  4877. continue; /* skip, extension match failed */
  4878. dpc->extension_existence = 1;
  4879. /* may we print context info? */
  4880. if (!context_info_printed) {
  4881. dpc->total_context++;
  4882. if (rinclude) { /* TODO Print more info about rinclude */
  4883. ast_cli(fd, "[ Included context '%s' created by '%s' ]\n",
  4884. ast_get_context_name(c), ast_get_context_registrar(c));
  4885. } else {
  4886. ast_cli(fd, "[ Context '%s' created by '%s' ]\n",
  4887. ast_get_context_name(c), ast_get_context_registrar(c));
  4888. }
  4889. context_info_printed = 1;
  4890. }
  4891. dpc->total_prio++;
  4892. /* write extension name and first peer */
  4893. if (e->matchcid == AST_EXT_MATCHCID_ON)
  4894. snprintf(buf, sizeof(buf), "'%s' (CID match '%s') => ", ast_get_extension_name(e), e->cidmatch);
  4895. else
  4896. snprintf(buf, sizeof(buf), "'%s' =>", ast_get_extension_name(e));
  4897. print_ext(e, buf2, sizeof(buf2));
  4898. ast_cli(fd, " %-17s %-45s [%s]\n", buf, buf2,
  4899. ast_get_extension_registrar(e));
  4900. dpc->total_exten++;
  4901. /* walk next extension peers */
  4902. p = e; /* skip the first one, we already got it */
  4903. while ( (p = ast_walk_extension_priorities(e, p)) ) {
  4904. const char *el = ast_get_extension_label(p);
  4905. dpc->total_prio++;
  4906. if (el)
  4907. snprintf(buf, sizeof(buf), " [%s]", el);
  4908. else
  4909. buf[0] = '\0';
  4910. print_ext(p, buf2, sizeof(buf2));
  4911. ast_cli(fd," %-17s %-45s [%s]\n", buf, buf2,
  4912. ast_get_extension_registrar(p));
  4913. }
  4914. }
  4915. /* walk included and write info ... */
  4916. i = NULL;
  4917. while ( (i = ast_walk_context_includes(c, i)) ) {
  4918. snprintf(buf, sizeof(buf), "'%s'", ast_get_include_name(i));
  4919. if (exten) {
  4920. /* Check all includes for the requested extension */
  4921. if (includecount >= AST_PBX_MAX_STACK) {
  4922. ast_log(LOG_WARNING, "Maximum include depth exceeded!\n");
  4923. } else {
  4924. int dupe = 0;
  4925. int x;
  4926. for (x = 0; x < includecount; x++) {
  4927. if (!strcasecmp(includes[x], ast_get_include_name(i))) {
  4928. dupe++;
  4929. break;
  4930. }
  4931. }
  4932. if (!dupe) {
  4933. includes[includecount] = ast_get_include_name(i);
  4934. show_dialplan_helper(fd, ast_get_include_name(i), exten, dpc, i, includecount + 1, includes);
  4935. } else {
  4936. ast_log(LOG_WARNING, "Avoiding circular include of %s within %s\n", ast_get_include_name(i), context);
  4937. }
  4938. }
  4939. } else {
  4940. ast_cli(fd, " Include => %-45s [%s]\n",
  4941. buf, ast_get_include_registrar(i));
  4942. }
  4943. }
  4944. /* walk ignore patterns and write info ... */
  4945. ip = NULL;
  4946. while ( (ip = ast_walk_context_ignorepats(c, ip)) ) {
  4947. const char *ipname = ast_get_ignorepat_name(ip);
  4948. char ignorepat[AST_MAX_EXTENSION];
  4949. snprintf(buf, sizeof(buf), "'%s'", ipname);
  4950. snprintf(ignorepat, sizeof(ignorepat), "_%s.", ipname);
  4951. if (!exten || ast_extension_match(ignorepat, exten)) {
  4952. ast_cli(fd, " Ignore pattern => %-45s [%s]\n",
  4953. buf, ast_get_ignorepat_registrar(ip));
  4954. }
  4955. }
  4956. if (!rinclude) {
  4957. struct ast_sw *sw = NULL;
  4958. while ( (sw = ast_walk_context_switches(c, sw)) ) {
  4959. snprintf(buf, sizeof(buf), "'%s/%s'",
  4960. ast_get_switch_name(sw),
  4961. ast_get_switch_data(sw));
  4962. ast_cli(fd, " Alt. Switch => %-45s [%s]\n",
  4963. buf, ast_get_switch_registrar(sw));
  4964. }
  4965. }
  4966. ast_unlock_context(c);
  4967. /* if we print something in context, make an empty line */
  4968. if (context_info_printed)
  4969. ast_cli(fd, "\n");
  4970. }
  4971. ast_unlock_contexts();
  4972. return (dpc->total_exten == old_total_exten) ? -1 : res;
  4973. }
  4974. static int show_debug_helper(int fd, const char *context, const char *exten, struct dialplan_counters *dpc, struct ast_include *rinclude, int includecount, const char *includes[])
  4975. {
  4976. struct ast_context *c = NULL;
  4977. int res = 0, old_total_exten = dpc->total_exten;
  4978. ast_cli(fd,"\n In-mem exten Trie for Fast Extension Pattern Matching:\n\n");
  4979. ast_cli(fd,"\n Explanation: Node Contents Format = <char(s) to match>:<pattern?>:<specif>:[matched extension]\n");
  4980. ast_cli(fd, " Where <char(s) to match> is a set of chars, any one of which should match the current character\n");
  4981. ast_cli(fd, " <pattern?>: Y if this a pattern match (eg. _XZN[5-7]), N otherwise\n");
  4982. ast_cli(fd, " <specif>: an assigned 'exactness' number for this matching char. The lower the number, the more exact the match\n");
  4983. ast_cli(fd, " [matched exten]: If all chars matched to this point, which extension this matches. In form: EXTEN:<exten string>\n");
  4984. ast_cli(fd, " In general, you match a trie node to a string character, from left to right. All possible matching chars\n");
  4985. ast_cli(fd, " are in a string vertically, separated by an unbroken string of '+' characters.\n\n");
  4986. ast_rdlock_contexts();
  4987. /* walk all contexts ... */
  4988. while ( (c = ast_walk_contexts(c)) ) {
  4989. int context_info_printed = 0;
  4990. if (context && strcmp(ast_get_context_name(c), context))
  4991. continue; /* skip this one, name doesn't match */
  4992. dpc->context_existence = 1;
  4993. if (!c->pattern_tree) {
  4994. /* Ignore check_return warning from Coverity for ast_exists_extension below */
  4995. ast_exists_extension(NULL, c->name, "s", 1, ""); /* do this to force the trie to built, if it is not already */
  4996. }
  4997. ast_rdlock_context(c);
  4998. dpc->total_context++;
  4999. ast_cli(fd, "[ Context '%s' created by '%s' ]\n",
  5000. ast_get_context_name(c), ast_get_context_registrar(c));
  5001. context_info_printed = 1;
  5002. if (c->pattern_tree)
  5003. {
  5004. cli_match_char_tree(c->pattern_tree, " ", fd);
  5005. } else {
  5006. ast_cli(fd,"\n No Pattern Trie present. Perhaps the context is empty...or there is trouble...\n\n");
  5007. }
  5008. ast_unlock_context(c);
  5009. /* if we print something in context, make an empty line */
  5010. if (context_info_printed)
  5011. ast_cli(fd, "\n");
  5012. }
  5013. ast_unlock_contexts();
  5014. return (dpc->total_exten == old_total_exten) ? -1 : res;
  5015. }
  5016. static char *handle_show_dialplan(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  5017. {
  5018. char *exten = NULL, *context = NULL;
  5019. /* Variables used for different counters */
  5020. struct dialplan_counters counters;
  5021. const char *incstack[AST_PBX_MAX_STACK];
  5022. switch (cmd) {
  5023. case CLI_INIT:
  5024. e->command = "dialplan show";
  5025. e->usage =
  5026. "Usage: dialplan show [[exten@]context]\n"
  5027. " Show dialplan\n";
  5028. return NULL;
  5029. case CLI_GENERATE:
  5030. return complete_show_dialplan_context(a->line, a->word, a->pos, a->n);
  5031. }
  5032. memset(&counters, 0, sizeof(counters));
  5033. if (a->argc != 2 && a->argc != 3)
  5034. return CLI_SHOWUSAGE;
  5035. /* we obtain [exten@]context? if yes, split them ... */
  5036. if (a->argc == 3) {
  5037. if (strchr(a->argv[2], '@')) { /* split into exten & context */
  5038. context = ast_strdupa(a->argv[2]);
  5039. exten = strsep(&context, "@");
  5040. /* change empty strings to NULL */
  5041. if (ast_strlen_zero(exten))
  5042. exten = NULL;
  5043. } else { /* no '@' char, only context given */
  5044. context = ast_strdupa(a->argv[2]);
  5045. }
  5046. if (ast_strlen_zero(context))
  5047. context = NULL;
  5048. }
  5049. /* else Show complete dial plan, context and exten are NULL */
  5050. show_dialplan_helper(a->fd, context, exten, &counters, NULL, 0, incstack);
  5051. /* check for input failure and throw some error messages */
  5052. if (context && !counters.context_existence) {
  5053. ast_cli(a->fd, "There is no existence of '%s' context\n", context);
  5054. return CLI_FAILURE;
  5055. }
  5056. if (exten && !counters.extension_existence) {
  5057. if (context)
  5058. ast_cli(a->fd, "There is no existence of %s@%s extension\n",
  5059. exten, context);
  5060. else
  5061. ast_cli(a->fd,
  5062. "There is no existence of '%s' extension in all contexts\n",
  5063. exten);
  5064. return CLI_FAILURE;
  5065. }
  5066. ast_cli(a->fd,"-= %d %s (%d %s) in %d %s. =-\n",
  5067. counters.total_exten, counters.total_exten == 1 ? "extension" : "extensions",
  5068. counters.total_prio, counters.total_prio == 1 ? "priority" : "priorities",
  5069. counters.total_context, counters.total_context == 1 ? "context" : "contexts");
  5070. /* everything ok */
  5071. return CLI_SUCCESS;
  5072. }
  5073. /*! \brief Send ack once */
  5074. static char *handle_debug_dialplan(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  5075. {
  5076. char *exten = NULL, *context = NULL;
  5077. /* Variables used for different counters */
  5078. struct dialplan_counters counters;
  5079. const char *incstack[AST_PBX_MAX_STACK];
  5080. switch (cmd) {
  5081. case CLI_INIT:
  5082. e->command = "dialplan debug";
  5083. e->usage =
  5084. "Usage: dialplan debug [context]\n"
  5085. " Show dialplan context Trie(s). Usually only useful to folks debugging the deep internals of the fast pattern matcher\n";
  5086. return NULL;
  5087. case CLI_GENERATE:
  5088. return complete_show_dialplan_context(a->line, a->word, a->pos, a->n);
  5089. }
  5090. memset(&counters, 0, sizeof(counters));
  5091. if (a->argc != 2 && a->argc != 3)
  5092. return CLI_SHOWUSAGE;
  5093. /* we obtain [exten@]context? if yes, split them ... */
  5094. /* note: we ignore the exten totally here .... */
  5095. if (a->argc == 3) {
  5096. if (strchr(a->argv[2], '@')) { /* split into exten & context */
  5097. context = ast_strdupa(a->argv[2]);
  5098. exten = strsep(&context, "@");
  5099. /* change empty strings to NULL */
  5100. if (ast_strlen_zero(exten))
  5101. exten = NULL;
  5102. } else { /* no '@' char, only context given */
  5103. context = ast_strdupa(a->argv[2]);
  5104. }
  5105. if (ast_strlen_zero(context))
  5106. context = NULL;
  5107. }
  5108. /* else Show complete dial plan, context and exten are NULL */
  5109. show_debug_helper(a->fd, context, exten, &counters, NULL, 0, incstack);
  5110. /* check for input failure and throw some error messages */
  5111. if (context && !counters.context_existence) {
  5112. ast_cli(a->fd, "There is no existence of '%s' context\n", context);
  5113. return CLI_FAILURE;
  5114. }
  5115. ast_cli(a->fd,"-= %d %s. =-\n",
  5116. counters.total_context, counters.total_context == 1 ? "context" : "contexts");
  5117. /* everything ok */
  5118. return CLI_SUCCESS;
  5119. }
  5120. /*! \brief Send ack once */
  5121. static void manager_dpsendack(struct mansession *s, const struct message *m)
  5122. {
  5123. astman_send_listack(s, m, "DialPlan list will follow", "start");
  5124. }
  5125. /*! \brief Show dialplan extensions
  5126. * XXX this function is similar but not exactly the same as the CLI's
  5127. * show dialplan. Must check whether the difference is intentional or not.
  5128. */
  5129. static int manager_show_dialplan_helper(struct mansession *s, const struct message *m,
  5130. const char *actionidtext, const char *context,
  5131. const char *exten, struct dialplan_counters *dpc,
  5132. struct ast_include *rinclude,
  5133. int includecount, const char *includes[])
  5134. {
  5135. struct ast_context *c;
  5136. int res = 0, old_total_exten = dpc->total_exten;
  5137. if (ast_strlen_zero(exten))
  5138. exten = NULL;
  5139. if (ast_strlen_zero(context))
  5140. context = NULL;
  5141. ast_debug(3, "manager_show_dialplan: Context: -%s- Extension: -%s-\n", context, exten);
  5142. /* try to lock contexts */
  5143. if (ast_rdlock_contexts()) {
  5144. astman_send_error(s, m, "Failed to lock contexts");
  5145. ast_log(LOG_WARNING, "Failed to lock contexts list for manager: listdialplan\n");
  5146. return -1;
  5147. }
  5148. c = NULL; /* walk all contexts ... */
  5149. while ( (c = ast_walk_contexts(c)) ) {
  5150. struct ast_exten *e;
  5151. struct ast_include *i;
  5152. struct ast_ignorepat *ip;
  5153. if (context && strcmp(ast_get_context_name(c), context) != 0)
  5154. continue; /* not the name we want */
  5155. dpc->context_existence = 1;
  5156. dpc->total_context++;
  5157. ast_debug(3, "manager_show_dialplan: Found Context: %s \n", ast_get_context_name(c));
  5158. if (ast_rdlock_context(c)) { /* failed to lock */
  5159. ast_debug(3, "manager_show_dialplan: Failed to lock context\n");
  5160. continue;
  5161. }
  5162. /* XXX note- an empty context is not printed */
  5163. e = NULL; /* walk extensions in context */
  5164. while ( (e = ast_walk_context_extensions(c, e)) ) {
  5165. struct ast_exten *p;
  5166. /* looking for extension? is this our extension? */
  5167. if (exten && !ast_extension_match(ast_get_extension_name(e), exten)) {
  5168. /* not the one we are looking for, continue */
  5169. ast_debug(3, "manager_show_dialplan: Skipping extension %s\n", ast_get_extension_name(e));
  5170. continue;
  5171. }
  5172. ast_debug(3, "manager_show_dialplan: Found Extension: %s \n", ast_get_extension_name(e));
  5173. dpc->extension_existence = 1;
  5174. dpc->total_exten++;
  5175. p = NULL; /* walk next extension peers */
  5176. while ( (p = ast_walk_extension_priorities(e, p)) ) {
  5177. int prio = ast_get_extension_priority(p);
  5178. dpc->total_prio++;
  5179. if (!dpc->total_items++)
  5180. manager_dpsendack(s, m);
  5181. astman_append(s, "Event: ListDialplan\r\n%s", actionidtext);
  5182. astman_append(s, "Context: %s\r\nExtension: %s\r\n", ast_get_context_name(c), ast_get_extension_name(e) );
  5183. /* XXX maybe make this conditional, if p != e ? */
  5184. if (ast_get_extension_label(p))
  5185. astman_append(s, "ExtensionLabel: %s\r\n", ast_get_extension_label(p));
  5186. if (prio == PRIORITY_HINT) {
  5187. astman_append(s, "Priority: hint\r\nApplication: %s\r\n", ast_get_extension_app(p));
  5188. } else {
  5189. astman_append(s, "Priority: %d\r\nApplication: %s\r\nAppData: %s\r\n", prio, ast_get_extension_app(p), (char *) ast_get_extension_app_data(p));
  5190. }
  5191. astman_append(s, "Registrar: %s\r\n\r\n", ast_get_extension_registrar(e));
  5192. }
  5193. }
  5194. i = NULL; /* walk included and write info ... */
  5195. while ( (i = ast_walk_context_includes(c, i)) ) {
  5196. if (exten) {
  5197. /* Check all includes for the requested extension */
  5198. if (includecount >= AST_PBX_MAX_STACK) {
  5199. ast_log(LOG_WARNING, "Maximum include depth exceeded!\n");
  5200. } else {
  5201. int dupe = 0;
  5202. int x;
  5203. for (x = 0; x < includecount; x++) {
  5204. if (!strcasecmp(includes[x], ast_get_include_name(i))) {
  5205. dupe++;
  5206. break;
  5207. }
  5208. }
  5209. if (!dupe) {
  5210. includes[includecount] = ast_get_include_name(i);
  5211. manager_show_dialplan_helper(s, m, actionidtext, ast_get_include_name(i), exten, dpc, i, includecount + 1, includes);
  5212. } else {
  5213. ast_log(LOG_WARNING, "Avoiding circular include of %s within %s\n", ast_get_include_name(i), context);
  5214. }
  5215. }
  5216. } else {
  5217. if (!dpc->total_items++)
  5218. manager_dpsendack(s, m);
  5219. astman_append(s, "Event: ListDialplan\r\n%s", actionidtext);
  5220. astman_append(s, "Context: %s\r\nIncludeContext: %s\r\nRegistrar: %s\r\n", ast_get_context_name(c), ast_get_include_name(i), ast_get_include_registrar(i));
  5221. astman_append(s, "\r\n");
  5222. ast_debug(3, "manager_show_dialplan: Found Included context: %s \n", ast_get_include_name(i));
  5223. }
  5224. }
  5225. ip = NULL; /* walk ignore patterns and write info ... */
  5226. while ( (ip = ast_walk_context_ignorepats(c, ip)) ) {
  5227. const char *ipname = ast_get_ignorepat_name(ip);
  5228. char ignorepat[AST_MAX_EXTENSION];
  5229. snprintf(ignorepat, sizeof(ignorepat), "_%s.", ipname);
  5230. if (!exten || ast_extension_match(ignorepat, exten)) {
  5231. if (!dpc->total_items++)
  5232. manager_dpsendack(s, m);
  5233. astman_append(s, "Event: ListDialplan\r\n%s", actionidtext);
  5234. astman_append(s, "Context: %s\r\nIgnorePattern: %s\r\nRegistrar: %s\r\n", ast_get_context_name(c), ipname, ast_get_ignorepat_registrar(ip));
  5235. astman_append(s, "\r\n");
  5236. }
  5237. }
  5238. if (!rinclude) {
  5239. struct ast_sw *sw = NULL;
  5240. while ( (sw = ast_walk_context_switches(c, sw)) ) {
  5241. if (!dpc->total_items++)
  5242. manager_dpsendack(s, m);
  5243. astman_append(s, "Event: ListDialplan\r\n%s", actionidtext);
  5244. astman_append(s, "Context: %s\r\nSwitch: %s/%s\r\nRegistrar: %s\r\n", ast_get_context_name(c), ast_get_switch_name(sw), ast_get_switch_data(sw), ast_get_switch_registrar(sw));
  5245. astman_append(s, "\r\n");
  5246. ast_debug(3, "manager_show_dialplan: Found Switch : %s \n", ast_get_switch_name(sw));
  5247. }
  5248. }
  5249. ast_unlock_context(c);
  5250. }
  5251. ast_unlock_contexts();
  5252. if (dpc->total_exten == old_total_exten) {
  5253. ast_debug(3, "manager_show_dialplan: Found nothing new\n");
  5254. /* Nothing new under the sun */
  5255. return -1;
  5256. } else {
  5257. return res;
  5258. }
  5259. }
  5260. /*! \brief Manager listing of dial plan */
  5261. static int manager_show_dialplan(struct mansession *s, const struct message *m)
  5262. {
  5263. const char *exten, *context;
  5264. const char *id = astman_get_header(m, "ActionID");
  5265. const char *incstack[AST_PBX_MAX_STACK];
  5266. char idtext[256];
  5267. /* Variables used for different counters */
  5268. struct dialplan_counters counters;
  5269. if (!ast_strlen_zero(id))
  5270. snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
  5271. else
  5272. idtext[0] = '\0';
  5273. memset(&counters, 0, sizeof(counters));
  5274. exten = astman_get_header(m, "Extension");
  5275. context = astman_get_header(m, "Context");
  5276. manager_show_dialplan_helper(s, m, idtext, context, exten, &counters, NULL, 0, incstack);
  5277. if (!ast_strlen_zero(context) && !counters.context_existence) {
  5278. char errorbuf[BUFSIZ];
  5279. snprintf(errorbuf, sizeof(errorbuf), "Did not find context %s", context);
  5280. astman_send_error(s, m, errorbuf);
  5281. return 0;
  5282. }
  5283. if (!ast_strlen_zero(exten) && !counters.extension_existence) {
  5284. char errorbuf[BUFSIZ];
  5285. if (!ast_strlen_zero(context))
  5286. snprintf(errorbuf, sizeof(errorbuf), "Did not find extension %s@%s", exten, context);
  5287. else
  5288. snprintf(errorbuf, sizeof(errorbuf), "Did not find extension %s in any context", exten);
  5289. astman_send_error(s, m, errorbuf);
  5290. return 0;
  5291. }
  5292. if (!counters.total_items) {
  5293. manager_dpsendack(s, m);
  5294. }
  5295. astman_send_list_complete_start(s, m, "ShowDialPlanComplete", counters.total_items);
  5296. astman_append(s,
  5297. "ListExtensions: %d\r\n"
  5298. "ListPriorities: %d\r\n"
  5299. "ListContexts: %d\r\n",
  5300. counters.total_exten, counters.total_prio, counters.total_context);
  5301. astman_send_list_complete_end(s);
  5302. /* everything ok */
  5303. return 0;
  5304. }
  5305. #ifdef AST_DEVMODE
  5306. static char *handle_show_device2extenstate(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  5307. {
  5308. struct ast_devstate_aggregate agg;
  5309. int i, j, exten, combined;
  5310. switch (cmd) {
  5311. case CLI_INIT:
  5312. e->command = "core show device2extenstate";
  5313. e->usage =
  5314. "Usage: core show device2extenstate\n"
  5315. " Lists device state to extension state combinations.\n";
  5316. case CLI_GENERATE:
  5317. return NULL;
  5318. }
  5319. for (i = 0; i < AST_DEVICE_TOTAL; i++) {
  5320. for (j = 0; j < AST_DEVICE_TOTAL; j++) {
  5321. ast_devstate_aggregate_init(&agg);
  5322. ast_devstate_aggregate_add(&agg, i);
  5323. ast_devstate_aggregate_add(&agg, j);
  5324. combined = ast_devstate_aggregate_result(&agg);
  5325. exten = ast_devstate_to_extenstate(combined);
  5326. ast_cli(a->fd, "\n Exten:%14s CombinedDevice:%12s Dev1:%12s Dev2:%12s", ast_extension_state2str(exten), ast_devstate_str(combined), ast_devstate_str(j), ast_devstate_str(i));
  5327. }
  5328. }
  5329. ast_cli(a->fd, "\n");
  5330. return CLI_SUCCESS;
  5331. }
  5332. #endif
  5333. static char *handle_set_extenpatternmatchnew(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  5334. {
  5335. int oldval = 0;
  5336. switch (cmd) {
  5337. case CLI_INIT:
  5338. e->command = "dialplan set extenpatternmatchnew true";
  5339. e->usage =
  5340. "Usage: dialplan set extenpatternmatchnew true|false\n"
  5341. " Use the NEW extension pattern matching algorithm, true or false.\n";
  5342. return NULL;
  5343. case CLI_GENERATE:
  5344. return NULL;
  5345. }
  5346. if (a->argc != 4)
  5347. return CLI_SHOWUSAGE;
  5348. oldval = pbx_set_extenpatternmatchnew(1);
  5349. if (oldval)
  5350. ast_cli(a->fd, "\n -- Still using the NEW pattern match algorithm for extension names in the dialplan.\n");
  5351. else
  5352. ast_cli(a->fd, "\n -- Switched to using the NEW pattern match algorithm for extension names in the dialplan.\n");
  5353. return CLI_SUCCESS;
  5354. }
  5355. static char *handle_unset_extenpatternmatchnew(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  5356. {
  5357. int oldval = 0;
  5358. switch (cmd) {
  5359. case CLI_INIT:
  5360. e->command = "dialplan set extenpatternmatchnew false";
  5361. e->usage =
  5362. "Usage: dialplan set extenpatternmatchnew true|false\n"
  5363. " Use the NEW extension pattern matching algorithm, true or false.\n";
  5364. return NULL;
  5365. case CLI_GENERATE:
  5366. return NULL;
  5367. }
  5368. if (a->argc != 4)
  5369. return CLI_SHOWUSAGE;
  5370. oldval = pbx_set_extenpatternmatchnew(0);
  5371. if (!oldval)
  5372. ast_cli(a->fd, "\n -- Still using the OLD pattern match algorithm for extension names in the dialplan.\n");
  5373. else
  5374. ast_cli(a->fd, "\n -- Switched to using the OLD pattern match algorithm for extension names in the dialplan.\n");
  5375. return CLI_SUCCESS;
  5376. }
  5377. /*
  5378. * CLI entries for upper commands ...
  5379. */
  5380. static struct ast_cli_entry pbx_cli[] = {
  5381. #if 0
  5382. AST_CLI_DEFINE(handle_eat_memory, "Eats all available memory"),
  5383. #endif
  5384. AST_CLI_DEFINE(handle_show_hints, "Show dialplan hints"),
  5385. AST_CLI_DEFINE(handle_show_hint, "Show dialplan hint"),
  5386. #ifdef AST_DEVMODE
  5387. AST_CLI_DEFINE(handle_show_device2extenstate, "Show expected exten state from multiple device states"),
  5388. #endif
  5389. AST_CLI_DEFINE(handle_show_dialplan, "Show dialplan"),
  5390. AST_CLI_DEFINE(handle_debug_dialplan, "Show fast extension pattern matching data structures"),
  5391. AST_CLI_DEFINE(handle_unset_extenpatternmatchnew, "Use the Old extension pattern matching algorithm."),
  5392. AST_CLI_DEFINE(handle_set_extenpatternmatchnew, "Use the New extension pattern matching algorithm."),
  5393. };
  5394. void unreference_cached_app(struct ast_app *app)
  5395. {
  5396. struct ast_context *context = NULL;
  5397. struct ast_exten *eroot = NULL, *e = NULL;
  5398. ast_rdlock_contexts();
  5399. while ((context = ast_walk_contexts(context))) {
  5400. while ((eroot = ast_walk_context_extensions(context, eroot))) {
  5401. while ((e = ast_walk_extension_priorities(eroot, e))) {
  5402. if (e->cached_app == app)
  5403. e->cached_app = NULL;
  5404. }
  5405. }
  5406. }
  5407. ast_unlock_contexts();
  5408. return;
  5409. }
  5410. struct ast_context *ast_context_find_or_create(struct ast_context **extcontexts, struct ast_hashtab *exttable, const char *name, const char *registrar)
  5411. {
  5412. struct ast_context *tmp, **local_contexts;
  5413. struct fake_context search;
  5414. int length = sizeof(struct ast_context) + strlen(name) + 1;
  5415. if (!contexts_table) {
  5416. /* Protect creation of contexts_table from reentrancy. */
  5417. ast_wrlock_contexts();
  5418. if (!contexts_table) {
  5419. contexts_table = ast_hashtab_create(17,
  5420. ast_hashtab_compare_contexts,
  5421. ast_hashtab_resize_java,
  5422. ast_hashtab_newsize_java,
  5423. ast_hashtab_hash_contexts,
  5424. 0);
  5425. }
  5426. ast_unlock_contexts();
  5427. }
  5428. ast_copy_string(search.name, name, sizeof(search.name));
  5429. if (!extcontexts) {
  5430. ast_rdlock_contexts();
  5431. local_contexts = &contexts;
  5432. tmp = ast_hashtab_lookup(contexts_table, &search);
  5433. if (tmp) {
  5434. tmp->refcount++;
  5435. ast_unlock_contexts();
  5436. return tmp;
  5437. }
  5438. } else { /* local contexts just in a linked list; search there for the new context; slow, linear search, but not frequent */
  5439. local_contexts = extcontexts;
  5440. tmp = ast_hashtab_lookup(exttable, &search);
  5441. if (tmp) {
  5442. tmp->refcount++;
  5443. return tmp;
  5444. }
  5445. }
  5446. if ((tmp = ast_calloc(1, length))) {
  5447. ast_rwlock_init(&tmp->lock);
  5448. ast_mutex_init(&tmp->macrolock);
  5449. strcpy(tmp->name, name);
  5450. tmp->root = NULL;
  5451. tmp->root_table = NULL;
  5452. tmp->registrar = ast_strdup(registrar);
  5453. tmp->includes = NULL;
  5454. tmp->ignorepats = NULL;
  5455. tmp->refcount = 1;
  5456. } else {
  5457. ast_log(LOG_ERROR, "Danger! We failed to allocate a context for %s!\n", name);
  5458. if (!extcontexts) {
  5459. ast_unlock_contexts();
  5460. }
  5461. return NULL;
  5462. }
  5463. if (!extcontexts) {
  5464. tmp->next = *local_contexts;
  5465. *local_contexts = tmp;
  5466. ast_hashtab_insert_safe(contexts_table, tmp); /*put this context into the tree */
  5467. ast_unlock_contexts();
  5468. } else {
  5469. tmp->next = *local_contexts;
  5470. if (exttable)
  5471. ast_hashtab_insert_immediate(exttable, tmp); /*put this context into the tree */
  5472. *local_contexts = tmp;
  5473. }
  5474. ast_debug(1, "Registered extension context '%s'; registrar: %s\n", tmp->name, registrar);
  5475. return tmp;
  5476. }
  5477. void __ast_context_destroy(struct ast_context *list, struct ast_hashtab *contexttab, struct ast_context *con, const char *registrar);
  5478. struct store_hint {
  5479. char *context;
  5480. char *exten;
  5481. AST_LIST_HEAD_NOLOCK(, ast_state_cb) callbacks;
  5482. int laststate;
  5483. int last_presence_state;
  5484. char *last_presence_subtype;
  5485. char *last_presence_message;
  5486. AST_LIST_ENTRY(store_hint) list;
  5487. char data[0];
  5488. };
  5489. AST_LIST_HEAD_NOLOCK(store_hints, store_hint);
  5490. static void context_merge_incls_swits_igps_other_registrars(struct ast_context *new, struct ast_context *old, const char *registrar)
  5491. {
  5492. struct ast_include *i;
  5493. struct ast_ignorepat *ip;
  5494. struct ast_sw *sw;
  5495. ast_debug(1, "merging incls/swits/igpats from old(%s) to new(%s) context, registrar = %s\n", ast_get_context_name(old), ast_get_context_name(new), registrar);
  5496. /* copy in the includes, switches, and ignorepats */
  5497. /* walk through includes */
  5498. for (i = NULL; (i = ast_walk_context_includes(old, i)) ; ) {
  5499. if (strcmp(ast_get_include_registrar(i), registrar) == 0)
  5500. continue; /* not mine */
  5501. ast_context_add_include2(new, ast_get_include_name(i), ast_get_include_registrar(i));
  5502. }
  5503. /* walk through switches */
  5504. for (sw = NULL; (sw = ast_walk_context_switches(old, sw)) ; ) {
  5505. if (strcmp(ast_get_switch_registrar(sw), registrar) == 0)
  5506. continue; /* not mine */
  5507. ast_context_add_switch2(new, ast_get_switch_name(sw), ast_get_switch_data(sw), ast_get_switch_eval(sw), ast_get_switch_registrar(sw));
  5508. }
  5509. /* walk thru ignorepats ... */
  5510. for (ip = NULL; (ip = ast_walk_context_ignorepats(old, ip)); ) {
  5511. if (strcmp(ast_get_ignorepat_registrar(ip), registrar) == 0)
  5512. continue; /* not mine */
  5513. ast_context_add_ignorepat2(new, ast_get_ignorepat_name(ip), ast_get_ignorepat_registrar(ip));
  5514. }
  5515. }
  5516. /* the purpose of this routine is to duplicate a context, with all its substructure,
  5517. except for any extens that have a matching registrar */
  5518. static void context_merge(struct ast_context **extcontexts, struct ast_hashtab *exttable, struct ast_context *context, const char *registrar)
  5519. {
  5520. struct ast_context *new = ast_hashtab_lookup(exttable, context); /* is there a match in the new set? */
  5521. struct ast_exten *exten_item, *prio_item, *new_exten_item, *new_prio_item;
  5522. struct ast_hashtab_iter *exten_iter;
  5523. struct ast_hashtab_iter *prio_iter;
  5524. int insert_count = 0;
  5525. int first = 1;
  5526. /* We'll traverse all the extensions/prios, and see which are not registrar'd with
  5527. the current registrar, and copy them to the new context. If the new context does not
  5528. exist, we'll create it "on demand". If no items are in this context to copy, then we'll
  5529. only create the empty matching context if the old one meets the criteria */
  5530. if (context->root_table) {
  5531. exten_iter = ast_hashtab_start_traversal(context->root_table);
  5532. while ((exten_item=ast_hashtab_next(exten_iter))) {
  5533. if (new) {
  5534. new_exten_item = ast_hashtab_lookup(new->root_table, exten_item);
  5535. } else {
  5536. new_exten_item = NULL;
  5537. }
  5538. prio_iter = ast_hashtab_start_traversal(exten_item->peer_table);
  5539. while ((prio_item=ast_hashtab_next(prio_iter))) {
  5540. int res1;
  5541. char *dupdstr;
  5542. if (new_exten_item) {
  5543. new_prio_item = ast_hashtab_lookup(new_exten_item->peer_table, prio_item);
  5544. } else {
  5545. new_prio_item = NULL;
  5546. }
  5547. if (strcmp(prio_item->registrar,registrar) == 0) {
  5548. continue;
  5549. }
  5550. /* make sure the new context exists, so we have somewhere to stick this exten/prio */
  5551. if (!new) {
  5552. new = ast_context_find_or_create(extcontexts, exttable, context->name, prio_item->registrar); /* a new context created via priority from a different context in the old dialplan, gets its registrar from the prio's registrar */
  5553. }
  5554. /* copy in the includes, switches, and ignorepats */
  5555. if (first) { /* but, only need to do this once */
  5556. context_merge_incls_swits_igps_other_registrars(new, context, registrar);
  5557. first = 0;
  5558. }
  5559. if (!new) {
  5560. ast_log(LOG_ERROR,"Could not allocate a new context for %s in merge_and_delete! Danger!\n", context->name);
  5561. ast_hashtab_end_traversal(prio_iter);
  5562. ast_hashtab_end_traversal(exten_iter);
  5563. return; /* no sense continuing. */
  5564. }
  5565. /* we will not replace existing entries in the new context with stuff from the old context.
  5566. but, if this is because of some sort of registrar conflict, we ought to say something... */
  5567. dupdstr = ast_strdup(prio_item->data);
  5568. res1 = ast_add_extension2(new, 0, prio_item->name, prio_item->priority, prio_item->label,
  5569. prio_item->matchcid ? prio_item->cidmatch : NULL, prio_item->app, dupdstr, ast_free_ptr, prio_item->registrar);
  5570. if (!res1 && new_exten_item && new_prio_item){
  5571. ast_verb(3,"Dropping old dialplan item %s/%s/%d [%s(%s)] (registrar=%s) due to conflict with new dialplan\n",
  5572. context->name, prio_item->name, prio_item->priority, prio_item->app, (char*)prio_item->data, prio_item->registrar);
  5573. } else {
  5574. /* we do NOT pass the priority data from the old to the new -- we pass a copy of it, so no changes to the current dialplan take place,
  5575. and no double frees take place, either! */
  5576. insert_count++;
  5577. }
  5578. }
  5579. ast_hashtab_end_traversal(prio_iter);
  5580. }
  5581. ast_hashtab_end_traversal(exten_iter);
  5582. } else if (new) {
  5583. /* If the context existed but had no extensions, we still want to merge
  5584. * the includes, switches and ignore patterns.
  5585. */
  5586. context_merge_incls_swits_igps_other_registrars(new, context, registrar);
  5587. }
  5588. if (!insert_count && !new && (strcmp(context->registrar, registrar) != 0 ||
  5589. (strcmp(context->registrar, registrar) == 0 && context->refcount > 1))) {
  5590. /* we could have given it the registrar of the other module who incremented the refcount,
  5591. but that's not available, so we give it the registrar we know about */
  5592. new = ast_context_find_or_create(extcontexts, exttable, context->name, context->registrar);
  5593. /* copy in the includes, switches, and ignorepats */
  5594. context_merge_incls_swits_igps_other_registrars(new, context, registrar);
  5595. }
  5596. }
  5597. /* XXX this does not check that multiple contexts are merged */
  5598. void ast_merge_contexts_and_delete(struct ast_context **extcontexts, struct ast_hashtab *exttable, const char *registrar)
  5599. {
  5600. double ft;
  5601. struct ast_context *tmp;
  5602. struct ast_context *oldcontextslist;
  5603. struct ast_hashtab *oldtable;
  5604. struct store_hints hints_stored = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
  5605. struct store_hints hints_removed = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
  5606. struct store_hint *saved_hint;
  5607. struct ast_hint *hint;
  5608. struct ast_exten *exten;
  5609. int length;
  5610. struct ast_state_cb *thiscb;
  5611. struct ast_hashtab_iter *iter;
  5612. struct ao2_iterator i;
  5613. int ctx_count = 0;
  5614. struct timeval begintime;
  5615. struct timeval writelocktime;
  5616. struct timeval endlocktime;
  5617. struct timeval enddeltime;
  5618. /*
  5619. * It is very important that this function hold the hints
  5620. * container lock _and_ the conlock during its operation; not
  5621. * only do we need to ensure that the list of contexts and
  5622. * extensions does not change, but also that no hint callbacks
  5623. * (watchers) are added or removed during the merge/delete
  5624. * process
  5625. *
  5626. * In addition, the locks _must_ be taken in this order, because
  5627. * there are already other code paths that use this order
  5628. */
  5629. begintime = ast_tvnow();
  5630. ast_mutex_lock(&context_merge_lock);/* Serialize ast_merge_contexts_and_delete */
  5631. ast_wrlock_contexts();
  5632. if (!contexts_table) {
  5633. /* Well, that's odd. There are no contexts. */
  5634. contexts_table = exttable;
  5635. contexts = *extcontexts;
  5636. ast_unlock_contexts();
  5637. ast_mutex_unlock(&context_merge_lock);
  5638. return;
  5639. }
  5640. iter = ast_hashtab_start_traversal(contexts_table);
  5641. while ((tmp = ast_hashtab_next(iter))) {
  5642. ++ctx_count;
  5643. context_merge(extcontexts, exttable, tmp, registrar);
  5644. }
  5645. ast_hashtab_end_traversal(iter);
  5646. ao2_lock(hints);
  5647. writelocktime = ast_tvnow();
  5648. /* preserve all watchers for hints */
  5649. i = ao2_iterator_init(hints, AO2_ITERATOR_DONTLOCK);
  5650. for (; (hint = ao2_iterator_next(&i)); ao2_ref(hint, -1)) {
  5651. if (ao2_container_count(hint->callbacks)) {
  5652. ao2_lock(hint);
  5653. if (!hint->exten) {
  5654. /* The extension has already been destroyed. (Should never happen here) */
  5655. ao2_unlock(hint);
  5656. continue;
  5657. }
  5658. length = strlen(hint->exten->exten) + strlen(hint->exten->parent->name) + 2
  5659. + sizeof(*saved_hint);
  5660. if (!(saved_hint = ast_calloc(1, length))) {
  5661. ao2_unlock(hint);
  5662. continue;
  5663. }
  5664. /* This removes all the callbacks from the hint into saved_hint. */
  5665. while ((thiscb = ao2_callback(hint->callbacks, OBJ_UNLINK, NULL, NULL))) {
  5666. AST_LIST_INSERT_TAIL(&saved_hint->callbacks, thiscb, entry);
  5667. /*
  5668. * We intentionally do not unref thiscb to account for the
  5669. * non-ao2 reference in saved_hint->callbacks
  5670. */
  5671. }
  5672. saved_hint->laststate = hint->laststate;
  5673. saved_hint->context = saved_hint->data;
  5674. strcpy(saved_hint->data, hint->exten->parent->name);
  5675. saved_hint->exten = saved_hint->data + strlen(saved_hint->context) + 1;
  5676. strcpy(saved_hint->exten, hint->exten->exten);
  5677. if (hint->last_presence_subtype) {
  5678. saved_hint->last_presence_subtype = ast_strdup(hint->last_presence_subtype);
  5679. }
  5680. if (hint->last_presence_message) {
  5681. saved_hint->last_presence_message = ast_strdup(hint->last_presence_message);
  5682. }
  5683. saved_hint->last_presence_state = hint->last_presence_state;
  5684. ao2_unlock(hint);
  5685. AST_LIST_INSERT_HEAD(&hints_stored, saved_hint, list);
  5686. }
  5687. }
  5688. ao2_iterator_destroy(&i);
  5689. /* save the old table and list */
  5690. oldtable = contexts_table;
  5691. oldcontextslist = contexts;
  5692. /* move in the new table and list */
  5693. contexts_table = exttable;
  5694. contexts = *extcontexts;
  5695. /*
  5696. * Restore the watchers for hints that can be found; notify
  5697. * those that cannot be restored.
  5698. */
  5699. while ((saved_hint = AST_LIST_REMOVE_HEAD(&hints_stored, list))) {
  5700. struct pbx_find_info q = { .stacklen = 0 };
  5701. exten = pbx_find_extension(NULL, NULL, &q, saved_hint->context, saved_hint->exten,
  5702. PRIORITY_HINT, NULL, "", E_MATCH);
  5703. /*
  5704. * If this is a pattern, dynamically create a new extension for this
  5705. * particular match. Note that this will only happen once for each
  5706. * individual extension, because the pattern will no longer match first.
  5707. */
  5708. if (exten && exten->exten[0] == '_') {
  5709. ast_add_extension_nolock(exten->parent->name, 0, saved_hint->exten,
  5710. PRIORITY_HINT, NULL, 0, exten->app, ast_strdup(exten->data), ast_free_ptr,
  5711. exten->registrar);
  5712. /* rwlocks are not recursive locks */
  5713. exten = ast_hint_extension_nolock(NULL, saved_hint->context,
  5714. saved_hint->exten);
  5715. }
  5716. /* Find the hint in the hints container */
  5717. hint = exten ? ao2_find(hints, exten, 0) : NULL;
  5718. if (!hint) {
  5719. /*
  5720. * Notify watchers of this removed hint later when we aren't
  5721. * encumberd by so many locks.
  5722. */
  5723. AST_LIST_INSERT_HEAD(&hints_removed, saved_hint, list);
  5724. } else {
  5725. ao2_lock(hint);
  5726. while ((thiscb = AST_LIST_REMOVE_HEAD(&saved_hint->callbacks, entry))) {
  5727. ao2_link(hint->callbacks, thiscb);
  5728. /* Ref that we added when putting into saved_hint->callbacks */
  5729. ao2_ref(thiscb, -1);
  5730. }
  5731. hint->laststate = saved_hint->laststate;
  5732. hint->last_presence_state = saved_hint->last_presence_state;
  5733. hint->last_presence_subtype = saved_hint->last_presence_subtype;
  5734. hint->last_presence_message = saved_hint->last_presence_message;
  5735. ao2_unlock(hint);
  5736. ao2_ref(hint, -1);
  5737. /*
  5738. * The free of saved_hint->last_presence_subtype and
  5739. * saved_hint->last_presence_message is not necessary here.
  5740. */
  5741. ast_free(saved_hint);
  5742. }
  5743. }
  5744. ao2_unlock(hints);
  5745. ast_unlock_contexts();
  5746. /*
  5747. * Notify watchers of all removed hints with the same lock
  5748. * environment as device_state_cb().
  5749. */
  5750. while ((saved_hint = AST_LIST_REMOVE_HEAD(&hints_removed, list))) {
  5751. /* this hint has been removed, notify the watchers */
  5752. while ((thiscb = AST_LIST_REMOVE_HEAD(&saved_hint->callbacks, entry))) {
  5753. execute_state_callback(thiscb->change_cb,
  5754. saved_hint->context,
  5755. saved_hint->exten,
  5756. thiscb->data,
  5757. AST_HINT_UPDATE_DEVICE,
  5758. NULL,
  5759. NULL);
  5760. /* Ref that we added when putting into saved_hint->callbacks */
  5761. ao2_ref(thiscb, -1);
  5762. }
  5763. ast_free(saved_hint->last_presence_subtype);
  5764. ast_free(saved_hint->last_presence_message);
  5765. ast_free(saved_hint);
  5766. }
  5767. ast_mutex_unlock(&context_merge_lock);
  5768. endlocktime = ast_tvnow();
  5769. /*
  5770. * The old list and hashtab no longer are relevant, delete them
  5771. * while the rest of asterisk is now freely using the new stuff
  5772. * instead.
  5773. */
  5774. ast_hashtab_destroy(oldtable, NULL);
  5775. for (tmp = oldcontextslist; tmp; ) {
  5776. struct ast_context *next; /* next starting point */
  5777. next = tmp->next;
  5778. __ast_internal_context_destroy(tmp);
  5779. tmp = next;
  5780. }
  5781. enddeltime = ast_tvnow();
  5782. ft = ast_tvdiff_us(writelocktime, begintime);
  5783. ft /= 1000000.0;
  5784. ast_verb(3,"Time to scan old dialplan and merge leftovers back into the new: %8.6f sec\n", ft);
  5785. ft = ast_tvdiff_us(endlocktime, writelocktime);
  5786. ft /= 1000000.0;
  5787. ast_verb(3,"Time to restore hints and swap in new dialplan: %8.6f sec\n", ft);
  5788. ft = ast_tvdiff_us(enddeltime, endlocktime);
  5789. ft /= 1000000.0;
  5790. ast_verb(3,"Time to delete the old dialplan: %8.6f sec\n", ft);
  5791. ft = ast_tvdiff_us(enddeltime, begintime);
  5792. ft /= 1000000.0;
  5793. ast_verb(3,"Total time merge_contexts_delete: %8.6f sec\n", ft);
  5794. ast_verb(3, "%s successfully loaded %d contexts (enable debug for details).\n", registrar, ctx_count);
  5795. }
  5796. /*
  5797. * errno values
  5798. * EBUSY - can't lock
  5799. * ENOENT - no existence of context
  5800. */
  5801. int ast_context_add_include(const char *context, const char *include, const char *registrar)
  5802. {
  5803. int ret = -1;
  5804. struct ast_context *c;
  5805. c = find_context_locked(context);
  5806. if (c) {
  5807. ret = ast_context_add_include2(c, include, registrar);
  5808. ast_unlock_contexts();
  5809. }
  5810. return ret;
  5811. }
  5812. /*
  5813. * errno values
  5814. * ENOMEM - out of memory
  5815. * EBUSY - can't lock
  5816. * EEXIST - already included
  5817. * EINVAL - there is no existence of context for inclusion
  5818. */
  5819. int ast_context_add_include2(struct ast_context *con, const char *value,
  5820. const char *registrar)
  5821. {
  5822. struct ast_include *new_include;
  5823. char *c;
  5824. struct ast_include *i, *il = NULL; /* include, include_last */
  5825. int length;
  5826. char *p;
  5827. length = sizeof(struct ast_include);
  5828. length += 2 * (strlen(value) + 1);
  5829. /* allocate new include structure ... */
  5830. if (!(new_include = ast_calloc(1, length)))
  5831. return -1;
  5832. /* Fill in this structure. Use 'p' for assignments, as the fields
  5833. * in the structure are 'const char *'
  5834. */
  5835. p = new_include->stuff;
  5836. new_include->name = p;
  5837. strcpy(p, value);
  5838. p += strlen(value) + 1;
  5839. new_include->rname = p;
  5840. strcpy(p, value);
  5841. /* Strip off timing info, and process if it is there */
  5842. if ( (c = strchr(p, ',')) ) {
  5843. *c++ = '\0';
  5844. new_include->hastime = ast_build_timing(&(new_include->timing), c);
  5845. }
  5846. new_include->next = NULL;
  5847. new_include->registrar = registrar;
  5848. ast_wrlock_context(con);
  5849. /* ... go to last include and check if context is already included too... */
  5850. for (i = con->includes; i; i = i->next) {
  5851. if (!strcasecmp(i->name, new_include->name)) {
  5852. include_free(new_include);
  5853. ast_unlock_context(con);
  5854. errno = EEXIST;
  5855. return -1;
  5856. }
  5857. il = i;
  5858. }
  5859. /* ... include new context into context list, unlock, return */
  5860. if (il)
  5861. il->next = new_include;
  5862. else
  5863. con->includes = new_include;
  5864. ast_debug(1, "Including context '%s' in context '%s'\n", new_include->name, ast_get_context_name(con));
  5865. ast_unlock_context(con);
  5866. return 0;
  5867. }
  5868. /*
  5869. * errno values
  5870. * EBUSY - can't lock
  5871. * ENOENT - no existence of context
  5872. */
  5873. int ast_context_add_switch(const char *context, const char *sw, const char *data, int eval, const char *registrar)
  5874. {
  5875. int ret = -1;
  5876. struct ast_context *c;
  5877. c = find_context_locked(context);
  5878. if (c) { /* found, add switch to this context */
  5879. ret = ast_context_add_switch2(c, sw, data, eval, registrar);
  5880. ast_unlock_contexts();
  5881. }
  5882. return ret;
  5883. }
  5884. /*
  5885. * errno values
  5886. * ENOMEM - out of memory
  5887. * EBUSY - can't lock
  5888. * EEXIST - already included
  5889. * EINVAL - there is no existence of context for inclusion
  5890. */
  5891. int ast_context_add_switch2(struct ast_context *con, const char *value,
  5892. const char *data, int eval, const char *registrar)
  5893. {
  5894. struct ast_sw *new_sw;
  5895. struct ast_sw *i;
  5896. int length;
  5897. char *p;
  5898. length = sizeof(struct ast_sw);
  5899. length += strlen(value) + 1;
  5900. if (data)
  5901. length += strlen(data);
  5902. length++;
  5903. /* allocate new sw structure ... */
  5904. if (!(new_sw = ast_calloc(1, length)))
  5905. return -1;
  5906. /* ... fill in this structure ... */
  5907. p = new_sw->stuff;
  5908. new_sw->name = p;
  5909. strcpy(new_sw->name, value);
  5910. p += strlen(value) + 1;
  5911. new_sw->data = p;
  5912. if (data) {
  5913. strcpy(new_sw->data, data);
  5914. p += strlen(data) + 1;
  5915. } else {
  5916. strcpy(new_sw->data, "");
  5917. p++;
  5918. }
  5919. new_sw->eval = eval;
  5920. new_sw->registrar = registrar;
  5921. /* ... try to lock this context ... */
  5922. ast_wrlock_context(con);
  5923. /* ... go to last sw and check if context is already swd too... */
  5924. AST_LIST_TRAVERSE(&con->alts, i, list) {
  5925. if (!strcasecmp(i->name, new_sw->name) && !strcasecmp(i->data, new_sw->data)) {
  5926. ast_free(new_sw);
  5927. ast_unlock_context(con);
  5928. errno = EEXIST;
  5929. return -1;
  5930. }
  5931. }
  5932. /* ... sw new context into context list, unlock, return */
  5933. AST_LIST_INSERT_TAIL(&con->alts, new_sw, list);
  5934. ast_verb(3, "Including switch '%s/%s' in context '%s'\n", new_sw->name, new_sw->data, ast_get_context_name(con));
  5935. ast_unlock_context(con);
  5936. return 0;
  5937. }
  5938. /*
  5939. * EBUSY - can't lock
  5940. * ENOENT - there is not context existence
  5941. */
  5942. int ast_context_remove_ignorepat(const char *context, const char *ignorepat, const char *registrar)
  5943. {
  5944. int ret = -1;
  5945. struct ast_context *c;
  5946. c = find_context_locked(context);
  5947. if (c) {
  5948. ret = ast_context_remove_ignorepat2(c, ignorepat, registrar);
  5949. ast_unlock_contexts();
  5950. }
  5951. return ret;
  5952. }
  5953. int ast_context_remove_ignorepat2(struct ast_context *con, const char *ignorepat, const char *registrar)
  5954. {
  5955. struct ast_ignorepat *ip, *ipl = NULL;
  5956. ast_wrlock_context(con);
  5957. for (ip = con->ignorepats; ip; ip = ip->next) {
  5958. if (!strcmp(ip->pattern, ignorepat) &&
  5959. (!registrar || (registrar == ip->registrar))) {
  5960. if (ipl) {
  5961. ipl->next = ip->next;
  5962. ast_free(ip);
  5963. } else {
  5964. con->ignorepats = ip->next;
  5965. ast_free(ip);
  5966. }
  5967. ast_unlock_context(con);
  5968. return 0;
  5969. }
  5970. ipl = ip;
  5971. }
  5972. ast_unlock_context(con);
  5973. errno = EINVAL;
  5974. return -1;
  5975. }
  5976. /*
  5977. * EBUSY - can't lock
  5978. * ENOENT - there is no existence of context
  5979. */
  5980. int ast_context_add_ignorepat(const char *context, const char *value, const char *registrar)
  5981. {
  5982. int ret = -1;
  5983. struct ast_context *c;
  5984. c = find_context_locked(context);
  5985. if (c) {
  5986. ret = ast_context_add_ignorepat2(c, value, registrar);
  5987. ast_unlock_contexts();
  5988. }
  5989. return ret;
  5990. }
  5991. int ast_context_add_ignorepat2(struct ast_context *con, const char *value, const char *registrar)
  5992. {
  5993. struct ast_ignorepat *ignorepat, *ignorepatc, *ignorepatl = NULL;
  5994. int length;
  5995. char *pattern;
  5996. length = sizeof(struct ast_ignorepat);
  5997. length += strlen(value) + 1;
  5998. if (!(ignorepat = ast_calloc(1, length)))
  5999. return -1;
  6000. /* The cast to char * is because we need to write the initial value.
  6001. * The field is not supposed to be modified otherwise. Also, gcc 4.2
  6002. * sees the cast as dereferencing a type-punned pointer and warns about
  6003. * it. This is the workaround (we're telling gcc, yes, that's really
  6004. * what we wanted to do).
  6005. */
  6006. pattern = (char *) ignorepat->pattern;
  6007. strcpy(pattern, value);
  6008. ignorepat->next = NULL;
  6009. ignorepat->registrar = registrar;
  6010. ast_wrlock_context(con);
  6011. for (ignorepatc = con->ignorepats; ignorepatc; ignorepatc = ignorepatc->next) {
  6012. ignorepatl = ignorepatc;
  6013. if (!strcasecmp(ignorepatc->pattern, value)) {
  6014. /* Already there */
  6015. ast_unlock_context(con);
  6016. ast_free(ignorepat);
  6017. errno = EEXIST;
  6018. return -1;
  6019. }
  6020. }
  6021. if (ignorepatl)
  6022. ignorepatl->next = ignorepat;
  6023. else
  6024. con->ignorepats = ignorepat;
  6025. ast_unlock_context(con);
  6026. return 0;
  6027. }
  6028. int ast_ignore_pattern(const char *context, const char *pattern)
  6029. {
  6030. int ret = 0;
  6031. struct ast_context *con;
  6032. ast_rdlock_contexts();
  6033. con = ast_context_find(context);
  6034. if (con) {
  6035. struct ast_ignorepat *pat;
  6036. for (pat = con->ignorepats; pat; pat = pat->next) {
  6037. if (ast_extension_match(pat->pattern, pattern)) {
  6038. ret = 1;
  6039. break;
  6040. }
  6041. }
  6042. }
  6043. ast_unlock_contexts();
  6044. return ret;
  6045. }
  6046. /*
  6047. * ast_add_extension_nolock -- use only in situations where the conlock is already held
  6048. * ENOENT - no existence of context
  6049. *
  6050. */
  6051. static int ast_add_extension_nolock(const char *context, int replace, const char *extension,
  6052. int priority, const char *label, const char *callerid,
  6053. const char *application, void *data, void (*datad)(void *), const char *registrar)
  6054. {
  6055. int ret = -1;
  6056. struct ast_context *c;
  6057. c = find_context(context);
  6058. if (c) {
  6059. ret = ast_add_extension2_lockopt(c, replace, extension, priority, label, callerid,
  6060. application, data, datad, registrar, 1);
  6061. }
  6062. return ret;
  6063. }
  6064. /*
  6065. * EBUSY - can't lock
  6066. * ENOENT - no existence of context
  6067. *
  6068. */
  6069. int ast_add_extension(const char *context, int replace, const char *extension,
  6070. int priority, const char *label, const char *callerid,
  6071. const char *application, void *data, void (*datad)(void *), const char *registrar)
  6072. {
  6073. int ret = -1;
  6074. struct ast_context *c;
  6075. c = find_context_locked(context);
  6076. if (c) {
  6077. ret = ast_add_extension2(c, replace, extension, priority, label, callerid,
  6078. application, data, datad, registrar);
  6079. ast_unlock_contexts();
  6080. }
  6081. return ret;
  6082. }
  6083. int ast_explicit_goto(struct ast_channel *chan, const char *context, const char *exten, int priority)
  6084. {
  6085. if (!chan)
  6086. return -1;
  6087. ast_channel_lock(chan);
  6088. if (!ast_strlen_zero(context))
  6089. ast_channel_context_set(chan, context);
  6090. if (!ast_strlen_zero(exten))
  6091. ast_channel_exten_set(chan, exten);
  6092. if (priority > -1) {
  6093. /* see flag description in channel.h for explanation */
  6094. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP)) {
  6095. --priority;
  6096. }
  6097. ast_channel_priority_set(chan, priority);
  6098. }
  6099. ast_channel_unlock(chan);
  6100. return 0;
  6101. }
  6102. int ast_async_goto(struct ast_channel *chan, const char *context, const char *exten, int priority)
  6103. {
  6104. struct ast_channel *newchan;
  6105. ast_channel_lock(chan);
  6106. /* Channels in a bridge or running a PBX can be sent directly to the specified destination */
  6107. if (ast_channel_is_bridged(chan) || ast_channel_pbx(chan)) {
  6108. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP)) {
  6109. priority += 1;
  6110. }
  6111. ast_explicit_goto(chan, context, exten, priority);
  6112. ast_softhangup_nolock(chan, AST_SOFTHANGUP_ASYNCGOTO);
  6113. ast_channel_unlock(chan);
  6114. return 0;
  6115. }
  6116. ast_channel_unlock(chan);
  6117. /* Otherwise, we need to gain control of the channel first */
  6118. newchan = ast_channel_yank(chan);
  6119. if (!newchan) {
  6120. ast_log(LOG_WARNING, "Unable to gain control of channel %s\n", ast_channel_name(chan));
  6121. return -1;
  6122. }
  6123. ast_explicit_goto(newchan, context, exten, priority);
  6124. if (ast_pbx_start(newchan)) {
  6125. ast_hangup(newchan);
  6126. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(newchan));
  6127. return -1;
  6128. }
  6129. return 0;
  6130. }
  6131. int ast_async_goto_by_name(const char *channame, const char *context, const char *exten, int priority)
  6132. {
  6133. struct ast_channel *chan;
  6134. int res = -1;
  6135. if ((chan = ast_channel_get_by_name(channame))) {
  6136. res = ast_async_goto(chan, context, exten, priority);
  6137. chan = ast_channel_unref(chan);
  6138. }
  6139. return res;
  6140. }
  6141. /*!
  6142. * \internal
  6143. * \brief Copy a string skipping whitespace and optionally dashes.
  6144. *
  6145. * \param dst Destination buffer to copy src string.
  6146. * \param src Null terminated string to copy.
  6147. * \param dst_size Number of bytes in the dst buffer.
  6148. * \param nofluf Nonzero if '-' chars are not copied.
  6149. *
  6150. * \return Number of bytes written to dst including null terminator.
  6151. */
  6152. static unsigned int ext_strncpy(char *dst, const char *src, size_t dst_size, int nofluff)
  6153. {
  6154. unsigned int count;
  6155. unsigned int insquares;
  6156. unsigned int is_pattern;
  6157. if (!dst_size--) {
  6158. /* There really is no dst buffer */
  6159. return 0;
  6160. }
  6161. count = 0;
  6162. insquares = 0;
  6163. is_pattern = *src == '_';
  6164. while (*src && count < dst_size) {
  6165. if (*src == '[') {
  6166. if (is_pattern) {
  6167. insquares = 1;
  6168. }
  6169. } else if (*src == ']') {
  6170. insquares = 0;
  6171. } else if (*src == ' ' && !insquares) {
  6172. ++src;
  6173. continue;
  6174. } else if (*src == '-' && !insquares && nofluff) {
  6175. ++src;
  6176. continue;
  6177. }
  6178. *dst++ = *src++;
  6179. ++count;
  6180. }
  6181. *dst = '\0';
  6182. return count + 1;
  6183. }
  6184. /*!
  6185. * \brief add the extension in the priority chain.
  6186. * \retval 0 on success.
  6187. * \retval -1 on failure.
  6188. */
  6189. static int add_priority(struct ast_context *con, struct ast_exten *tmp,
  6190. struct ast_exten *el, struct ast_exten *e, int replace)
  6191. {
  6192. struct ast_exten *ep;
  6193. struct ast_exten *eh=e;
  6194. int repeated_label = 0; /* Track if this label is a repeat, assume no. */
  6195. for (ep = NULL; e ; ep = e, e = e->peer) {
  6196. if (e->label && tmp->label && e->priority != tmp->priority && !strcmp(e->label, tmp->label)) {
  6197. if (strcmp(e->name, tmp->name)) {
  6198. ast_log(LOG_WARNING,
  6199. "Extension '%s' priority %d in '%s', label '%s' already in use at aliased extension '%s' priority %d\n",
  6200. tmp->name, tmp->priority, con->name, tmp->label, e->name, e->priority);
  6201. } else {
  6202. ast_log(LOG_WARNING,
  6203. "Extension '%s' priority %d in '%s', label '%s' already in use at priority %d\n",
  6204. tmp->name, tmp->priority, con->name, tmp->label, e->priority);
  6205. }
  6206. repeated_label = 1;
  6207. }
  6208. if (e->priority >= tmp->priority) {
  6209. break;
  6210. }
  6211. }
  6212. if (repeated_label) { /* Discard the label since it's a repeat. */
  6213. tmp->label = NULL;
  6214. }
  6215. if (!e) { /* go at the end, and ep is surely set because the list is not empty */
  6216. ast_hashtab_insert_safe(eh->peer_table, tmp);
  6217. if (tmp->label) {
  6218. ast_hashtab_insert_safe(eh->peer_label_table, tmp);
  6219. }
  6220. ep->peer = tmp;
  6221. return 0; /* success */
  6222. }
  6223. if (e->priority == tmp->priority) {
  6224. /* Can't have something exactly the same. Is this a
  6225. replacement? If so, replace, otherwise, bonk. */
  6226. if (!replace) {
  6227. if (strcmp(e->name, tmp->name)) {
  6228. ast_log(LOG_WARNING,
  6229. "Unable to register extension '%s' priority %d in '%s', already in use by aliased extension '%s'\n",
  6230. tmp->name, tmp->priority, con->name, e->name);
  6231. } else {
  6232. ast_log(LOG_WARNING,
  6233. "Unable to register extension '%s' priority %d in '%s', already in use\n",
  6234. tmp->name, tmp->priority, con->name);
  6235. }
  6236. return -1;
  6237. }
  6238. /* we are replacing e, so copy the link fields and then update
  6239. * whoever pointed to e to point to us
  6240. */
  6241. tmp->next = e->next; /* not meaningful if we are not first in the peer list */
  6242. tmp->peer = e->peer; /* always meaningful */
  6243. if (ep) { /* We're in the peer list, just insert ourselves */
  6244. ast_hashtab_remove_object_via_lookup(eh->peer_table,e);
  6245. if (e->label) {
  6246. ast_hashtab_remove_object_via_lookup(eh->peer_label_table,e);
  6247. }
  6248. ast_hashtab_insert_safe(eh->peer_table,tmp);
  6249. if (tmp->label) {
  6250. ast_hashtab_insert_safe(eh->peer_label_table,tmp);
  6251. }
  6252. ep->peer = tmp;
  6253. } else if (el) { /* We're the first extension. Take over e's functions */
  6254. struct match_char *x = add_exten_to_pattern_tree(con, e, 1);
  6255. tmp->peer_table = e->peer_table;
  6256. tmp->peer_label_table = e->peer_label_table;
  6257. ast_hashtab_remove_object_via_lookup(tmp->peer_table,e);
  6258. ast_hashtab_insert_safe(tmp->peer_table,tmp);
  6259. if (e->label) {
  6260. ast_hashtab_remove_object_via_lookup(tmp->peer_label_table, e);
  6261. }
  6262. if (tmp->label) {
  6263. ast_hashtab_insert_safe(tmp->peer_label_table, tmp);
  6264. }
  6265. ast_hashtab_remove_object_via_lookup(con->root_table, e);
  6266. ast_hashtab_insert_safe(con->root_table, tmp);
  6267. el->next = tmp;
  6268. /* The pattern trie points to this exten; replace the pointer,
  6269. and all will be well */
  6270. if (x) { /* if the trie isn't formed yet, don't sweat this */
  6271. if (x->exten) { /* this test for safety purposes */
  6272. x->exten = tmp; /* replace what would become a bad pointer */
  6273. } else {
  6274. ast_log(LOG_ERROR,"Trying to delete an exten from a context, but the pattern tree node returned isn't an extension\n");
  6275. }
  6276. }
  6277. } else { /* We're the very first extension. */
  6278. struct match_char *x = add_exten_to_pattern_tree(con, e, 1);
  6279. ast_hashtab_remove_object_via_lookup(con->root_table, e);
  6280. ast_hashtab_insert_safe(con->root_table, tmp);
  6281. tmp->peer_table = e->peer_table;
  6282. tmp->peer_label_table = e->peer_label_table;
  6283. ast_hashtab_remove_object_via_lookup(tmp->peer_table, e);
  6284. ast_hashtab_insert_safe(tmp->peer_table, tmp);
  6285. if (e->label) {
  6286. ast_hashtab_remove_object_via_lookup(tmp->peer_label_table, e);
  6287. }
  6288. if (tmp->label) {
  6289. ast_hashtab_insert_safe(tmp->peer_label_table, tmp);
  6290. }
  6291. ast_hashtab_remove_object_via_lookup(con->root_table, e);
  6292. ast_hashtab_insert_safe(con->root_table, tmp);
  6293. con->root = tmp;
  6294. /* The pattern trie points to this exten; replace the pointer,
  6295. and all will be well */
  6296. if (x) { /* if the trie isn't formed yet; no problem */
  6297. if (x->exten) { /* this test for safety purposes */
  6298. x->exten = tmp; /* replace what would become a bad pointer */
  6299. } else {
  6300. ast_log(LOG_ERROR,"Trying to delete an exten from a context, but the pattern tree node returned isn't an extension\n");
  6301. }
  6302. }
  6303. }
  6304. if (tmp->priority == PRIORITY_HINT)
  6305. ast_change_hint(e,tmp);
  6306. /* Destroy the old one */
  6307. if (e->datad)
  6308. e->datad(e->data);
  6309. ast_free(e);
  6310. } else { /* Slip ourselves in just before e */
  6311. tmp->peer = e;
  6312. tmp->next = e->next; /* extension chain, or NULL if e is not the first extension */
  6313. if (ep) { /* Easy enough, we're just in the peer list */
  6314. if (tmp->label) {
  6315. ast_hashtab_insert_safe(eh->peer_label_table, tmp);
  6316. }
  6317. ast_hashtab_insert_safe(eh->peer_table, tmp);
  6318. ep->peer = tmp;
  6319. } else { /* we are the first in some peer list, so link in the ext list */
  6320. tmp->peer_table = e->peer_table;
  6321. tmp->peer_label_table = e->peer_label_table;
  6322. e->peer_table = 0;
  6323. e->peer_label_table = 0;
  6324. ast_hashtab_insert_safe(tmp->peer_table, tmp);
  6325. if (tmp->label) {
  6326. ast_hashtab_insert_safe(tmp->peer_label_table, tmp);
  6327. }
  6328. ast_hashtab_remove_object_via_lookup(con->root_table, e);
  6329. ast_hashtab_insert_safe(con->root_table, tmp);
  6330. if (el)
  6331. el->next = tmp; /* in the middle... */
  6332. else
  6333. con->root = tmp; /* ... or at the head */
  6334. e->next = NULL; /* e is no more at the head, so e->next must be reset */
  6335. }
  6336. /* And immediately return success. */
  6337. if (tmp->priority == PRIORITY_HINT) {
  6338. ast_add_hint(tmp);
  6339. }
  6340. }
  6341. return 0;
  6342. }
  6343. /*! \brief
  6344. * Main interface to add extensions to the list for out context.
  6345. *
  6346. * We sort extensions in order of matching preference, so that we can
  6347. * stop the search as soon as we find a suitable match.
  6348. * This ordering also takes care of wildcards such as '.' (meaning
  6349. * "one or more of any character") and '!' (which is 'earlymatch',
  6350. * meaning "zero or more of any character" but also impacts the
  6351. * return value from CANMATCH and EARLYMATCH.
  6352. *
  6353. * The extension match rules defined in the devmeeting 2006.05.05 are
  6354. * quite simple: WE SELECT THE LONGEST MATCH.
  6355. * In detail, "longest" means the number of matched characters in
  6356. * the extension. In case of ties (e.g. _XXX and 333) in the length
  6357. * of a pattern, we give priority to entries with the smallest cardinality
  6358. * (e.g, [5-9] comes before [2-8] before the former has only 5 elements,
  6359. * while the latter has 7, etc.
  6360. * In case of same cardinality, the first element in the range counts.
  6361. * If we still have a tie, any final '!' will make this as a possibly
  6362. * less specific pattern.
  6363. *
  6364. * EBUSY - can't lock
  6365. * EEXIST - extension with the same priority exist and no replace is set
  6366. *
  6367. */
  6368. int ast_add_extension2(struct ast_context *con,
  6369. int replace, const char *extension, int priority, const char *label, const char *callerid,
  6370. const char *application, void *data, void (*datad)(void *),
  6371. const char *registrar)
  6372. {
  6373. return ast_add_extension2_lockopt(con, replace, extension, priority, label, callerid,
  6374. application, data, datad, registrar, 1);
  6375. }
  6376. int ast_add_extension2_nolock(struct ast_context *con,
  6377. int replace, const char *extension, int priority, const char *label, const char *callerid,
  6378. const char *application, void *data, void (*datad)(void *),
  6379. const char *registrar)
  6380. {
  6381. return ast_add_extension2_lockopt(con, replace, extension, priority, label, callerid,
  6382. application, data, datad, registrar, 0);
  6383. }
  6384. /*!
  6385. * \brief Same as ast_add_extension2() but controls the context locking.
  6386. *
  6387. * \details
  6388. * Does all the work of ast_add_extension2, but adds an arg to
  6389. * determine if context locking should be done.
  6390. */
  6391. static int ast_add_extension2_lockopt(struct ast_context *con,
  6392. int replace, const char *extension, int priority, const char *label, const char *callerid,
  6393. const char *application, void *data, void (*datad)(void *),
  6394. const char *registrar, int lock_context)
  6395. {
  6396. /*
  6397. * Sort extensions (or patterns) according to the rules indicated above.
  6398. * These are implemented by the function ext_cmp()).
  6399. * All priorities for the same ext/pattern/cid are kept in a list,
  6400. * using the 'peer' field as a link field..
  6401. */
  6402. struct ast_exten *tmp, *tmp2, *e, *el = NULL;
  6403. int res;
  6404. int length;
  6405. char *p;
  6406. char expand_buf[VAR_BUF_SIZE];
  6407. struct ast_exten dummy_exten = {0};
  6408. char dummy_name[1024];
  6409. int exten_fluff;
  6410. int callerid_fluff;
  6411. if (ast_strlen_zero(extension)) {
  6412. ast_log(LOG_ERROR,"You have to be kidding-- add exten '' to context %s? Figure out a name and call me back. Action ignored.\n",
  6413. con->name);
  6414. return -1;
  6415. }
  6416. /* If we are adding a hint evalulate in variables and global variables */
  6417. if (priority == PRIORITY_HINT && strstr(application, "${") && extension[0] != '_') {
  6418. int inhibited;
  6419. struct ast_channel *c = ast_dummy_channel_alloc();
  6420. if (c) {
  6421. ast_channel_exten_set(c, extension);
  6422. ast_channel_context_set(c, con->name);
  6423. }
  6424. /*
  6425. * We can allow dangerous functions when adding a hint since
  6426. * altering dialplan is itself a privileged activity. Otherwise,
  6427. * we could never execute dangerous functions.
  6428. */
  6429. inhibited = ast_thread_inhibit_escalations_swap(0);
  6430. pbx_substitute_variables_helper(c, application, expand_buf, sizeof(expand_buf));
  6431. if (0 < inhibited) {
  6432. ast_thread_inhibit_escalations();
  6433. }
  6434. application = expand_buf;
  6435. if (c) {
  6436. ast_channel_unref(c);
  6437. }
  6438. }
  6439. exten_fluff = ext_fluff_count(extension);
  6440. callerid_fluff = callerid ? ext_fluff_count(callerid) : 0;
  6441. length = sizeof(struct ast_exten);
  6442. length += strlen(extension) + 1;
  6443. if (exten_fluff) {
  6444. length += strlen(extension) + 1 - exten_fluff;
  6445. }
  6446. length += strlen(application) + 1;
  6447. if (label) {
  6448. length += strlen(label) + 1;
  6449. }
  6450. if (callerid) {
  6451. length += strlen(callerid) + 1;
  6452. if (callerid_fluff) {
  6453. length += strlen(callerid) + 1 - callerid_fluff;
  6454. }
  6455. } else {
  6456. length ++; /* just the '\0' */
  6457. }
  6458. /* Be optimistic: Build the extension structure first */
  6459. if (!(tmp = ast_calloc(1, length)))
  6460. return -1;
  6461. if (ast_strlen_zero(label)) /* let's turn empty labels to a null ptr */
  6462. label = 0;
  6463. /* use p as dst in assignments, as the fields are const char * */
  6464. p = tmp->stuff;
  6465. if (label) {
  6466. tmp->label = p;
  6467. strcpy(p, label);
  6468. p += strlen(label) + 1;
  6469. }
  6470. tmp->name = p;
  6471. p += ext_strncpy(p, extension, strlen(extension) + 1, 0);
  6472. if (exten_fluff) {
  6473. tmp->exten = p;
  6474. p += ext_strncpy(p, extension, strlen(extension) + 1 - exten_fluff, 1);
  6475. } else {
  6476. /* no fluff, we don't need a copy. */
  6477. tmp->exten = tmp->name;
  6478. }
  6479. tmp->priority = priority;
  6480. tmp->cidmatch_display = tmp->cidmatch = p; /* but use p for assignments below */
  6481. /* Blank callerid and NULL callerid are two SEPARATE things. Do NOT confuse the two!!! */
  6482. if (callerid) {
  6483. p += ext_strncpy(p, callerid, strlen(callerid) + 1, 0);
  6484. if (callerid_fluff) {
  6485. tmp->cidmatch = p;
  6486. p += ext_strncpy(p, callerid, strlen(callerid) + 1 - callerid_fluff, 1);
  6487. }
  6488. tmp->matchcid = AST_EXT_MATCHCID_ON;
  6489. } else {
  6490. *p++ = '\0';
  6491. tmp->matchcid = AST_EXT_MATCHCID_OFF;
  6492. }
  6493. tmp->app = p;
  6494. strcpy(p, application);
  6495. tmp->parent = con;
  6496. tmp->data = data;
  6497. tmp->datad = datad;
  6498. tmp->registrar = registrar;
  6499. if (lock_context) {
  6500. ast_wrlock_context(con);
  6501. }
  6502. if (con->pattern_tree) { /* usually, on initial load, the pattern_tree isn't formed until the first find_exten; so if we are adding
  6503. an extension, and the trie exists, then we need to incrementally add this pattern to it. */
  6504. ext_strncpy(dummy_name, tmp->exten, sizeof(dummy_name), 1);
  6505. dummy_exten.exten = dummy_name;
  6506. dummy_exten.matchcid = AST_EXT_MATCHCID_OFF;
  6507. dummy_exten.cidmatch = 0;
  6508. tmp2 = ast_hashtab_lookup(con->root_table, &dummy_exten);
  6509. if (!tmp2) {
  6510. /* hmmm, not in the trie; */
  6511. add_exten_to_pattern_tree(con, tmp, 0);
  6512. ast_hashtab_insert_safe(con->root_table, tmp); /* for the sake of completeness */
  6513. }
  6514. }
  6515. res = 0; /* some compilers will think it is uninitialized otherwise */
  6516. for (e = con->root; e; el = e, e = e->next) { /* scan the extension list */
  6517. res = ext_cmp(e->exten, tmp->exten);
  6518. if (res == 0) { /* extension match, now look at cidmatch */
  6519. if (e->matchcid == AST_EXT_MATCHCID_OFF && tmp->matchcid == AST_EXT_MATCHCID_OFF)
  6520. res = 0;
  6521. else if (tmp->matchcid == AST_EXT_MATCHCID_ON && e->matchcid == AST_EXT_MATCHCID_OFF)
  6522. res = 1;
  6523. else if (e->matchcid == AST_EXT_MATCHCID_ON && tmp->matchcid == AST_EXT_MATCHCID_OFF)
  6524. res = -1;
  6525. else
  6526. res = ext_cmp(e->cidmatch, tmp->cidmatch);
  6527. }
  6528. if (res >= 0)
  6529. break;
  6530. }
  6531. if (e && res == 0) { /* exact match, insert in the priority chain */
  6532. res = add_priority(con, tmp, el, e, replace);
  6533. if (res < 0) {
  6534. if (con->pattern_tree) {
  6535. struct match_char *x = add_exten_to_pattern_tree(con, tmp, 1);
  6536. if (x->exten) {
  6537. x->deleted = 1;
  6538. x->exten = 0;
  6539. }
  6540. ast_hashtab_remove_this_object(con->root_table, tmp);
  6541. }
  6542. if (tmp->datad) {
  6543. tmp->datad(tmp->data);
  6544. /* if you free this, null it out */
  6545. tmp->data = NULL;
  6546. }
  6547. ast_free(tmp);
  6548. }
  6549. if (lock_context) {
  6550. ast_unlock_context(con);
  6551. }
  6552. if (res < 0) {
  6553. errno = EEXIST;
  6554. return -1;
  6555. }
  6556. } else {
  6557. /*
  6558. * not an exact match, this is the first entry with this pattern,
  6559. * so insert in the main list right before 'e' (if any)
  6560. */
  6561. tmp->next = e;
  6562. if (el) { /* there is another exten already in this context */
  6563. el->next = tmp;
  6564. tmp->peer_table = ast_hashtab_create(13,
  6565. hashtab_compare_exten_numbers,
  6566. ast_hashtab_resize_java,
  6567. ast_hashtab_newsize_java,
  6568. hashtab_hash_priority,
  6569. 0);
  6570. tmp->peer_label_table = ast_hashtab_create(7,
  6571. hashtab_compare_exten_labels,
  6572. ast_hashtab_resize_java,
  6573. ast_hashtab_newsize_java,
  6574. hashtab_hash_labels,
  6575. 0);
  6576. if (label) {
  6577. ast_hashtab_insert_safe(tmp->peer_label_table, tmp);
  6578. }
  6579. ast_hashtab_insert_safe(tmp->peer_table, tmp);
  6580. } else { /* this is the first exten in this context */
  6581. if (!con->root_table)
  6582. con->root_table = ast_hashtab_create(27,
  6583. hashtab_compare_extens,
  6584. ast_hashtab_resize_java,
  6585. ast_hashtab_newsize_java,
  6586. hashtab_hash_extens,
  6587. 0);
  6588. con->root = tmp;
  6589. con->root->peer_table = ast_hashtab_create(13,
  6590. hashtab_compare_exten_numbers,
  6591. ast_hashtab_resize_java,
  6592. ast_hashtab_newsize_java,
  6593. hashtab_hash_priority,
  6594. 0);
  6595. con->root->peer_label_table = ast_hashtab_create(7,
  6596. hashtab_compare_exten_labels,
  6597. ast_hashtab_resize_java,
  6598. ast_hashtab_newsize_java,
  6599. hashtab_hash_labels,
  6600. 0);
  6601. if (label) {
  6602. ast_hashtab_insert_safe(con->root->peer_label_table, tmp);
  6603. }
  6604. ast_hashtab_insert_safe(con->root->peer_table, tmp);
  6605. }
  6606. ast_hashtab_insert_safe(con->root_table, tmp);
  6607. if (lock_context) {
  6608. ast_unlock_context(con);
  6609. }
  6610. if (tmp->priority == PRIORITY_HINT) {
  6611. ast_add_hint(tmp);
  6612. }
  6613. }
  6614. if (DEBUG_ATLEAST(1)) {
  6615. if (tmp->matchcid == AST_EXT_MATCHCID_ON) {
  6616. ast_log(LOG_DEBUG, "Added extension '%s' priority %d (CID match '%s') to %s (%p)\n",
  6617. tmp->name, tmp->priority, tmp->cidmatch_display, con->name, con);
  6618. } else {
  6619. ast_log(LOG_DEBUG, "Added extension '%s' priority %d to %s (%p)\n",
  6620. tmp->name, tmp->priority, con->name, con);
  6621. }
  6622. }
  6623. return 0;
  6624. }
  6625. /*! \brief Structure which contains information about an outgoing dial */
  6626. struct pbx_outgoing {
  6627. /*! \brief Dialing structure being used */
  6628. struct ast_dial *dial;
  6629. /*! \brief Condition for synchronous dialing */
  6630. ast_cond_t cond;
  6631. /*! \brief Application to execute */
  6632. char app[AST_MAX_APP];
  6633. /*! \brief Application data to pass to application */
  6634. char *appdata;
  6635. /*! \brief Dialplan context */
  6636. char context[AST_MAX_CONTEXT];
  6637. /*! \brief Dialplan extension */
  6638. char exten[AST_MAX_EXTENSION];
  6639. /*! \brief Dialplan priority */
  6640. int priority;
  6641. /*! \brief Result of the dial operation when dialed is set */
  6642. int dial_res;
  6643. /*! \brief Set when dialing is completed */
  6644. unsigned int dialed:1;
  6645. /*! \brief Set if we've spawned a thread to do our work */
  6646. unsigned int in_separate_thread:1;
  6647. };
  6648. /*! \brief Destructor for outgoing structure */
  6649. static void pbx_outgoing_destroy(void *obj)
  6650. {
  6651. struct pbx_outgoing *outgoing = obj;
  6652. if (outgoing->dial) {
  6653. ast_dial_destroy(outgoing->dial);
  6654. }
  6655. ast_cond_destroy(&outgoing->cond);
  6656. ast_free(outgoing->appdata);
  6657. }
  6658. /*! \brief Internal function which dials an outgoing leg and sends it to a provided extension or application */
  6659. static void *pbx_outgoing_exec(void *data)
  6660. {
  6661. RAII_VAR(struct pbx_outgoing *, outgoing, data, ao2_cleanup);
  6662. enum ast_dial_result res;
  6663. res = ast_dial_run(outgoing->dial, NULL, 0);
  6664. if (outgoing->in_separate_thread) {
  6665. /* Notify anyone interested that dialing is complete */
  6666. ao2_lock(outgoing);
  6667. outgoing->dial_res = res;
  6668. outgoing->dialed = 1;
  6669. ast_cond_signal(&outgoing->cond);
  6670. ao2_unlock(outgoing);
  6671. } else {
  6672. /* We still need the dial result, but we don't need to lock */
  6673. outgoing->dial_res = res;
  6674. }
  6675. /* If the outgoing leg was not answered we can immediately return and go no further */
  6676. if (res != AST_DIAL_RESULT_ANSWERED) {
  6677. return NULL;
  6678. }
  6679. if (!ast_strlen_zero(outgoing->app)) {
  6680. struct ast_app *app = pbx_findapp(outgoing->app);
  6681. if (app) {
  6682. ast_verb(4, "Launching %s(%s) on %s\n", outgoing->app, S_OR(outgoing->appdata, ""),
  6683. ast_channel_name(ast_dial_answered(outgoing->dial)));
  6684. pbx_exec(ast_dial_answered(outgoing->dial), app, outgoing->appdata);
  6685. } else {
  6686. ast_log(LOG_WARNING, "No such application '%s'\n", outgoing->app);
  6687. }
  6688. } else {
  6689. struct ast_channel *answered = ast_dial_answered(outgoing->dial);
  6690. if (!ast_strlen_zero(outgoing->context)) {
  6691. ast_channel_context_set(answered, outgoing->context);
  6692. }
  6693. if (!ast_strlen_zero(outgoing->exten)) {
  6694. ast_channel_exten_set(answered, outgoing->exten);
  6695. }
  6696. if (outgoing->priority > 0) {
  6697. ast_channel_priority_set(answered, outgoing->priority);
  6698. }
  6699. if (ast_pbx_run(answered)) {
  6700. ast_log(LOG_ERROR, "Failed to start PBX on %s\n", ast_channel_name(answered));
  6701. } else {
  6702. /* PBX will have taken care of hanging up, so we steal the answered channel so dial doesn't do it */
  6703. ast_dial_answered_steal(outgoing->dial);
  6704. }
  6705. }
  6706. return NULL;
  6707. }
  6708. /*! \brief Internal dialing state callback which causes early media to trigger an answer */
  6709. static void pbx_outgoing_state_callback(struct ast_dial *dial)
  6710. {
  6711. struct ast_channel *channel;
  6712. if (ast_dial_state(dial) != AST_DIAL_RESULT_PROGRESS) {
  6713. return;
  6714. }
  6715. if (!(channel = ast_dial_get_channel(dial, 0))) {
  6716. return;
  6717. }
  6718. ast_verb(4, "Treating progress as answer on '%s' due to early media option\n",
  6719. ast_channel_name(channel));
  6720. ast_queue_control(channel, AST_CONTROL_ANSWER);
  6721. }
  6722. /*!
  6723. * \brief Attempt to convert disconnect cause to old originate reason.
  6724. *
  6725. * \todo XXX The old originate reasons need to be trashed and replaced
  6726. * with normal disconnect cause codes if the call was not answered.
  6727. * The internal consumers of the reason values would also need to be
  6728. * updated: app_originate, call files, and AMI OriginateResponse.
  6729. */
  6730. static enum ast_control_frame_type pbx_dial_reason(enum ast_dial_result dial_result, int cause)
  6731. {
  6732. enum ast_control_frame_type pbx_reason;
  6733. if (dial_result == AST_DIAL_RESULT_ANSWERED) {
  6734. /* Remote end answered. */
  6735. pbx_reason = AST_CONTROL_ANSWER;
  6736. } else if (dial_result == AST_DIAL_RESULT_HANGUP) {
  6737. /* Caller hungup */
  6738. pbx_reason = AST_CONTROL_HANGUP;
  6739. } else {
  6740. switch (cause) {
  6741. case AST_CAUSE_USER_BUSY:
  6742. pbx_reason = AST_CONTROL_BUSY;
  6743. break;
  6744. case AST_CAUSE_CALL_REJECTED:
  6745. case AST_CAUSE_NETWORK_OUT_OF_ORDER:
  6746. case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
  6747. case AST_CAUSE_NORMAL_TEMPORARY_FAILURE:
  6748. case AST_CAUSE_SWITCH_CONGESTION:
  6749. case AST_CAUSE_NORMAL_CIRCUIT_CONGESTION:
  6750. pbx_reason = AST_CONTROL_CONGESTION;
  6751. break;
  6752. case AST_CAUSE_ANSWERED_ELSEWHERE:
  6753. case AST_CAUSE_NO_ANSWER:
  6754. /* Remote end was ringing (but isn't anymore) */
  6755. pbx_reason = AST_CONTROL_RINGING;
  6756. break;
  6757. case AST_CAUSE_UNALLOCATED:
  6758. default:
  6759. /* Call Failure (not BUSY, and not NO_ANSWER, maybe Circuit busy or down?) */
  6760. pbx_reason = 0;
  6761. break;
  6762. }
  6763. }
  6764. return pbx_reason;
  6765. }
  6766. static int pbx_outgoing_attempt(const char *type, struct ast_format_cap *cap,
  6767. const char *addr, int timeout, const char *context, const char *exten, int priority,
  6768. const char *app, const char *appdata, int *reason, int synchronous,
  6769. const char *cid_num, const char *cid_name, struct ast_variable *vars,
  6770. const char *account, struct ast_channel **locked_channel, int early_media,
  6771. const struct ast_assigned_ids *assignedids)
  6772. {
  6773. RAII_VAR(struct pbx_outgoing *, outgoing, NULL, ao2_cleanup);
  6774. struct ast_channel *dialed;
  6775. pthread_t thread;
  6776. outgoing = ao2_alloc(sizeof(*outgoing), pbx_outgoing_destroy);
  6777. if (!outgoing) {
  6778. return -1;
  6779. }
  6780. ast_cond_init(&outgoing->cond, NULL);
  6781. if (!ast_strlen_zero(app)) {
  6782. ast_copy_string(outgoing->app, app, sizeof(outgoing->app));
  6783. outgoing->appdata = ast_strdup(appdata);
  6784. } else {
  6785. ast_copy_string(outgoing->context, context, sizeof(outgoing->context));
  6786. ast_copy_string(outgoing->exten, exten, sizeof(outgoing->exten));
  6787. outgoing->priority = priority;
  6788. }
  6789. if (!(outgoing->dial = ast_dial_create())) {
  6790. return -1;
  6791. }
  6792. if (ast_dial_append(outgoing->dial, type, addr, assignedids)) {
  6793. return -1;
  6794. }
  6795. ast_dial_set_global_timeout(outgoing->dial, timeout);
  6796. if (ast_dial_prerun(outgoing->dial, NULL, cap)) {
  6797. if (synchronous && reason) {
  6798. *reason = pbx_dial_reason(AST_DIAL_RESULT_FAILED,
  6799. ast_dial_reason(outgoing->dial, 0));
  6800. }
  6801. return -1;
  6802. }
  6803. dialed = ast_dial_get_channel(outgoing->dial, 0);
  6804. if (!dialed) {
  6805. return -1;
  6806. }
  6807. ast_channel_lock(dialed);
  6808. if (vars) {
  6809. ast_set_variables(dialed, vars);
  6810. }
  6811. if (!ast_strlen_zero(account)) {
  6812. ast_channel_stage_snapshot(dialed);
  6813. ast_channel_accountcode_set(dialed, account);
  6814. ast_channel_peeraccount_set(dialed, account);
  6815. ast_channel_stage_snapshot_done(dialed);
  6816. }
  6817. ast_set_flag(ast_channel_flags(dialed), AST_FLAG_ORIGINATED);
  6818. ast_channel_unlock(dialed);
  6819. if (!ast_strlen_zero(cid_num) || !ast_strlen_zero(cid_name)) {
  6820. struct ast_party_connected_line connected;
  6821. /*
  6822. * It seems strange to set the CallerID on an outgoing call leg
  6823. * to whom we are calling, but this function's callers are doing
  6824. * various Originate methods. This call leg goes to the local
  6825. * user. Once the called party answers, the dialplan needs to
  6826. * be able to access the CallerID from the CALLERID function as
  6827. * if the called party had placed this call.
  6828. */
  6829. ast_set_callerid(dialed, cid_num, cid_name, cid_num);
  6830. ast_party_connected_line_set_init(&connected, ast_channel_connected(dialed));
  6831. if (!ast_strlen_zero(cid_num)) {
  6832. connected.id.number.valid = 1;
  6833. connected.id.number.str = (char *) cid_num;
  6834. connected.id.number.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
  6835. }
  6836. if (!ast_strlen_zero(cid_name)) {
  6837. connected.id.name.valid = 1;
  6838. connected.id.name.str = (char *) cid_name;
  6839. connected.id.name.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
  6840. }
  6841. ast_channel_set_connected_line(dialed, &connected, NULL);
  6842. }
  6843. if (early_media) {
  6844. ast_dial_set_state_callback(outgoing->dial, pbx_outgoing_state_callback);
  6845. }
  6846. if (locked_channel) {
  6847. /*
  6848. * Keep a dialed channel ref since the caller wants
  6849. * the channel returned. We must get the ref before
  6850. * spawning off pbx_outgoing_exec().
  6851. */
  6852. ast_channel_ref(dialed);
  6853. if (!synchronous) {
  6854. /*
  6855. * Lock it now to hold off pbx_outgoing_exec() in case the
  6856. * calling function needs the channel state/snapshot before
  6857. * dialing actually happens.
  6858. */
  6859. ast_channel_lock(dialed);
  6860. }
  6861. }
  6862. /* This extra reference is dereferenced by pbx_outgoing_exec */
  6863. ao2_ref(outgoing, +1);
  6864. if (synchronous == AST_OUTGOING_WAIT_COMPLETE) {
  6865. /*
  6866. * Because we are waiting until this is complete anyway, there is no
  6867. * sense in creating another thread that we will just need to wait
  6868. * for, so instead we commandeer the current thread.
  6869. */
  6870. pbx_outgoing_exec(outgoing);
  6871. } else {
  6872. outgoing->in_separate_thread = 1;
  6873. if (ast_pthread_create_detached(&thread, NULL, pbx_outgoing_exec, outgoing)) {
  6874. ast_log(LOG_WARNING, "Unable to spawn dialing thread for '%s/%s'\n", type, addr);
  6875. ao2_ref(outgoing, -1);
  6876. if (locked_channel) {
  6877. if (!synchronous) {
  6878. ast_channel_unlock(dialed);
  6879. }
  6880. ast_channel_unref(dialed);
  6881. }
  6882. return -1;
  6883. }
  6884. if (synchronous) {
  6885. ao2_lock(outgoing);
  6886. /* Wait for dialing to complete */
  6887. while (!outgoing->dialed) {
  6888. ast_cond_wait(&outgoing->cond, ao2_object_get_lockaddr(outgoing));
  6889. }
  6890. ao2_unlock(outgoing);
  6891. }
  6892. }
  6893. if (synchronous) {
  6894. /* Determine the outcome of the dialing attempt up to it being answered. */
  6895. if (reason) {
  6896. *reason = pbx_dial_reason(outgoing->dial_res,
  6897. ast_dial_reason(outgoing->dial, 0));
  6898. }
  6899. if (outgoing->dial_res != AST_DIAL_RESULT_ANSWERED) {
  6900. /* The dial operation failed. */
  6901. if (locked_channel) {
  6902. ast_channel_unref(dialed);
  6903. }
  6904. return -1;
  6905. }
  6906. if (locked_channel) {
  6907. ast_channel_lock(dialed);
  6908. }
  6909. }
  6910. if (locked_channel) {
  6911. *locked_channel = dialed;
  6912. }
  6913. return 0;
  6914. }
  6915. int ast_pbx_outgoing_exten(const char *type, struct ast_format_cap *cap, const char *addr,
  6916. int timeout, const char *context, const char *exten, int priority, int *reason,
  6917. int synchronous, const char *cid_num, const char *cid_name, struct ast_variable *vars,
  6918. const char *account, struct ast_channel **locked_channel, int early_media,
  6919. const struct ast_assigned_ids *assignedids)
  6920. {
  6921. int res;
  6922. int my_reason;
  6923. if (!reason) {
  6924. reason = &my_reason;
  6925. }
  6926. *reason = 0;
  6927. if (locked_channel) {
  6928. *locked_channel = NULL;
  6929. }
  6930. res = pbx_outgoing_attempt(type, cap, addr, timeout, context, exten, priority,
  6931. NULL, NULL, reason, synchronous, cid_num, cid_name, vars, account, locked_channel,
  6932. early_media, assignedids);
  6933. if (res < 0 /* Call failed to get connected for some reason. */
  6934. && 0 < synchronous
  6935. && ast_exists_extension(NULL, context, "failed", 1, NULL)) {
  6936. struct ast_channel *failed;
  6937. /* We do not have to worry about a locked_channel if dialing failed. */
  6938. ast_assert(!locked_channel || !*locked_channel);
  6939. /*!
  6940. * \todo XXX Not good. The channel name is not unique if more than
  6941. * one originate fails at a time.
  6942. */
  6943. failed = ast_channel_alloc(0, AST_STATE_DOWN, cid_num, cid_name, account,
  6944. "failed", context, NULL, NULL, 0, "OutgoingSpoolFailed");
  6945. if (failed) {
  6946. char failed_reason[12];
  6947. ast_set_variables(failed, vars);
  6948. snprintf(failed_reason, sizeof(failed_reason), "%d", *reason);
  6949. pbx_builtin_setvar_helper(failed, "REASON", failed_reason);
  6950. ast_channel_unlock(failed);
  6951. if (ast_pbx_run(failed)) {
  6952. ast_log(LOG_ERROR, "Unable to run PBX on '%s'\n",
  6953. ast_channel_name(failed));
  6954. ast_hangup(failed);
  6955. }
  6956. }
  6957. }
  6958. return res;
  6959. }
  6960. int ast_pbx_outgoing_app(const char *type, struct ast_format_cap *cap, const char *addr,
  6961. int timeout, const char *app, const char *appdata, int *reason, int synchronous,
  6962. const char *cid_num, const char *cid_name, struct ast_variable *vars,
  6963. const char *account, struct ast_channel **locked_channel,
  6964. const struct ast_assigned_ids *assignedids)
  6965. {
  6966. if (reason) {
  6967. *reason = 0;
  6968. }
  6969. if (locked_channel) {
  6970. *locked_channel = NULL;
  6971. }
  6972. if (ast_strlen_zero(app)) {
  6973. return -1;
  6974. }
  6975. return pbx_outgoing_attempt(type, cap, addr, timeout, NULL, NULL, 0, app, appdata,
  6976. reason, synchronous, cid_num, cid_name, vars, account, locked_channel, 0,
  6977. assignedids);
  6978. }
  6979. /* this is the guts of destroying a context --
  6980. freeing up the structure, traversing and destroying the
  6981. extensions, switches, ignorepats, includes, etc. etc. */
  6982. static void __ast_internal_context_destroy( struct ast_context *con)
  6983. {
  6984. struct ast_include *tmpi;
  6985. struct ast_sw *sw;
  6986. struct ast_exten *e, *el, *en;
  6987. struct ast_ignorepat *ipi;
  6988. struct ast_context *tmp = con;
  6989. for (tmpi = tmp->includes; tmpi; ) { /* Free includes */
  6990. struct ast_include *tmpil = tmpi;
  6991. tmpi = tmpi->next;
  6992. include_free(tmpil);
  6993. }
  6994. for (ipi = tmp->ignorepats; ipi; ) { /* Free ignorepats */
  6995. struct ast_ignorepat *ipl = ipi;
  6996. ipi = ipi->next;
  6997. ast_free(ipl);
  6998. }
  6999. if (tmp->registrar)
  7000. ast_free(tmp->registrar);
  7001. /* destroy the hash tabs */
  7002. if (tmp->root_table) {
  7003. ast_hashtab_destroy(tmp->root_table, 0);
  7004. }
  7005. /* and destroy the pattern tree */
  7006. if (tmp->pattern_tree)
  7007. destroy_pattern_tree(tmp->pattern_tree);
  7008. while ((sw = AST_LIST_REMOVE_HEAD(&tmp->alts, list)))
  7009. ast_free(sw);
  7010. for (e = tmp->root; e;) {
  7011. for (en = e->peer; en;) {
  7012. el = en;
  7013. en = en->peer;
  7014. destroy_exten(el);
  7015. }
  7016. el = e;
  7017. e = e->next;
  7018. destroy_exten(el);
  7019. }
  7020. tmp->root = NULL;
  7021. ast_rwlock_destroy(&tmp->lock);
  7022. ast_mutex_destroy(&tmp->macrolock);
  7023. ast_free(tmp);
  7024. }
  7025. void __ast_context_destroy(struct ast_context *list, struct ast_hashtab *contexttab, struct ast_context *con, const char *registrar)
  7026. {
  7027. struct ast_context *tmp, *tmpl=NULL;
  7028. struct ast_exten *exten_item, *prio_item;
  7029. for (tmp = list; tmp; ) {
  7030. struct ast_context *next = NULL; /* next starting point */
  7031. /* The following code used to skip forward to the next
  7032. context with matching registrar, but this didn't
  7033. make sense; individual priorities registrar'd to
  7034. the matching registrar could occur in any context! */
  7035. ast_debug(1, "Investigate ctx %s %s\n", tmp->name, tmp->registrar);
  7036. if (con) {
  7037. for (; tmp; tmpl = tmp, tmp = tmp->next) { /* skip to the matching context */
  7038. ast_debug(1, "check ctx %s %s\n", tmp->name, tmp->registrar);
  7039. if ( !strcasecmp(tmp->name, con->name) ) {
  7040. break; /* found it */
  7041. }
  7042. }
  7043. }
  7044. if (!tmp) /* not found, we are done */
  7045. break;
  7046. ast_wrlock_context(tmp);
  7047. if (registrar) {
  7048. /* then search thru and remove any extens that match registrar. */
  7049. struct ast_hashtab_iter *exten_iter;
  7050. struct ast_hashtab_iter *prio_iter;
  7051. struct ast_ignorepat *ip, *ipl = NULL, *ipn = NULL;
  7052. struct ast_include *i, *pi = NULL, *ni = NULL;
  7053. struct ast_sw *sw = NULL;
  7054. /* remove any ignorepats whose registrar matches */
  7055. for (ip = tmp->ignorepats; ip; ip = ipn) {
  7056. ipn = ip->next;
  7057. if (!strcmp(ip->registrar, registrar)) {
  7058. if (ipl) {
  7059. ipl->next = ip->next;
  7060. ast_free(ip);
  7061. continue; /* don't change ipl */
  7062. } else {
  7063. tmp->ignorepats = ip->next;
  7064. ast_free(ip);
  7065. continue; /* don't change ipl */
  7066. }
  7067. }
  7068. ipl = ip;
  7069. }
  7070. /* remove any includes whose registrar matches */
  7071. for (i = tmp->includes; i; i = ni) {
  7072. ni = i->next;
  7073. if (strcmp(i->registrar, registrar) == 0) {
  7074. /* remove from list */
  7075. if (pi) {
  7076. pi->next = i->next;
  7077. /* free include */
  7078. include_free(i);
  7079. continue; /* don't change pi */
  7080. } else {
  7081. tmp->includes = i->next;
  7082. /* free include */
  7083. include_free(i);
  7084. continue; /* don't change pi */
  7085. }
  7086. }
  7087. pi = i;
  7088. }
  7089. /* remove any switches whose registrar matches */
  7090. AST_LIST_TRAVERSE_SAFE_BEGIN(&tmp->alts, sw, list) {
  7091. if (strcmp(sw->registrar,registrar) == 0) {
  7092. AST_LIST_REMOVE_CURRENT(list);
  7093. ast_free(sw);
  7094. }
  7095. }
  7096. AST_LIST_TRAVERSE_SAFE_END;
  7097. if (tmp->root_table) { /* it is entirely possible that the context is EMPTY */
  7098. exten_iter = ast_hashtab_start_traversal(tmp->root_table);
  7099. while ((exten_item=ast_hashtab_next(exten_iter))) {
  7100. int end_traversal = 1;
  7101. /*
  7102. * If the extension could not be removed from the root_table due to
  7103. * a loaded PBX app, it can exist here but have its peer_table be
  7104. * destroyed due to a previous pass through this function.
  7105. */
  7106. if (!exten_item->peer_table) {
  7107. continue;
  7108. }
  7109. prio_iter = ast_hashtab_start_traversal(exten_item->peer_table);
  7110. while ((prio_item=ast_hashtab_next(prio_iter))) {
  7111. char extension[AST_MAX_EXTENSION];
  7112. char cidmatch[AST_MAX_EXTENSION];
  7113. if (!prio_item->registrar || strcmp(prio_item->registrar, registrar) != 0) {
  7114. continue;
  7115. }
  7116. ast_verb(3, "Remove %s/%s/%d, registrar=%s; con=%s(%p); con->root=%p\n",
  7117. tmp->name, prio_item->name, prio_item->priority, registrar, con? con->name : "<nil>", con, con? con->root_table: NULL);
  7118. ast_copy_string(extension, prio_item->exten, sizeof(extension));
  7119. if (prio_item->cidmatch) {
  7120. ast_copy_string(cidmatch, prio_item->cidmatch, sizeof(cidmatch));
  7121. }
  7122. end_traversal &= ast_context_remove_extension_callerid2(tmp, extension, prio_item->priority, cidmatch, prio_item->matchcid, NULL, 1);
  7123. }
  7124. /* Explanation:
  7125. * ast_context_remove_extension_callerid2 will destroy the extension that it comes across. This
  7126. * destruction includes destroying the exten's peer_table, which we are currently traversing. If
  7127. * ast_context_remove_extension_callerid2 ever should return '0' then this means we have destroyed
  7128. * the hashtable which we are traversing, and thus calling ast_hashtab_end_traversal will result
  7129. * in reading invalid memory. Thus, if we detect that we destroyed the hashtable, then we will simply
  7130. * free the iterator
  7131. */
  7132. if (end_traversal) {
  7133. ast_hashtab_end_traversal(prio_iter);
  7134. } else {
  7135. ast_free(prio_iter);
  7136. }
  7137. }
  7138. ast_hashtab_end_traversal(exten_iter);
  7139. }
  7140. /* delete the context if it's registrar matches, is empty, has refcount of 1, */
  7141. /* it's not empty, if it has includes, ignorepats, or switches that are registered from
  7142. another registrar. It's not empty if there are any extensions */
  7143. if (strcmp(tmp->registrar, registrar) == 0 && tmp->refcount < 2 && !tmp->root && !tmp->ignorepats && !tmp->includes && AST_LIST_EMPTY(&tmp->alts)) {
  7144. ast_debug(1, "delete ctx %s %s\n", tmp->name, tmp->registrar);
  7145. ast_hashtab_remove_this_object(contexttab, tmp);
  7146. next = tmp->next;
  7147. if (tmpl)
  7148. tmpl->next = next;
  7149. else
  7150. contexts = next;
  7151. /* Okay, now we're safe to let it go -- in a sense, we were
  7152. ready to let it go as soon as we locked it. */
  7153. ast_unlock_context(tmp);
  7154. __ast_internal_context_destroy(tmp);
  7155. } else {
  7156. ast_debug(1,"Couldn't delete ctx %s/%s; refc=%d; tmp.root=%p\n", tmp->name, tmp->registrar,
  7157. tmp->refcount, tmp->root);
  7158. ast_unlock_context(tmp);
  7159. next = tmp->next;
  7160. tmpl = tmp;
  7161. }
  7162. } else if (con) {
  7163. ast_verb(3, "Deleting context %s registrar=%s\n", tmp->name, tmp->registrar);
  7164. ast_debug(1, "delete ctx %s %s\n", tmp->name, tmp->registrar);
  7165. ast_hashtab_remove_this_object(contexttab, tmp);
  7166. next = tmp->next;
  7167. if (tmpl)
  7168. tmpl->next = next;
  7169. else
  7170. contexts = next;
  7171. /* Okay, now we're safe to let it go -- in a sense, we were
  7172. ready to let it go as soon as we locked it. */
  7173. ast_unlock_context(tmp);
  7174. __ast_internal_context_destroy(tmp);
  7175. }
  7176. /* if we have a specific match, we are done, otherwise continue */
  7177. tmp = con ? NULL : next;
  7178. }
  7179. }
  7180. int ast_context_destroy_by_name(const char *context, const char *registrar)
  7181. {
  7182. struct ast_context *con;
  7183. int ret = -1;
  7184. ast_wrlock_contexts();
  7185. con = ast_context_find(context);
  7186. if (con) {
  7187. ast_context_destroy(con, registrar);
  7188. ret = 0;
  7189. }
  7190. ast_unlock_contexts();
  7191. return ret;
  7192. }
  7193. void ast_context_destroy(struct ast_context *con, const char *registrar)
  7194. {
  7195. ast_wrlock_contexts();
  7196. __ast_context_destroy(contexts, contexts_table, con,registrar);
  7197. ast_unlock_contexts();
  7198. }
  7199. void wait_for_hangup(struct ast_channel *chan, const void *data)
  7200. {
  7201. int res;
  7202. struct ast_frame *f;
  7203. double waitsec;
  7204. int waittime;
  7205. if (ast_strlen_zero(data) || (sscanf(data, "%30lg", &waitsec) != 1) || (waitsec < 0))
  7206. waitsec = -1;
  7207. if (waitsec > -1) {
  7208. waittime = waitsec * 1000.0;
  7209. ast_safe_sleep(chan, waittime);
  7210. } else do {
  7211. res = ast_waitfor(chan, -1);
  7212. if (res < 0)
  7213. return;
  7214. f = ast_read(chan);
  7215. if (f)
  7216. ast_frfree(f);
  7217. } while(f);
  7218. }
  7219. /*!
  7220. * \ingroup functions
  7221. */
  7222. static int testtime_write(struct ast_channel *chan, const char *cmd, char *var, const char *value)
  7223. {
  7224. struct ast_tm tm;
  7225. struct timeval tv;
  7226. char *remainder, result[30], timezone[80];
  7227. /* Turn off testing? */
  7228. if (!pbx_checkcondition(value)) {
  7229. pbx_builtin_setvar_helper(chan, "TESTTIME", NULL);
  7230. return 0;
  7231. }
  7232. /* Parse specified time */
  7233. if (!(remainder = ast_strptime(value, "%Y/%m/%d %H:%M:%S", &tm))) {
  7234. return -1;
  7235. }
  7236. sscanf(remainder, "%79s", timezone);
  7237. tv = ast_mktime(&tm, S_OR(timezone, NULL));
  7238. snprintf(result, sizeof(result), "%ld", (long) tv.tv_sec);
  7239. pbx_builtin_setvar_helper(chan, "__TESTTIME", result);
  7240. return 0;
  7241. }
  7242. static struct ast_custom_function testtime_function = {
  7243. .name = "TESTTIME",
  7244. .write = testtime_write,
  7245. };
  7246. int pbx_checkcondition(const char *condition)
  7247. {
  7248. int res;
  7249. if (ast_strlen_zero(condition)) { /* NULL or empty strings are false */
  7250. return 0;
  7251. } else if (sscanf(condition, "%30d", &res) == 1) { /* Numbers are evaluated for truth */
  7252. return res;
  7253. } else { /* Strings are true */
  7254. return 1;
  7255. }
  7256. }
  7257. static void presence_state_cb(void *unused, struct stasis_subscription *sub, struct stasis_message *msg)
  7258. {
  7259. struct ast_hint *hint;
  7260. struct ast_str *hint_app = NULL;
  7261. struct ao2_iterator hint_iter;
  7262. if (handle_hint_change_message_type(msg, AST_HINT_UPDATE_PRESENCE)) {
  7263. return;
  7264. }
  7265. if (stasis_message_type(msg) != ast_presence_state_message_type()) {
  7266. return;
  7267. }
  7268. hint_app = ast_str_create(1024);
  7269. if (!hint_app) {
  7270. return;
  7271. }
  7272. ast_mutex_lock(&context_merge_lock);/* Hold off ast_merge_contexts_and_delete */
  7273. hint_iter = ao2_iterator_init(hints, 0);
  7274. for (; (hint = ao2_iterator_next(&hint_iter)); ao2_cleanup(hint)) {
  7275. presence_state_notify_callbacks(msg, hint, &hint_app, stasis_message_data(msg));
  7276. }
  7277. ao2_iterator_destroy(&hint_iter);
  7278. ast_mutex_unlock(&context_merge_lock);
  7279. ast_free(hint_app);
  7280. }
  7281. /*!
  7282. * \internal
  7283. * \brief Implements the hints data provider.
  7284. */
  7285. static int hints_data_provider_get(const struct ast_data_search *search,
  7286. struct ast_data *data_root)
  7287. {
  7288. struct ast_data *data_hint;
  7289. struct ast_hint *hint;
  7290. int watchers;
  7291. struct ao2_iterator i;
  7292. if (ao2_container_count(hints) == 0) {
  7293. return 0;
  7294. }
  7295. i = ao2_iterator_init(hints, 0);
  7296. for (; (hint = ao2_iterator_next(&i)); ao2_ref(hint, -1)) {
  7297. watchers = ao2_container_count(hint->callbacks);
  7298. data_hint = ast_data_add_node(data_root, "hint");
  7299. if (!data_hint) {
  7300. continue;
  7301. }
  7302. ast_data_add_str(data_hint, "extension", ast_get_extension_name(hint->exten));
  7303. ast_data_add_str(data_hint, "context", ast_get_context_name(ast_get_extension_context(hint->exten)));
  7304. ast_data_add_str(data_hint, "application", ast_get_extension_app(hint->exten));
  7305. ast_data_add_str(data_hint, "state", ast_extension_state2str(hint->laststate));
  7306. ast_data_add_str(data_hint, "presence_state", ast_presence_state2str(hint->last_presence_state));
  7307. ast_data_add_str(data_hint, "presence_subtype", S_OR(hint->last_presence_subtype, ""));
  7308. ast_data_add_str(data_hint, "presence_subtype", S_OR(hint->last_presence_message, ""));
  7309. ast_data_add_int(data_hint, "watchers", watchers);
  7310. if (!ast_data_search_match(search, data_hint)) {
  7311. ast_data_remove_node(data_root, data_hint);
  7312. }
  7313. }
  7314. ao2_iterator_destroy(&i);
  7315. return 0;
  7316. }
  7317. static const struct ast_data_handler hints_data_provider = {
  7318. .version = AST_DATA_HANDLER_VERSION,
  7319. .get = hints_data_provider_get
  7320. };
  7321. static const struct ast_data_entry pbx_data_providers[] = {
  7322. AST_DATA_ENTRY("asterisk/core/hints", &hints_data_provider),
  7323. };
  7324. static int action_extensionstatelist(struct mansession *s, const struct message *m)
  7325. {
  7326. const char *action_id = astman_get_header(m, "ActionID");
  7327. struct ast_hint *hint;
  7328. struct ao2_iterator it_hints;
  7329. int hint_count = 0;
  7330. if (!hints) {
  7331. astman_send_error(s, m, "No dialplan hints are available");
  7332. return 0;
  7333. }
  7334. astman_send_listack(s, m, "Extension Statuses will follow", "start");
  7335. ao2_lock(hints);
  7336. it_hints = ao2_iterator_init(hints, 0);
  7337. for (; (hint = ao2_iterator_next(&it_hints)); ao2_ref(hint, -1)) {
  7338. ao2_lock(hint);
  7339. /* Ignore pattern matching hints; they are stored in the
  7340. * hints container but aren't real from the perspective of
  7341. * an AMI user
  7342. */
  7343. if (hint->exten->exten[0] == '_') {
  7344. ao2_unlock(hint);
  7345. continue;
  7346. }
  7347. ++hint_count;
  7348. astman_append(s, "Event: ExtensionStatus\r\n");
  7349. if (!ast_strlen_zero(action_id)) {
  7350. astman_append(s, "ActionID: %s\r\n", action_id);
  7351. }
  7352. astman_append(s,
  7353. "Exten: %s\r\n"
  7354. "Context: %s\r\n"
  7355. "Hint: %s\r\n"
  7356. "Status: %d\r\n"
  7357. "StatusText: %s\r\n\r\n",
  7358. hint->exten->exten,
  7359. hint->exten->parent->name,
  7360. hint->exten->app,
  7361. hint->laststate,
  7362. ast_extension_state2str(hint->laststate));
  7363. ao2_unlock(hint);
  7364. }
  7365. ao2_iterator_destroy(&it_hints);
  7366. ao2_unlock(hints);
  7367. astman_send_list_complete_start(s, m, "ExtensionStateListComplete", hint_count);
  7368. astman_send_list_complete_end(s);
  7369. return 0;
  7370. }
  7371. /*!
  7372. * \internal
  7373. * \brief Clean up resources on Asterisk shutdown.
  7374. *
  7375. * \note Cleans up resources allocated in load_pbx
  7376. */
  7377. static void unload_pbx(void)
  7378. {
  7379. presence_state_sub = stasis_unsubscribe_and_join(presence_state_sub);
  7380. device_state_sub = stasis_unsubscribe_and_join(device_state_sub);
  7381. ast_manager_unregister("ShowDialPlan");
  7382. ast_manager_unregister("ExtensionStateList");
  7383. ast_cli_unregister_multiple(pbx_cli, ARRAY_LEN(pbx_cli));
  7384. ast_custom_function_unregister(&exception_function);
  7385. ast_custom_function_unregister(&testtime_function);
  7386. ast_data_unregister(NULL);
  7387. }
  7388. int load_pbx(void)
  7389. {
  7390. int res = 0;
  7391. ast_register_cleanup(unload_pbx);
  7392. /* Initialize the PBX */
  7393. ast_verb(1, "Asterisk PBX Core Initializing\n");
  7394. ast_verb(2, "Registering builtin functions:\n");
  7395. ast_cli_register_multiple(pbx_cli, ARRAY_LEN(pbx_cli));
  7396. ast_data_register_multiple_core(pbx_data_providers, ARRAY_LEN(pbx_data_providers));
  7397. __ast_custom_function_register(&exception_function, NULL);
  7398. __ast_custom_function_register(&testtime_function, NULL);
  7399. /* Register manager application */
  7400. res |= ast_manager_register_xml_core("ShowDialPlan", EVENT_FLAG_CONFIG | EVENT_FLAG_REPORTING, manager_show_dialplan);
  7401. res |= ast_manager_register_xml_core("ExtensionStateList", EVENT_FLAG_CALL | EVENT_FLAG_REPORTING, action_extensionstatelist);
  7402. if (res) {
  7403. return -1;
  7404. }
  7405. if (!(device_state_sub = stasis_subscribe(ast_device_state_topic_all(), device_state_cb, NULL))) {
  7406. return -1;
  7407. }
  7408. stasis_subscription_accept_message_type(device_state_sub, ast_device_state_message_type());
  7409. stasis_subscription_accept_message_type(device_state_sub, hint_change_message_type());
  7410. stasis_subscription_accept_message_type(device_state_sub, hint_remove_message_type());
  7411. stasis_subscription_set_filter(device_state_sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
  7412. if (!(presence_state_sub = stasis_subscribe(ast_presence_state_topic_all(), presence_state_cb, NULL))) {
  7413. return -1;
  7414. }
  7415. stasis_subscription_accept_message_type(presence_state_sub, ast_presence_state_message_type());
  7416. stasis_subscription_set_filter(presence_state_sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
  7417. return 0;
  7418. }
  7419. /*
  7420. * Lock context list functions ...
  7421. */
  7422. int ast_wrlock_contexts(void)
  7423. {
  7424. return ast_mutex_lock(&conlock);
  7425. }
  7426. int ast_rdlock_contexts(void)
  7427. {
  7428. return ast_mutex_lock(&conlock);
  7429. }
  7430. int ast_unlock_contexts(void)
  7431. {
  7432. return ast_mutex_unlock(&conlock);
  7433. }
  7434. /*
  7435. * Lock context ...
  7436. */
  7437. int ast_wrlock_context(struct ast_context *con)
  7438. {
  7439. return ast_rwlock_wrlock(&con->lock);
  7440. }
  7441. int ast_rdlock_context(struct ast_context *con)
  7442. {
  7443. return ast_rwlock_rdlock(&con->lock);
  7444. }
  7445. int ast_unlock_context(struct ast_context *con)
  7446. {
  7447. return ast_rwlock_unlock(&con->lock);
  7448. }
  7449. /*
  7450. * Name functions ...
  7451. */
  7452. const char *ast_get_context_name(struct ast_context *con)
  7453. {
  7454. return con ? con->name : NULL;
  7455. }
  7456. struct ast_context *ast_get_extension_context(struct ast_exten *exten)
  7457. {
  7458. return exten ? exten->parent : NULL;
  7459. }
  7460. const char *ast_get_extension_name(struct ast_exten *exten)
  7461. {
  7462. return exten ? exten->name : NULL;
  7463. }
  7464. const char *ast_get_extension_label(struct ast_exten *exten)
  7465. {
  7466. return exten ? exten->label : NULL;
  7467. }
  7468. const char *ast_get_include_name(struct ast_include *inc)
  7469. {
  7470. return inc ? inc->name : NULL;
  7471. }
  7472. const char *ast_get_ignorepat_name(struct ast_ignorepat *ip)
  7473. {
  7474. return ip ? ip->pattern : NULL;
  7475. }
  7476. int ast_get_extension_priority(struct ast_exten *exten)
  7477. {
  7478. return exten ? exten->priority : -1;
  7479. }
  7480. /*
  7481. * Registrar info functions ...
  7482. */
  7483. const char *ast_get_context_registrar(struct ast_context *c)
  7484. {
  7485. return c ? c->registrar : NULL;
  7486. }
  7487. const char *ast_get_extension_registrar(struct ast_exten *e)
  7488. {
  7489. return e ? e->registrar : NULL;
  7490. }
  7491. const char *ast_get_include_registrar(struct ast_include *i)
  7492. {
  7493. return i ? i->registrar : NULL;
  7494. }
  7495. const char *ast_get_ignorepat_registrar(struct ast_ignorepat *ip)
  7496. {
  7497. return ip ? ip->registrar : NULL;
  7498. }
  7499. int ast_get_extension_matchcid(struct ast_exten *e)
  7500. {
  7501. return e ? e->matchcid : 0;
  7502. }
  7503. const char *ast_get_extension_cidmatch(struct ast_exten *e)
  7504. {
  7505. return e ? e->cidmatch_display : NULL;
  7506. }
  7507. const char *ast_get_extension_app(struct ast_exten *e)
  7508. {
  7509. return e ? e->app : NULL;
  7510. }
  7511. void *ast_get_extension_app_data(struct ast_exten *e)
  7512. {
  7513. return e ? e->data : NULL;
  7514. }
  7515. const char *ast_get_switch_name(struct ast_sw *sw)
  7516. {
  7517. return sw ? sw->name : NULL;
  7518. }
  7519. const char *ast_get_switch_data(struct ast_sw *sw)
  7520. {
  7521. return sw ? sw->data : NULL;
  7522. }
  7523. int ast_get_switch_eval(struct ast_sw *sw)
  7524. {
  7525. return sw->eval;
  7526. }
  7527. const char *ast_get_switch_registrar(struct ast_sw *sw)
  7528. {
  7529. return sw ? sw->registrar : NULL;
  7530. }
  7531. /*
  7532. * Walking functions ...
  7533. */
  7534. struct ast_context *ast_walk_contexts(struct ast_context *con)
  7535. {
  7536. return con ? con->next : contexts;
  7537. }
  7538. struct ast_exten *ast_walk_context_extensions(struct ast_context *con,
  7539. struct ast_exten *exten)
  7540. {
  7541. if (!exten)
  7542. return con ? con->root : NULL;
  7543. else
  7544. return exten->next;
  7545. }
  7546. struct ast_sw *ast_walk_context_switches(struct ast_context *con,
  7547. struct ast_sw *sw)
  7548. {
  7549. if (!sw)
  7550. return con ? AST_LIST_FIRST(&con->alts) : NULL;
  7551. else
  7552. return AST_LIST_NEXT(sw, list);
  7553. }
  7554. struct ast_exten *ast_walk_extension_priorities(struct ast_exten *exten,
  7555. struct ast_exten *priority)
  7556. {
  7557. return priority ? priority->peer : exten;
  7558. }
  7559. struct ast_include *ast_walk_context_includes(struct ast_context *con,
  7560. struct ast_include *inc)
  7561. {
  7562. if (!inc)
  7563. return con ? con->includes : NULL;
  7564. else
  7565. return inc->next;
  7566. }
  7567. struct ast_ignorepat *ast_walk_context_ignorepats(struct ast_context *con,
  7568. struct ast_ignorepat *ip)
  7569. {
  7570. if (!ip)
  7571. return con ? con->ignorepats : NULL;
  7572. else
  7573. return ip->next;
  7574. }
  7575. int ast_context_verify_includes(struct ast_context *con)
  7576. {
  7577. struct ast_include *inc = NULL;
  7578. int res = 0;
  7579. while ( (inc = ast_walk_context_includes(con, inc)) ) {
  7580. if (ast_context_find(inc->rname))
  7581. continue;
  7582. res = -1;
  7583. ast_log(LOG_WARNING, "Context '%s' tries to include nonexistent context '%s'\n",
  7584. ast_get_context_name(con), inc->rname);
  7585. break;
  7586. }
  7587. return res;
  7588. }
  7589. static int __ast_goto_if_exists(struct ast_channel *chan, const char *context, const char *exten, int priority, int async)
  7590. {
  7591. int (*goto_func)(struct ast_channel *chan, const char *context, const char *exten, int priority);
  7592. if (!chan)
  7593. return -2;
  7594. if (context == NULL)
  7595. context = ast_channel_context(chan);
  7596. if (exten == NULL)
  7597. exten = ast_channel_exten(chan);
  7598. goto_func = (async) ? ast_async_goto : ast_explicit_goto;
  7599. if (ast_exists_extension(chan, context, exten, priority,
  7600. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL)))
  7601. return goto_func(chan, context, exten, priority);
  7602. else {
  7603. return AST_PBX_GOTO_FAILED;
  7604. }
  7605. }
  7606. int ast_goto_if_exists(struct ast_channel *chan, const char* context, const char *exten, int priority)
  7607. {
  7608. return __ast_goto_if_exists(chan, context, exten, priority, 0);
  7609. }
  7610. int ast_async_goto_if_exists(struct ast_channel *chan, const char * context, const char *exten, int priority)
  7611. {
  7612. return __ast_goto_if_exists(chan, context, exten, priority, 1);
  7613. }
  7614. static int pbx_parseable_goto(struct ast_channel *chan, const char *goto_string, int async)
  7615. {
  7616. char *exten, *pri, *context;
  7617. char *stringp;
  7618. int ipri;
  7619. int mode = 0;
  7620. if (ast_strlen_zero(goto_string)) {
  7621. ast_log(LOG_WARNING, "Goto requires an argument ([[context,]extension,]priority)\n");
  7622. return -1;
  7623. }
  7624. stringp = ast_strdupa(goto_string);
  7625. context = strsep(&stringp, ","); /* guaranteed non-null */
  7626. exten = strsep(&stringp, ",");
  7627. pri = strsep(&stringp, ",");
  7628. if (!exten) { /* Only a priority in this one */
  7629. pri = context;
  7630. exten = NULL;
  7631. context = NULL;
  7632. } else if (!pri) { /* Only an extension and priority in this one */
  7633. pri = exten;
  7634. exten = context;
  7635. context = NULL;
  7636. }
  7637. if (*pri == '+') {
  7638. mode = 1;
  7639. pri++;
  7640. } else if (*pri == '-') {
  7641. mode = -1;
  7642. pri++;
  7643. }
  7644. if (sscanf(pri, "%30d", &ipri) != 1) {
  7645. ipri = ast_findlabel_extension(chan, context ? context : ast_channel_context(chan),
  7646. exten ? exten : ast_channel_exten(chan), pri,
  7647. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
  7648. if (ipri < 1) {
  7649. ast_log(LOG_WARNING, "Priority '%s' must be a number > 0, or valid label\n", pri);
  7650. return -1;
  7651. } else
  7652. mode = 0;
  7653. }
  7654. /* At this point we have a priority and maybe an extension and a context */
  7655. if (mode)
  7656. ipri = ast_channel_priority(chan) + (ipri * mode);
  7657. if (async)
  7658. ast_async_goto(chan, context, exten, ipri);
  7659. else
  7660. ast_explicit_goto(chan, context, exten, ipri);
  7661. return 0;
  7662. }
  7663. int ast_parseable_goto(struct ast_channel *chan, const char *goto_string)
  7664. {
  7665. return pbx_parseable_goto(chan, goto_string, 0);
  7666. }
  7667. int ast_async_parseable_goto(struct ast_channel *chan, const char *goto_string)
  7668. {
  7669. return pbx_parseable_goto(chan, goto_string, 1);
  7670. }
  7671. static int hint_hash(const void *obj, const int flags)
  7672. {
  7673. const struct ast_hint *hint = obj;
  7674. const char *exten_name;
  7675. int res;
  7676. exten_name = ast_get_extension_name(hint->exten);
  7677. if (ast_strlen_zero(exten_name)) {
  7678. /*
  7679. * If the exten or extension name isn't set, return 0 so that
  7680. * the ao2_find() search will start in the first bucket.
  7681. */
  7682. res = 0;
  7683. } else {
  7684. res = ast_str_case_hash(exten_name);
  7685. }
  7686. return res;
  7687. }
  7688. static int hint_cmp(void *obj, void *arg, int flags)
  7689. {
  7690. const struct ast_hint *hint = obj;
  7691. const struct ast_exten *exten = arg;
  7692. return (hint->exten == exten) ? CMP_MATCH | CMP_STOP : 0;
  7693. }
  7694. static int statecbs_cmp(void *obj, void *arg, int flags)
  7695. {
  7696. const struct ast_state_cb *state_cb = obj;
  7697. ast_state_cb_type change_cb = arg;
  7698. return (state_cb->change_cb == change_cb) ? CMP_MATCH | CMP_STOP : 0;
  7699. }
  7700. /*!
  7701. * \internal
  7702. * \brief Clean up resources on Asterisk shutdown
  7703. */
  7704. static void pbx_shutdown(void)
  7705. {
  7706. STASIS_MESSAGE_TYPE_CLEANUP(hint_change_message_type);
  7707. STASIS_MESSAGE_TYPE_CLEANUP(hint_remove_message_type);
  7708. if (hints) {
  7709. ao2_container_unregister("hints");
  7710. ao2_ref(hints, -1);
  7711. hints = NULL;
  7712. }
  7713. if (hintdevices) {
  7714. ao2_container_unregister("hintdevices");
  7715. ao2_ref(hintdevices, -1);
  7716. hintdevices = NULL;
  7717. }
  7718. if (statecbs) {
  7719. ao2_container_unregister("statecbs");
  7720. ao2_ref(statecbs, -1);
  7721. statecbs = NULL;
  7722. }
  7723. if (contexts_table) {
  7724. ast_hashtab_destroy(contexts_table, NULL);
  7725. }
  7726. }
  7727. static void print_hints_key(void *v_obj, void *where, ao2_prnt_fn *prnt)
  7728. {
  7729. struct ast_hint *hint = v_obj;
  7730. if (!hint) {
  7731. return;
  7732. }
  7733. prnt(where, "%s@%s", ast_get_extension_name(hint->exten),
  7734. ast_get_context_name(ast_get_extension_context(hint->exten)));
  7735. }
  7736. static void print_hintdevices_key(void *v_obj, void *where, ao2_prnt_fn *prnt)
  7737. {
  7738. struct ast_hintdevice *hintdevice = v_obj;
  7739. if (!hintdevice) {
  7740. return;
  7741. }
  7742. prnt(where, "%s => %s@%s", hintdevice->hintdevice,
  7743. ast_get_extension_name(hintdevice->hint->exten),
  7744. ast_get_context_name(ast_get_extension_context(hintdevice->hint->exten)));
  7745. }
  7746. static void print_statecbs_key(void *v_obj, void *where, ao2_prnt_fn *prnt)
  7747. {
  7748. struct ast_state_cb *state_cb = v_obj;
  7749. if (!state_cb) {
  7750. return;
  7751. }
  7752. prnt(where, "%d", state_cb->id);
  7753. }
  7754. int ast_pbx_init(void)
  7755. {
  7756. hints = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  7757. HASH_EXTENHINT_SIZE, hint_hash, NULL, hint_cmp);
  7758. if (hints) {
  7759. ao2_container_register("hints", hints, print_hints_key);
  7760. }
  7761. hintdevices = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  7762. HASH_EXTENHINT_SIZE, hintdevice_hash_cb, NULL, hintdevice_cmp_multiple);
  7763. if (hintdevices) {
  7764. ao2_container_register("hintdevices", hintdevices, print_hintdevices_key);
  7765. }
  7766. statecbs = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, statecbs_cmp);
  7767. if (statecbs) {
  7768. ao2_container_register("statecbs", statecbs, print_statecbs_key);
  7769. }
  7770. ast_register_cleanup(pbx_shutdown);
  7771. if (STASIS_MESSAGE_TYPE_INIT(hint_change_message_type) != 0) {
  7772. return -1;
  7773. }
  7774. if (STASIS_MESSAGE_TYPE_INIT(hint_remove_message_type) != 0) {
  7775. return -1;
  7776. }
  7777. return (hints && hintdevices && statecbs) ? 0 : -1;
  7778. }