channel.c 332 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2006, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Channel Management
  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 <sys/time.h>
  31. #include <signal.h>
  32. #include <math.h>
  33. #include "asterisk/paths.h" /* use ast_config_AST_SYSTEM_NAME */
  34. #include "asterisk/pbx.h"
  35. #include "asterisk/frame.h"
  36. #include "asterisk/mod_format.h"
  37. #include "asterisk/sched.h"
  38. #include "asterisk/channel.h"
  39. #include "asterisk/musiconhold.h"
  40. #include "asterisk/say.h"
  41. #include "asterisk/file.h"
  42. #include "asterisk/cli.h"
  43. #include "asterisk/translate.h"
  44. #include "asterisk/manager.h"
  45. #include "asterisk/chanvars.h"
  46. #include "asterisk/linkedlists.h"
  47. #include "asterisk/indications.h"
  48. #include "asterisk/causes.h"
  49. #include "asterisk/callerid.h"
  50. #include "asterisk/utils.h"
  51. #include "asterisk/lock.h"
  52. #include "asterisk/app.h"
  53. #include "asterisk/transcap.h"
  54. #include "asterisk/devicestate.h"
  55. #include "asterisk/threadstorage.h"
  56. #include "asterisk/slinfactory.h"
  57. #include "asterisk/audiohook.h"
  58. #include "asterisk/framehook.h"
  59. #include "asterisk/timing.h"
  60. #include "asterisk/autochan.h"
  61. #include "asterisk/stringfields.h"
  62. #include "asterisk/global_datastores.h"
  63. #include "asterisk/data.h"
  64. #include "asterisk/channel_internal.h"
  65. #include "asterisk/features.h"
  66. #include "asterisk/bridge.h"
  67. #include "asterisk/test.h"
  68. #include "asterisk/stasis_channels.h"
  69. #include "asterisk/max_forwards.h"
  70. #include "asterisk/message.h"
  71. /*** DOCUMENTATION
  72. ***/
  73. #ifdef HAVE_EPOLL
  74. #include <sys/epoll.h>
  75. #endif
  76. #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
  77. #if defined(HAVE_PRI)
  78. #include "libpri.h"
  79. #endif /* defined(HAVE_PRI) */
  80. #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
  81. struct ast_epoll_data {
  82. struct ast_channel *chan;
  83. int which;
  84. };
  85. /* uncomment if you have problems with 'monitoring' synchronized files */
  86. #if 0
  87. #define MONITOR_CONSTANT_DELAY
  88. #define MONITOR_DELAY 150 * 8 /*!< 150 ms of MONITORING DELAY */
  89. #endif
  90. static int chancount;
  91. unsigned long global_fin, global_fout;
  92. AST_THREADSTORAGE(state2str_threadbuf);
  93. #define STATE2STR_BUFSIZE 32
  94. /*! Default amount of time to use when emulating a digit as a begin and end
  95. * 100ms */
  96. #define AST_DEFAULT_EMULATE_DTMF_DURATION 100
  97. #define DEFAULT_AMA_FLAGS AST_AMA_DOCUMENTATION
  98. /*! Minimum amount of time between the end of the last digit and the beginning
  99. * of a new one - 45ms */
  100. #define AST_MIN_DTMF_GAP 45
  101. /*! \brief List of channel drivers */
  102. struct chanlist {
  103. const struct ast_channel_tech *tech;
  104. AST_LIST_ENTRY(chanlist) list;
  105. };
  106. /*! \brief the list of registered channel types */
  107. static AST_RWLIST_HEAD_STATIC(backends, chanlist);
  108. #ifdef LOW_MEMORY
  109. #define NUM_CHANNEL_BUCKETS 61
  110. #else
  111. #define NUM_CHANNEL_BUCKETS 1567
  112. #endif
  113. /*! \brief All active channels on the system */
  114. static struct ao2_container *channels;
  115. /*! \brief map AST_CAUSE's to readable string representations
  116. *
  117. * \ref causes.h
  118. */
  119. struct causes_map {
  120. int cause;
  121. const char *name;
  122. const char *desc;
  123. };
  124. static const struct causes_map causes[] = {
  125. { AST_CAUSE_UNALLOCATED, "UNALLOCATED", "Unallocated (unassigned) number" },
  126. { AST_CAUSE_NO_ROUTE_TRANSIT_NET, "NO_ROUTE_TRANSIT_NET", "No route to specified transmit network" },
  127. { AST_CAUSE_NO_ROUTE_DESTINATION, "NO_ROUTE_DESTINATION", "No route to destination" },
  128. { AST_CAUSE_MISDIALLED_TRUNK_PREFIX, "MISDIALLED_TRUNK_PREFIX", "Misdialed trunk prefix" },
  129. { AST_CAUSE_CHANNEL_UNACCEPTABLE, "CHANNEL_UNACCEPTABLE", "Channel unacceptable" },
  130. { AST_CAUSE_CALL_AWARDED_DELIVERED, "CALL_AWARDED_DELIVERED", "Call awarded and being delivered in an established channel" },
  131. { AST_CAUSE_PRE_EMPTED, "PRE_EMPTED", "Pre-empted" },
  132. { AST_CAUSE_NUMBER_PORTED_NOT_HERE, "NUMBER_PORTED_NOT_HERE", "Number ported elsewhere" },
  133. { AST_CAUSE_NORMAL_CLEARING, "NORMAL_CLEARING", "Normal Clearing" },
  134. { AST_CAUSE_USER_BUSY, "USER_BUSY", "User busy" },
  135. { AST_CAUSE_NO_USER_RESPONSE, "NO_USER_RESPONSE", "No user responding" },
  136. { AST_CAUSE_NO_ANSWER, "NO_ANSWER", "User alerting, no answer" },
  137. { AST_CAUSE_SUBSCRIBER_ABSENT, "SUBSCRIBER_ABSENT", "Subscriber absent" },
  138. { AST_CAUSE_CALL_REJECTED, "CALL_REJECTED", "Call Rejected" },
  139. { AST_CAUSE_NUMBER_CHANGED, "NUMBER_CHANGED", "Number changed" },
  140. { AST_CAUSE_REDIRECTED_TO_NEW_DESTINATION, "REDIRECTED_TO_NEW_DESTINATION", "Redirected to new destination" },
  141. { AST_CAUSE_ANSWERED_ELSEWHERE, "ANSWERED_ELSEWHERE", "Answered elsewhere" },
  142. { AST_CAUSE_DESTINATION_OUT_OF_ORDER, "DESTINATION_OUT_OF_ORDER", "Destination out of order" },
  143. { AST_CAUSE_INVALID_NUMBER_FORMAT, "INVALID_NUMBER_FORMAT", "Invalid number format" },
  144. { AST_CAUSE_FACILITY_REJECTED, "FACILITY_REJECTED", "Facility rejected" },
  145. { AST_CAUSE_RESPONSE_TO_STATUS_ENQUIRY, "RESPONSE_TO_STATUS_ENQUIRY", "Response to STATus ENQuiry" },
  146. { AST_CAUSE_NORMAL_UNSPECIFIED, "NORMAL_UNSPECIFIED", "Normal, unspecified" },
  147. { AST_CAUSE_NORMAL_CIRCUIT_CONGESTION, "NORMAL_CIRCUIT_CONGESTION", "Circuit/channel congestion" },
  148. { AST_CAUSE_NETWORK_OUT_OF_ORDER, "NETWORK_OUT_OF_ORDER", "Network out of order" },
  149. { AST_CAUSE_NORMAL_TEMPORARY_FAILURE, "NORMAL_TEMPORARY_FAILURE", "Temporary failure" },
  150. { AST_CAUSE_SWITCH_CONGESTION, "SWITCH_CONGESTION", "Switching equipment congestion" },
  151. { AST_CAUSE_ACCESS_INFO_DISCARDED, "ACCESS_INFO_DISCARDED", "Access information discarded" },
  152. { AST_CAUSE_REQUESTED_CHAN_UNAVAIL, "REQUESTED_CHAN_UNAVAIL", "Requested channel not available" },
  153. { AST_CAUSE_FACILITY_NOT_SUBSCRIBED, "FACILITY_NOT_SUBSCRIBED", "Facility not subscribed" },
  154. { AST_CAUSE_OUTGOING_CALL_BARRED, "OUTGOING_CALL_BARRED", "Outgoing call barred" },
  155. { AST_CAUSE_INCOMING_CALL_BARRED, "INCOMING_CALL_BARRED", "Incoming call barred" },
  156. { AST_CAUSE_BEARERCAPABILITY_NOTAUTH, "BEARERCAPABILITY_NOTAUTH", "Bearer capability not authorized" },
  157. { AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, "BEARERCAPABILITY_NOTAVAIL", "Bearer capability not available" },
  158. { AST_CAUSE_BEARERCAPABILITY_NOTIMPL, "BEARERCAPABILITY_NOTIMPL", "Bearer capability not implemented" },
  159. { AST_CAUSE_CHAN_NOT_IMPLEMENTED, "CHAN_NOT_IMPLEMENTED", "Channel not implemented" },
  160. { AST_CAUSE_FACILITY_NOT_IMPLEMENTED, "FACILITY_NOT_IMPLEMENTED", "Facility not implemented" },
  161. { AST_CAUSE_INVALID_CALL_REFERENCE, "INVALID_CALL_REFERENCE", "Invalid call reference value" },
  162. { AST_CAUSE_INCOMPATIBLE_DESTINATION, "INCOMPATIBLE_DESTINATION", "Incompatible destination" },
  163. { AST_CAUSE_INVALID_MSG_UNSPECIFIED, "INVALID_MSG_UNSPECIFIED", "Invalid message unspecified" },
  164. { AST_CAUSE_MANDATORY_IE_MISSING, "MANDATORY_IE_MISSING", "Mandatory information element is missing" },
  165. { AST_CAUSE_MESSAGE_TYPE_NONEXIST, "MESSAGE_TYPE_NONEXIST", "Message type nonexist." },
  166. { AST_CAUSE_WRONG_MESSAGE, "WRONG_MESSAGE", "Wrong message" },
  167. { AST_CAUSE_IE_NONEXIST, "IE_NONEXIST", "Info. element nonexist or not implemented" },
  168. { AST_CAUSE_INVALID_IE_CONTENTS, "INVALID_IE_CONTENTS", "Invalid information element contents" },
  169. { AST_CAUSE_WRONG_CALL_STATE, "WRONG_CALL_STATE", "Message not compatible with call state" },
  170. { AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE, "RECOVERY_ON_TIMER_EXPIRE", "Recover on timer expiry" },
  171. { AST_CAUSE_MANDATORY_IE_LENGTH_ERROR, "MANDATORY_IE_LENGTH_ERROR", "Mandatory IE length error" },
  172. { AST_CAUSE_PROTOCOL_ERROR, "PROTOCOL_ERROR", "Protocol error, unspecified" },
  173. { AST_CAUSE_INTERWORKING, "INTERWORKING", "Interworking, unspecified" },
  174. };
  175. struct ast_variable *ast_channeltype_list(void)
  176. {
  177. struct chanlist *cl;
  178. struct ast_variable *var = NULL, *prev = NULL;
  179. AST_RWLIST_RDLOCK(&backends);
  180. AST_RWLIST_TRAVERSE(&backends, cl, list) {
  181. if (prev) {
  182. if ((prev->next = ast_variable_new(cl->tech->type, cl->tech->description, "")))
  183. prev = prev->next;
  184. } else {
  185. var = ast_variable_new(cl->tech->type, cl->tech->description, "");
  186. prev = var;
  187. }
  188. }
  189. AST_RWLIST_UNLOCK(&backends);
  190. return var;
  191. }
  192. #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
  193. static const char *party_number_ton2str(int ton)
  194. {
  195. #if defined(HAVE_PRI)
  196. switch ((ton >> 4) & 0x07) {
  197. case PRI_TON_INTERNATIONAL:
  198. return "International";
  199. case PRI_TON_NATIONAL:
  200. return "National";
  201. case PRI_TON_NET_SPECIFIC:
  202. return "Network Specific";
  203. case PRI_TON_SUBSCRIBER:
  204. return "Subscriber";
  205. case PRI_TON_ABBREVIATED:
  206. return "Abbreviated";
  207. case PRI_TON_RESERVED:
  208. return "Reserved";
  209. case PRI_TON_UNKNOWN:
  210. default:
  211. break;
  212. }
  213. #endif /* defined(HAVE_PRI) */
  214. return "Unknown";
  215. }
  216. #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
  217. #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
  218. static const char *party_number_plan2str(int plan)
  219. {
  220. #if defined(HAVE_PRI)
  221. switch (plan & 0x0F) {
  222. default:
  223. case PRI_NPI_UNKNOWN:
  224. break;
  225. case PRI_NPI_E163_E164:
  226. return "Public (E.163/E.164)";
  227. case PRI_NPI_X121:
  228. return "Data (X.121)";
  229. case PRI_NPI_F69:
  230. return "Telex (F.69)";
  231. case PRI_NPI_NATIONAL:
  232. return "National Standard";
  233. case PRI_NPI_PRIVATE:
  234. return "Private";
  235. case PRI_NPI_RESERVED:
  236. return "Reserved";
  237. }
  238. #endif /* defined(HAVE_PRI) */
  239. return "Unknown";
  240. }
  241. #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
  242. /*! \brief Show channel types - CLI command */
  243. static char *handle_cli_core_show_channeltypes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  244. {
  245. #define FORMAT "%-15.15s %-40.40s %-12.12s %-12.12s %-12.12s\n"
  246. struct chanlist *cl;
  247. int count_chan = 0;
  248. switch (cmd) {
  249. case CLI_INIT:
  250. e->command = "core show channeltypes";
  251. e->usage =
  252. "Usage: core show channeltypes\n"
  253. " Lists available channel types registered in your\n"
  254. " Asterisk server.\n";
  255. return NULL;
  256. case CLI_GENERATE:
  257. return NULL;
  258. }
  259. if (a->argc != 3)
  260. return CLI_SHOWUSAGE;
  261. ast_cli(a->fd, FORMAT, "Type", "Description", "Devicestate", "Indications", "Transfer");
  262. ast_cli(a->fd, FORMAT, "-----------", "-----------", "-----------", "-----------", "-----------");
  263. AST_RWLIST_RDLOCK(&backends);
  264. AST_RWLIST_TRAVERSE(&backends, cl, list) {
  265. ast_cli(a->fd, FORMAT, cl->tech->type, cl->tech->description,
  266. (cl->tech->devicestate) ? "yes" : "no",
  267. (cl->tech->indicate) ? "yes" : "no",
  268. (cl->tech->transfer) ? "yes" : "no");
  269. count_chan++;
  270. }
  271. AST_RWLIST_UNLOCK(&backends);
  272. ast_cli(a->fd, "----------\n%d channel drivers registered.\n", count_chan);
  273. return CLI_SUCCESS;
  274. #undef FORMAT
  275. }
  276. static char *complete_channeltypes(struct ast_cli_args *a)
  277. {
  278. struct chanlist *cl;
  279. int wordlen;
  280. if (a->pos != 3) {
  281. return NULL;
  282. }
  283. wordlen = strlen(a->word);
  284. AST_RWLIST_RDLOCK(&backends);
  285. AST_RWLIST_TRAVERSE(&backends, cl, list) {
  286. if (!strncasecmp(a->word, cl->tech->type, wordlen)) {
  287. ast_cli_completion_add(ast_strdup(cl->tech->type));
  288. }
  289. }
  290. AST_RWLIST_UNLOCK(&backends);
  291. return NULL;
  292. }
  293. /*! \brief Show details about a channel driver - CLI command */
  294. static char *handle_cli_core_show_channeltype(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  295. {
  296. struct chanlist *cl = NULL;
  297. struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  298. switch (cmd) {
  299. case CLI_INIT:
  300. e->command = "core show channeltype";
  301. e->usage =
  302. "Usage: core show channeltype <name>\n"
  303. " Show details about the specified channel type, <name>.\n";
  304. return NULL;
  305. case CLI_GENERATE:
  306. return complete_channeltypes(a);
  307. }
  308. if (a->argc != 4)
  309. return CLI_SHOWUSAGE;
  310. AST_RWLIST_RDLOCK(&backends);
  311. AST_RWLIST_TRAVERSE(&backends, cl, list) {
  312. if (!strncasecmp(cl->tech->type, a->argv[3], strlen(cl->tech->type)))
  313. break;
  314. }
  315. if (!cl) {
  316. ast_cli(a->fd, "\n%s is not a registered channel driver.\n", a->argv[3]);
  317. AST_RWLIST_UNLOCK(&backends);
  318. return CLI_FAILURE;
  319. }
  320. ast_cli(a->fd,
  321. "-- Info about channel driver: %s --\n"
  322. " Device State: %s\n"
  323. " Indication: %s\n"
  324. " Transfer : %s\n"
  325. " Capabilities: %s\n"
  326. " Digit Begin: %s\n"
  327. " Digit End: %s\n"
  328. " Send HTML : %s\n"
  329. " Image Support: %s\n"
  330. " Text Support: %s\n",
  331. cl->tech->type,
  332. (cl->tech->devicestate) ? "yes" : "no",
  333. (cl->tech->indicate) ? "yes" : "no",
  334. (cl->tech->transfer) ? "yes" : "no",
  335. ast_format_cap_get_names(cl->tech->capabilities, &codec_buf),
  336. (cl->tech->send_digit_begin) ? "yes" : "no",
  337. (cl->tech->send_digit_end) ? "yes" : "no",
  338. (cl->tech->send_html) ? "yes" : "no",
  339. (cl->tech->send_image) ? "yes" : "no",
  340. (cl->tech->send_text) ? "yes" : "no"
  341. );
  342. AST_RWLIST_UNLOCK(&backends);
  343. return CLI_SUCCESS;
  344. }
  345. static struct ast_cli_entry cli_channel[] = {
  346. AST_CLI_DEFINE(handle_cli_core_show_channeltypes, "List available channel types"),
  347. AST_CLI_DEFINE(handle_cli_core_show_channeltype, "Give more details on that channel type")
  348. };
  349. static struct ast_frame *kill_read(struct ast_channel *chan)
  350. {
  351. /* Hangup channel. */
  352. return NULL;
  353. }
  354. static struct ast_frame *kill_exception(struct ast_channel *chan)
  355. {
  356. /* Hangup channel. */
  357. return NULL;
  358. }
  359. static int kill_write(struct ast_channel *chan, struct ast_frame *frame)
  360. {
  361. /* Hangup channel. */
  362. return -1;
  363. }
  364. static int kill_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  365. {
  366. /* No problem fixing up the channel. */
  367. return 0;
  368. }
  369. static int kill_hangup(struct ast_channel *chan)
  370. {
  371. ast_channel_tech_pvt_set(chan, NULL);
  372. return 0;
  373. }
  374. /*!
  375. * \brief Kill the channel channel driver technology descriptor.
  376. *
  377. * \details
  378. * The purpose of this channel technology is to encourage the
  379. * channel to hangup as quickly as possible.
  380. *
  381. * \note Used by DTMF atxfer and zombie channels.
  382. */
  383. const struct ast_channel_tech ast_kill_tech = {
  384. .type = "Kill",
  385. .description = "Kill channel (should not see this)",
  386. .read = kill_read,
  387. .exception = kill_exception,
  388. .write = kill_write,
  389. .fixup = kill_fixup,
  390. .hangup = kill_hangup,
  391. };
  392. /*! \brief Checks to see if a channel is needing hang up */
  393. int ast_check_hangup(struct ast_channel *chan)
  394. {
  395. if (ast_channel_softhangup_internal_flag(chan)) /* yes if soft hangup flag set */
  396. return 1;
  397. if (ast_tvzero(*ast_channel_whentohangup(chan))) /* no if no hangup scheduled */
  398. return 0;
  399. if (ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()) > 0) /* no if hangup time has not come yet. */
  400. return 0;
  401. ast_debug(4, "Hangup time has come: %" PRIi64 "\n", ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()));
  402. ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(chan));
  403. ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT); /* record event */
  404. return 1;
  405. }
  406. int ast_check_hangup_locked(struct ast_channel *chan)
  407. {
  408. int res;
  409. ast_channel_lock(chan);
  410. res = ast_check_hangup(chan);
  411. ast_channel_unlock(chan);
  412. return res;
  413. }
  414. void ast_channel_softhangup_withcause_locked(struct ast_channel *chan, int causecode)
  415. {
  416. ast_channel_lock(chan);
  417. if (causecode > 0) {
  418. ast_debug(1, "Setting hangupcause of channel %s to %d (is %d now)\n",
  419. ast_channel_name(chan), causecode, ast_channel_hangupcause(chan));
  420. ast_channel_hangupcause_set(chan, causecode);
  421. }
  422. ast_softhangup_nolock(chan, AST_SOFTHANGUP_EXPLICIT);
  423. ast_channel_unlock(chan);
  424. }
  425. static int ast_channel_softhangup_cb(void *obj, void *arg, int flags)
  426. {
  427. struct ast_channel *chan = obj;
  428. ast_softhangup(chan, AST_SOFTHANGUP_SHUTDOWN);
  429. return 0;
  430. }
  431. void ast_softhangup_all(void)
  432. {
  433. ao2_callback(channels, OBJ_NODATA | OBJ_MULTIPLE, ast_channel_softhangup_cb, NULL);
  434. }
  435. /*! \brief returns number of active/allocated channels */
  436. int ast_active_channels(void)
  437. {
  438. return channels ? ao2_container_count(channels) : 0;
  439. }
  440. int ast_undestroyed_channels(void)
  441. {
  442. return ast_atomic_fetchadd_int(&chancount, 0);
  443. }
  444. /*! \brief Set when to hangup channel */
  445. void ast_channel_setwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
  446. {
  447. if (ast_tvzero(offset)) {
  448. ast_channel_whentohangup_set(chan, &offset);
  449. } else {
  450. struct timeval tv = ast_tvadd(offset, ast_tvnow());
  451. ast_channel_whentohangup_set(chan, &tv);
  452. }
  453. ast_queue_frame(chan, &ast_null_frame);
  454. return;
  455. }
  456. void ast_channel_setwhentohangup(struct ast_channel *chan, time_t offset)
  457. {
  458. struct timeval when = { offset, };
  459. ast_channel_setwhentohangup_tv(chan, when);
  460. }
  461. /*! \brief Compare a offset with when to hangup channel */
  462. int ast_channel_cmpwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
  463. {
  464. struct timeval whentohangup;
  465. if (ast_tvzero(*ast_channel_whentohangup(chan)))
  466. return ast_tvzero(offset) ? 0 : -1;
  467. if (ast_tvzero(offset))
  468. return 1;
  469. whentohangup = ast_tvadd(offset, ast_tvnow());
  470. return ast_tvdiff_ms(whentohangup, *ast_channel_whentohangup(chan));
  471. }
  472. int ast_channel_cmpwhentohangup(struct ast_channel *chan, time_t offset)
  473. {
  474. struct timeval when = { offset, };
  475. return ast_channel_cmpwhentohangup_tv(chan, when);
  476. }
  477. /*! \brief Register a new telephony channel in Asterisk */
  478. int ast_channel_register(const struct ast_channel_tech *tech)
  479. {
  480. struct chanlist *chan;
  481. AST_RWLIST_WRLOCK(&backends);
  482. AST_RWLIST_TRAVERSE(&backends, chan, list) {
  483. if (!strcasecmp(tech->type, chan->tech->type)) {
  484. ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", tech->type);
  485. AST_RWLIST_UNLOCK(&backends);
  486. return -1;
  487. }
  488. }
  489. if (!(chan = ast_calloc(1, sizeof(*chan)))) {
  490. AST_RWLIST_UNLOCK(&backends);
  491. return -1;
  492. }
  493. chan->tech = tech;
  494. AST_RWLIST_INSERT_HEAD(&backends, chan, list);
  495. ast_debug(1, "Registered handler for '%s' (%s)\n", chan->tech->type, chan->tech->description);
  496. ast_verb(2, "Registered channel type '%s' (%s)\n", chan->tech->type, chan->tech->description);
  497. AST_RWLIST_UNLOCK(&backends);
  498. return 0;
  499. }
  500. /*! \brief Unregister channel driver */
  501. void ast_channel_unregister(const struct ast_channel_tech *tech)
  502. {
  503. struct chanlist *chan;
  504. ast_debug(1, "Unregistering channel type '%s'\n", tech->type);
  505. AST_RWLIST_WRLOCK(&backends);
  506. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&backends, chan, list) {
  507. if (chan->tech == tech) {
  508. AST_LIST_REMOVE_CURRENT(list);
  509. ast_free(chan);
  510. ast_verb(2, "Unregistered channel type '%s'\n", tech->type);
  511. break;
  512. }
  513. }
  514. AST_LIST_TRAVERSE_SAFE_END;
  515. AST_RWLIST_UNLOCK(&backends);
  516. }
  517. /*! \brief Get handle to channel driver based on name */
  518. const struct ast_channel_tech *ast_get_channel_tech(const char *name)
  519. {
  520. struct chanlist *chanls;
  521. const struct ast_channel_tech *ret = NULL;
  522. AST_RWLIST_RDLOCK(&backends);
  523. AST_RWLIST_TRAVERSE(&backends, chanls, list) {
  524. if (!strcasecmp(name, chanls->tech->type)) {
  525. ret = chanls->tech;
  526. break;
  527. }
  528. }
  529. AST_RWLIST_UNLOCK(&backends);
  530. return ret;
  531. }
  532. /*! \brief Gives the string form of a given hangup cause */
  533. const char *ast_cause2str(int cause)
  534. {
  535. int x;
  536. for (x = 0; x < ARRAY_LEN(causes); x++) {
  537. if (causes[x].cause == cause)
  538. return causes[x].desc;
  539. }
  540. return "Unknown";
  541. }
  542. /*! \brief Convert a symbolic hangup cause to number */
  543. int ast_str2cause(const char *name)
  544. {
  545. int x;
  546. for (x = 0; x < ARRAY_LEN(causes); x++)
  547. if (!strncasecmp(causes[x].name, name, strlen(causes[x].name)))
  548. return causes[x].cause;
  549. return -1;
  550. }
  551. static struct stasis_message *create_channel_snapshot_message(struct ast_channel *channel)
  552. {
  553. RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
  554. if (!ast_channel_snapshot_type()) {
  555. return NULL;
  556. }
  557. ast_channel_lock(channel);
  558. snapshot = ast_channel_snapshot_create(channel);
  559. ast_channel_unlock(channel);
  560. if (!snapshot) {
  561. return NULL;
  562. }
  563. return stasis_message_create(ast_channel_snapshot_type(), snapshot);
  564. }
  565. static void publish_cache_clear(struct ast_channel *chan)
  566. {
  567. RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
  568. RAII_VAR(struct stasis_message *, clear_msg, NULL, ao2_cleanup);
  569. clear_msg = create_channel_snapshot_message(chan);
  570. if (!clear_msg) {
  571. return;
  572. }
  573. message = stasis_cache_clear_create(clear_msg);
  574. stasis_publish(ast_channel_topic(chan), message);
  575. }
  576. /*! \brief Gives the string form of a given channel state.
  577. *
  578. * \note This function is not reentrant.
  579. *
  580. * \param state
  581. */
  582. const char *ast_state2str(enum ast_channel_state state)
  583. {
  584. char *buf;
  585. switch (state) {
  586. case AST_STATE_DOWN:
  587. return "Down";
  588. case AST_STATE_RESERVED:
  589. return "Rsrvd";
  590. case AST_STATE_OFFHOOK:
  591. return "OffHook";
  592. case AST_STATE_DIALING:
  593. return "Dialing";
  594. case AST_STATE_RING:
  595. return "Ring";
  596. case AST_STATE_RINGING:
  597. return "Ringing";
  598. case AST_STATE_UP:
  599. return "Up";
  600. case AST_STATE_BUSY:
  601. return "Busy";
  602. case AST_STATE_DIALING_OFFHOOK:
  603. return "Dialing Offhook";
  604. case AST_STATE_PRERING:
  605. return "Pre-ring";
  606. case AST_STATE_MUTE:
  607. return "Mute";
  608. default:
  609. if (!(buf = ast_threadstorage_get(&state2str_threadbuf, STATE2STR_BUFSIZE)))
  610. return "Unknown";
  611. snprintf(buf, STATE2STR_BUFSIZE, "Unknown (%u)", state);
  612. return buf;
  613. }
  614. }
  615. /*! \brief Gives the string form of a given transfer capability */
  616. char *ast_transfercapability2str(int transfercapability)
  617. {
  618. switch (transfercapability) {
  619. case AST_TRANS_CAP_SPEECH:
  620. return "SPEECH";
  621. case AST_TRANS_CAP_DIGITAL:
  622. return "DIGITAL";
  623. case AST_TRANS_CAP_RESTRICTED_DIGITAL:
  624. return "RESTRICTED_DIGITAL";
  625. case AST_TRANS_CAP_3_1K_AUDIO:
  626. return "3K1AUDIO";
  627. case AST_TRANS_CAP_DIGITAL_W_TONES:
  628. return "DIGITAL_W_TONES";
  629. case AST_TRANS_CAP_VIDEO:
  630. return "VIDEO";
  631. default:
  632. return "UNKNOWN";
  633. }
  634. }
  635. /*! \brief Channel technology used to extract a channel from a running application. The
  636. * channel created with this technology will be immediately hung up - most external
  637. * applications won't ever want to see this.
  638. */
  639. static const struct ast_channel_tech surrogate_tech = {
  640. .type = "Surrogate",
  641. .description = "Surrogate channel used to pull channel from an application",
  642. .properties = AST_CHAN_TP_INTERNAL,
  643. };
  644. static const struct ast_channel_tech null_tech = {
  645. .type = "NULL",
  646. .description = "Null channel (should not see this)",
  647. };
  648. static void ast_channel_destructor(void *obj);
  649. static void ast_dummy_channel_destructor(void *obj);
  650. static int ast_channel_by_uniqueid_cb(void *obj, void *arg, void *data, int flags);
  651. static int does_id_conflict(const char *uniqueid)
  652. {
  653. struct ast_channel *conflict;
  654. size_t length = 0;
  655. if (ast_strlen_zero(uniqueid)) {
  656. return 0;
  657. }
  658. conflict = ast_channel_callback(ast_channel_by_uniqueid_cb, (char *) uniqueid, &length, OBJ_NOLOCK);
  659. if (conflict) {
  660. ast_log(LOG_ERROR, "Channel Unique ID '%s' already in use by channel %s(%p)\n",
  661. uniqueid, ast_channel_name(conflict), conflict);
  662. ast_channel_unref(conflict);
  663. return 1;
  664. }
  665. return 0;
  666. }
  667. /*! \brief Create a new channel structure */
  668. static struct ast_channel *__attribute__((format(printf, 15, 0)))
  669. __ast_channel_alloc_ap(int needqueue, int state, const char *cid_num, const char *cid_name,
  670. const char *acctcode, const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
  671. const struct ast_channel *requestor, enum ama_flags amaflag, struct ast_endpoint *endpoint,
  672. const char *file, int line,
  673. const char *function, const char *name_fmt, va_list ap)
  674. {
  675. struct ast_channel *tmp;
  676. struct varshead *headp;
  677. char *tech = "", *tech2 = NULL;
  678. struct ast_format_cap *nativeformats;
  679. struct ast_sched_context *schedctx;
  680. struct ast_timer *timer;
  681. struct timeval now;
  682. const struct ast_channel_tech *channel_tech;
  683. /* If shutting down, don't allocate any new channels */
  684. if (ast_shutting_down()) {
  685. ast_log(LOG_WARNING, "Channel allocation failed: Refusing due to active shutdown\n");
  686. return NULL;
  687. }
  688. if (!(tmp = ast_channel_internal_alloc(ast_channel_destructor, assignedids, requestor))) {
  689. /* Channel structure allocation failure. */
  690. return NULL;
  691. }
  692. ast_channel_stage_snapshot(tmp);
  693. /*
  694. * Init file descriptors to unopened state so
  695. * the destructor can know not to close them.
  696. */
  697. ast_channel_timingfd_set(tmp, -1);
  698. ast_channel_internal_alertpipe_clear(tmp);
  699. ast_channel_internal_fd_clear_all(tmp);
  700. #ifdef HAVE_EPOLL
  701. ast_channel_epfd_set(tmp, epoll_create(25));
  702. #endif
  703. nativeformats = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  704. if (!nativeformats) {
  705. /*
  706. * Aborting the channel creation. We do not need to complete staging
  707. * the channel snapshot because the channel has not been finalized or
  708. * linked into the channels container yet. Nobody else knows about
  709. * this channel nor will anybody ever know about it.
  710. */
  711. return ast_channel_unref(tmp);
  712. }
  713. ast_format_cap_append(nativeformats, ast_format_none, 0);
  714. ast_channel_nativeformats_set(tmp, nativeformats);
  715. ao2_ref(nativeformats, -1);
  716. ast_channel_set_rawwriteformat(tmp, ast_format_none);
  717. ast_channel_set_rawreadformat(tmp, ast_format_none);
  718. ast_channel_set_writeformat(tmp, ast_format_none);
  719. ast_channel_set_readformat(tmp, ast_format_none);
  720. if (!(schedctx = ast_sched_context_create())) {
  721. ast_log(LOG_WARNING, "Channel allocation failed: Unable to create schedule context\n");
  722. /* See earlier channel creation abort comment above. */
  723. return ast_channel_unref(tmp);
  724. }
  725. ast_channel_sched_set(tmp, schedctx);
  726. ast_party_dialed_init(ast_channel_dialed(tmp));
  727. ast_party_caller_init(ast_channel_caller(tmp));
  728. ast_party_connected_line_init(ast_channel_connected(tmp));
  729. ast_party_connected_line_init(ast_channel_connected_indicated(tmp));
  730. ast_party_redirecting_init(ast_channel_redirecting(tmp));
  731. if (cid_name) {
  732. ast_channel_caller(tmp)->id.name.valid = 1;
  733. ast_channel_caller(tmp)->id.name.str = ast_strdup(cid_name);
  734. if (!ast_channel_caller(tmp)->id.name.str) {
  735. /* See earlier channel creation abort comment above. */
  736. return ast_channel_unref(tmp);
  737. }
  738. }
  739. if (cid_num) {
  740. ast_channel_caller(tmp)->id.number.valid = 1;
  741. ast_channel_caller(tmp)->id.number.str = ast_strdup(cid_num);
  742. if (!ast_channel_caller(tmp)->id.number.str) {
  743. /* See earlier channel creation abort comment above. */
  744. return ast_channel_unref(tmp);
  745. }
  746. }
  747. if ((timer = ast_timer_open())) {
  748. ast_channel_timer_set(tmp, timer);
  749. if (strcmp(ast_timer_get_name(ast_channel_timer(tmp)), "timerfd")) {
  750. needqueue = 0;
  751. }
  752. ast_channel_timingfd_set(tmp, ast_timer_fd(ast_channel_timer(tmp)));
  753. }
  754. if (needqueue && ast_channel_internal_alertpipe_init(tmp)) {
  755. /* See earlier channel creation abort comment above. */
  756. return ast_channel_unref(tmp);
  757. }
  758. /* Always watch the alertpipe */
  759. ast_channel_set_fd(tmp, AST_ALERT_FD, ast_channel_internal_alert_readfd(tmp));
  760. /* And timing pipe */
  761. ast_channel_set_fd(tmp, AST_TIMING_FD, ast_channel_timingfd(tmp));
  762. /* Initial state */
  763. ast_channel_state_set(tmp, state);
  764. ast_channel_hold_state_set(tmp, AST_CONTROL_UNHOLD);
  765. ast_channel_streamid_set(tmp, -1);
  766. ast_channel_vstreamid_set(tmp, -1);
  767. ast_channel_fin_set(tmp, global_fin);
  768. ast_channel_fout_set(tmp, global_fout);
  769. now = ast_tvnow();
  770. ast_channel_creationtime_set(tmp, &now);
  771. ast_channel_internal_setup_topics(tmp);
  772. if (!ast_strlen_zero(name_fmt)) {
  773. char *slash, *slash2;
  774. /* Almost every channel is calling this function, and setting the name via the ast_string_field_build() call.
  775. * And they all use slightly different formats for their name string.
  776. * This means, to set the name here, we have to accept variable args, and call the string_field_build from here.
  777. * This means, that the stringfields must have a routine that takes the va_lists directly, and
  778. * uses them to build the string, instead of forming the va_lists internally from the vararg ... list.
  779. * This new function was written so this can be accomplished.
  780. */
  781. ast_channel_name_build_va(tmp, name_fmt, ap);
  782. tech = ast_strdupa(ast_channel_name(tmp));
  783. if ((slash = strchr(tech, '/'))) {
  784. if ((slash2 = strchr(slash + 1, '/'))) {
  785. tech2 = slash + 1;
  786. *slash2 = '\0';
  787. }
  788. *slash = '\0';
  789. }
  790. } else {
  791. /*
  792. * Start the string with '-' so it becomes an empty string
  793. * in the destructor.
  794. */
  795. ast_channel_name_set(tmp, "-**Unknown**");
  796. }
  797. if (amaflag != AST_AMA_NONE) {
  798. ast_channel_amaflags_set(tmp, amaflag);
  799. } else {
  800. ast_channel_amaflags_set(tmp, DEFAULT_AMA_FLAGS);
  801. }
  802. if (!ast_strlen_zero(acctcode)) {
  803. ast_channel_accountcode_set(tmp, acctcode);
  804. }
  805. ast_channel_language_set(tmp, ast_defaultlanguage);
  806. ast_channel_context_set(tmp, S_OR(context, "default"));
  807. ast_channel_exten_set(tmp, S_OR(exten, "s"));
  808. ast_channel_priority_set(tmp, 1);
  809. headp = ast_channel_varshead(tmp);
  810. AST_LIST_HEAD_INIT_NOLOCK(headp);
  811. ast_pbx_hangup_handler_init(tmp);
  812. AST_LIST_HEAD_INIT_NOLOCK(ast_channel_datastores(tmp));
  813. AST_LIST_HEAD_INIT_NOLOCK(ast_channel_autochans(tmp));
  814. channel_tech = ast_get_channel_tech(tech);
  815. if (!channel_tech && !ast_strlen_zero(tech2)) {
  816. channel_tech = ast_get_channel_tech(tech2);
  817. }
  818. if (channel_tech) {
  819. ast_channel_tech_set(tmp, channel_tech);
  820. } else {
  821. ast_channel_tech_set(tmp, &null_tech);
  822. }
  823. /* You might scream "locking inversion" at seeing this but it is actually perfectly fine.
  824. * Since the channel was just created nothing can know about it yet or even acquire it.
  825. */
  826. ast_channel_lock(tmp);
  827. ao2_lock(channels);
  828. if (assignedids && (does_id_conflict(assignedids->uniqueid) || does_id_conflict(assignedids->uniqueid2))) {
  829. ast_channel_internal_errno_set(AST_CHANNEL_ERROR_ID_EXISTS);
  830. ao2_unlock(channels);
  831. ast_channel_unlock(tmp);
  832. /* See earlier channel creation abort comment above. */
  833. return ast_channel_unref(tmp);
  834. }
  835. /* Finalize and link into the channels container. */
  836. ast_channel_internal_finalize(tmp);
  837. ast_atomic_fetchadd_int(&chancount, +1);
  838. ao2_link_flags(channels, tmp, OBJ_NOLOCK);
  839. ao2_unlock(channels);
  840. if (endpoint) {
  841. ast_endpoint_add_channel(endpoint, tmp);
  842. }
  843. /*
  844. * And now, since the channel structure is built, and has its name, let
  845. * the world know of its existance
  846. */
  847. ast_channel_stage_snapshot_done(tmp);
  848. ast_debug(1, "Channel %p '%s' allocated\n", tmp, ast_channel_name(tmp));
  849. return tmp;
  850. }
  851. struct ast_channel *__ast_channel_alloc(int needqueue, int state, const char *cid_num,
  852. const char *cid_name, const char *acctcode,
  853. const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
  854. const struct ast_channel *requestor, enum ama_flags amaflag,
  855. struct ast_endpoint *endpoint,
  856. const char *file, int line, const char *function,
  857. const char *name_fmt, ...)
  858. {
  859. va_list ap;
  860. struct ast_channel *result;
  861. va_start(ap, name_fmt);
  862. result = __ast_channel_alloc_ap(needqueue, state, cid_num, cid_name, acctcode, exten, context,
  863. assignedids, requestor, amaflag, endpoint, file, line, function, name_fmt, ap);
  864. va_end(ap);
  865. return result;
  866. }
  867. /* only do the minimum amount of work needed here to make a channel
  868. * structure that can be used to expand channel vars */
  869. #if defined(REF_DEBUG) || defined(__AST_DEBUG_MALLOC)
  870. struct ast_channel *__ast_dummy_channel_alloc(const char *file, int line, const char *function)
  871. #else
  872. struct ast_channel *ast_dummy_channel_alloc(void)
  873. #endif
  874. {
  875. struct ast_channel *tmp;
  876. struct varshead *headp;
  877. if (!(tmp = ast_channel_internal_alloc(ast_dummy_channel_destructor, NULL, NULL))) {
  878. /* Dummy channel structure allocation failure. */
  879. return NULL;
  880. }
  881. ast_pbx_hangup_handler_init(tmp);
  882. AST_LIST_HEAD_INIT_NOLOCK(ast_channel_datastores(tmp));
  883. /*
  884. * Init file descriptors to unopened state just in case
  885. * autoservice is called on the channel or something tries to
  886. * read a frame from it.
  887. */
  888. ast_channel_timingfd_set(tmp, -1);
  889. ast_channel_internal_alertpipe_clear(tmp);
  890. ast_channel_internal_fd_clear_all(tmp);
  891. #ifdef HAVE_EPOLL
  892. ast_channel_epfd_set(tmp, -1);
  893. #endif
  894. ast_channel_hold_state_set(tmp, AST_CONTROL_UNHOLD);
  895. ast_channel_internal_setup_topics(tmp);
  896. headp = ast_channel_varshead(tmp);
  897. AST_LIST_HEAD_INIT_NOLOCK(headp);
  898. return tmp;
  899. }
  900. static int __ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin, int head, struct ast_frame *after)
  901. {
  902. struct ast_frame *f;
  903. struct ast_frame *cur;
  904. unsigned int new_frames = 0;
  905. unsigned int new_voice_frames = 0;
  906. unsigned int queued_frames = 0;
  907. unsigned int queued_voice_frames = 0;
  908. AST_LIST_HEAD_NOLOCK(,ast_frame) frames;
  909. ast_channel_lock(chan);
  910. /*
  911. * Check the last frame on the queue if we are queuing the new
  912. * frames after it.
  913. */
  914. cur = AST_LIST_LAST(ast_channel_readq(chan));
  915. if (cur && cur->frametype == AST_FRAME_CONTROL && !head && (!after || after == cur)) {
  916. switch (cur->subclass.integer) {
  917. case AST_CONTROL_END_OF_Q:
  918. if (fin->frametype == AST_FRAME_CONTROL
  919. && fin->subclass.integer == AST_CONTROL_HANGUP) {
  920. /*
  921. * Destroy the end-of-Q marker frame so we can queue the hangup
  922. * frame in its place.
  923. */
  924. AST_LIST_REMOVE(ast_channel_readq(chan), cur, frame_list);
  925. ast_frfree(cur);
  926. /*
  927. * This has degenerated to a normal queue append anyway. Since
  928. * we just destroyed the last frame in the queue we must make
  929. * sure that "after" is NULL or bad things will happen.
  930. */
  931. after = NULL;
  932. break;
  933. }
  934. /* Fall through */
  935. case AST_CONTROL_HANGUP:
  936. /* Don't queue anything. */
  937. ast_channel_unlock(chan);
  938. return 0;
  939. default:
  940. break;
  941. }
  942. }
  943. /* Build copies of all the new frames and count them */
  944. AST_LIST_HEAD_INIT_NOLOCK(&frames);
  945. for (cur = fin; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
  946. if (!(f = ast_frdup(cur))) {
  947. if (AST_LIST_FIRST(&frames)) {
  948. ast_frfree(AST_LIST_FIRST(&frames));
  949. }
  950. ast_channel_unlock(chan);
  951. return -1;
  952. }
  953. AST_LIST_INSERT_TAIL(&frames, f, frame_list);
  954. new_frames++;
  955. if (f->frametype == AST_FRAME_VOICE) {
  956. new_voice_frames++;
  957. }
  958. }
  959. /* Count how many frames exist on the queue */
  960. AST_LIST_TRAVERSE(ast_channel_readq(chan), cur, frame_list) {
  961. queued_frames++;
  962. if (cur->frametype == AST_FRAME_VOICE) {
  963. queued_voice_frames++;
  964. }
  965. }
  966. if ((queued_frames + new_frames > 128 || queued_voice_frames + new_voice_frames > 96)) {
  967. int count = 0;
  968. ast_log(LOG_WARNING, "Exceptionally long %squeue length queuing to %s\n", queued_frames + new_frames > 128 ? "" : "voice ", ast_channel_name(chan));
  969. AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_readq(chan), cur, frame_list) {
  970. /* Save the most recent frame */
  971. if (!AST_LIST_NEXT(cur, frame_list)) {
  972. break;
  973. } else if (cur->frametype == AST_FRAME_VOICE || cur->frametype == AST_FRAME_VIDEO || cur->frametype == AST_FRAME_NULL) {
  974. if (++count > 64) {
  975. break;
  976. }
  977. AST_LIST_REMOVE_CURRENT(frame_list);
  978. ast_frfree(cur);
  979. /* Read from the alert pipe for each flushed frame. */
  980. ast_channel_internal_alert_read(chan);
  981. }
  982. }
  983. AST_LIST_TRAVERSE_SAFE_END;
  984. }
  985. if (after) {
  986. AST_LIST_INSERT_LIST_AFTER(ast_channel_readq(chan), &frames, after, frame_list);
  987. } else {
  988. if (head) {
  989. AST_LIST_APPEND_LIST(&frames, ast_channel_readq(chan), frame_list);
  990. AST_LIST_HEAD_INIT_NOLOCK(ast_channel_readq(chan));
  991. }
  992. AST_LIST_APPEND_LIST(ast_channel_readq(chan), &frames, frame_list);
  993. }
  994. if (ast_channel_alert_writable(chan)) {
  995. /* Write to the alert pipe for each added frame */
  996. while (new_frames--) {
  997. if (ast_channel_alert_write(chan)) {
  998. ast_log(LOG_WARNING, "Unable to write to alert pipe on %s (qlen = %u): %s!\n",
  999. ast_channel_name(chan), queued_frames, strerror(errno));
  1000. break;
  1001. }
  1002. }
  1003. } else if (ast_channel_timingfd(chan) > -1) {
  1004. ast_timer_enable_continuous(ast_channel_timer(chan));
  1005. } else if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING)) {
  1006. pthread_kill(ast_channel_blocker(chan), SIGURG);
  1007. }
  1008. ast_channel_unlock(chan);
  1009. return 0;
  1010. }
  1011. int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
  1012. {
  1013. return __ast_queue_frame(chan, fin, 0, NULL);
  1014. }
  1015. int ast_queue_frame_head(struct ast_channel *chan, struct ast_frame *fin)
  1016. {
  1017. return __ast_queue_frame(chan, fin, 1, NULL);
  1018. }
  1019. /*! \brief Queue a hangup frame for channel */
  1020. int ast_queue_hangup(struct ast_channel *chan)
  1021. {
  1022. struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
  1023. int res;
  1024. /* Yeah, let's not change a lock-critical value without locking */
  1025. ast_channel_lock(chan);
  1026. ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
  1027. ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), NULL);
  1028. res = ast_queue_frame(chan, &f);
  1029. ast_channel_unlock(chan);
  1030. return res;
  1031. }
  1032. /*! \brief Queue a hangup frame for channel */
  1033. int ast_queue_hangup_with_cause(struct ast_channel *chan, int cause)
  1034. {
  1035. RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
  1036. struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
  1037. int res;
  1038. if (cause >= 0) {
  1039. f.data.uint32 = cause;
  1040. }
  1041. /* Yeah, let's not change a lock-critical value without locking */
  1042. ast_channel_lock(chan);
  1043. ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
  1044. if (cause < 0) {
  1045. f.data.uint32 = ast_channel_hangupcause(chan);
  1046. }
  1047. blob = ast_json_pack("{s: i}",
  1048. "cause", cause);
  1049. ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), blob);
  1050. res = ast_queue_frame(chan, &f);
  1051. ast_channel_unlock(chan);
  1052. return res;
  1053. }
  1054. int ast_queue_hold(struct ast_channel *chan, const char *musicclass)
  1055. {
  1056. struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HOLD };
  1057. struct ast_json *blob = NULL;
  1058. int res;
  1059. if (!ast_strlen_zero(musicclass)) {
  1060. f.data.ptr = (void *) musicclass;
  1061. f.datalen = strlen(musicclass) + 1;
  1062. blob = ast_json_pack("{s: s}",
  1063. "musicclass", musicclass);
  1064. }
  1065. ast_channel_publish_cached_blob(chan, ast_channel_hold_type(), blob);
  1066. res = ast_queue_frame(chan, &f);
  1067. ast_json_unref(blob);
  1068. return res;
  1069. }
  1070. int ast_queue_unhold(struct ast_channel *chan)
  1071. {
  1072. struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_UNHOLD };
  1073. int res;
  1074. ast_channel_publish_cached_blob(chan, ast_channel_unhold_type(), NULL);
  1075. res = ast_queue_frame(chan, &f);
  1076. return res;
  1077. }
  1078. /*! \brief Queue a control frame */
  1079. int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
  1080. {
  1081. struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control };
  1082. return ast_queue_frame(chan, &f);
  1083. }
  1084. /*! \brief Queue a control frame with payload */
  1085. int ast_queue_control_data(struct ast_channel *chan, enum ast_control_frame_type control,
  1086. const void *data, size_t datalen)
  1087. {
  1088. struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control, .data.ptr = (void *) data, .datalen = datalen };
  1089. return ast_queue_frame(chan, &f);
  1090. }
  1091. /*! \brief Set defer DTMF flag on channel */
  1092. int ast_channel_defer_dtmf(struct ast_channel *chan)
  1093. {
  1094. int pre = 0;
  1095. if (chan) {
  1096. ast_channel_lock(chan);
  1097. pre = ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
  1098. ast_set_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
  1099. ast_channel_unlock(chan);
  1100. }
  1101. return pre;
  1102. }
  1103. /*! \brief Unset defer DTMF flag on channel */
  1104. void ast_channel_undefer_dtmf(struct ast_channel *chan)
  1105. {
  1106. if (chan) {
  1107. ast_channel_clear_flag(chan, AST_FLAG_DEFER_DTMF);
  1108. }
  1109. }
  1110. struct ast_channel *ast_channel_callback(ao2_callback_data_fn *cb_fn, void *arg,
  1111. void *data, int ao2_flags)
  1112. {
  1113. return ao2_callback_data(channels, ao2_flags, cb_fn, arg, data);
  1114. }
  1115. static int ast_channel_by_name_cb(void *obj, void *arg, void *data, int flags)
  1116. {
  1117. struct ast_channel *chan = obj;
  1118. const char *name = arg;
  1119. size_t name_len = *(size_t *) data;
  1120. int ret = CMP_MATCH;
  1121. if (ast_strlen_zero(name)) {
  1122. ast_log(LOG_ERROR, "BUG! Must supply a channel name or partial name to match!\n");
  1123. return CMP_STOP;
  1124. }
  1125. ast_channel_lock(chan);
  1126. if ((!name_len && strcasecmp(ast_channel_name(chan), name))
  1127. || (name_len && strncasecmp(ast_channel_name(chan), name, name_len))) {
  1128. ret = 0; /* name match failed, keep looking */
  1129. }
  1130. ast_channel_unlock(chan);
  1131. return ret;
  1132. }
  1133. static int ast_channel_by_exten_cb(void *obj, void *arg, void *data, int flags)
  1134. {
  1135. struct ast_channel *chan = obj;
  1136. char *context = arg;
  1137. char *exten = data;
  1138. int ret = CMP_MATCH;
  1139. if (ast_strlen_zero(exten) || ast_strlen_zero(context)) {
  1140. ast_log(LOG_ERROR, "BUG! Must have a context and extension to match!\n");
  1141. return CMP_STOP;
  1142. }
  1143. ast_channel_lock(chan);
  1144. if (strcasecmp(ast_channel_context(chan), context) && strcasecmp(ast_channel_macrocontext(chan), context)) {
  1145. ret = 0; /* Context match failed, continue */
  1146. } else if (strcasecmp(ast_channel_exten(chan), exten) && strcasecmp(ast_channel_macroexten(chan), exten)) {
  1147. ret = 0; /* Extension match failed, continue */
  1148. }
  1149. ast_channel_unlock(chan);
  1150. return ret;
  1151. }
  1152. static int ast_channel_by_uniqueid_cb(void *obj, void *arg, void *data, int flags)
  1153. {
  1154. struct ast_channel *chan = obj;
  1155. char *uniqueid = arg;
  1156. size_t id_len = *(size_t *) data;
  1157. int ret = CMP_MATCH;
  1158. if (ast_strlen_zero(uniqueid)) {
  1159. ast_log(LOG_ERROR, "BUG! Must supply a uniqueid or partial uniqueid to match!\n");
  1160. return CMP_STOP;
  1161. }
  1162. ast_channel_lock(chan);
  1163. if ((!id_len && strcasecmp(ast_channel_uniqueid(chan), uniqueid))
  1164. || (id_len && strncasecmp(ast_channel_uniqueid(chan), uniqueid, id_len))) {
  1165. ret = 0; /* uniqueid match failed, keep looking */
  1166. }
  1167. ast_channel_unlock(chan);
  1168. return ret;
  1169. }
  1170. struct ast_channel_iterator {
  1171. /* storage for non-dynamically allocated iterator */
  1172. struct ao2_iterator simple_iterator;
  1173. /* pointer to the actual iterator (simple_iterator or a dynamically
  1174. * allocated iterator)
  1175. */
  1176. struct ao2_iterator *active_iterator;
  1177. };
  1178. struct ast_channel_iterator *ast_channel_iterator_destroy(struct ast_channel_iterator *i)
  1179. {
  1180. ao2_iterator_destroy(i->active_iterator);
  1181. ast_free(i);
  1182. return NULL;
  1183. }
  1184. struct ast_channel_iterator *ast_channel_iterator_by_exten_new(const char *exten, const char *context)
  1185. {
  1186. struct ast_channel_iterator *i;
  1187. char *l_exten = (char *) exten;
  1188. char *l_context = (char *) context;
  1189. if (!(i = ast_calloc(1, sizeof(*i)))) {
  1190. return NULL;
  1191. }
  1192. i->active_iterator = (void *) ast_channel_callback(ast_channel_by_exten_cb,
  1193. l_context, l_exten, OBJ_MULTIPLE);
  1194. if (!i->active_iterator) {
  1195. ast_free(i);
  1196. return NULL;
  1197. }
  1198. return i;
  1199. }
  1200. struct ast_channel_iterator *ast_channel_iterator_by_name_new(const char *name, size_t name_len)
  1201. {
  1202. struct ast_channel_iterator *i;
  1203. char *l_name = (char *) name;
  1204. if (!(i = ast_calloc(1, sizeof(*i)))) {
  1205. return NULL;
  1206. }
  1207. i->active_iterator = (void *) ast_channel_callback(ast_channel_by_name_cb,
  1208. l_name, &name_len,
  1209. OBJ_MULTIPLE | (name_len == 0 /* match the whole word, so optimize */ ? OBJ_KEY : 0));
  1210. if (!i->active_iterator) {
  1211. ast_free(i);
  1212. return NULL;
  1213. }
  1214. return i;
  1215. }
  1216. struct ast_channel_iterator *ast_channel_iterator_all_new(void)
  1217. {
  1218. struct ast_channel_iterator *i;
  1219. if (!(i = ast_calloc(1, sizeof(*i)))) {
  1220. return NULL;
  1221. }
  1222. i->simple_iterator = ao2_iterator_init(channels, 0);
  1223. i->active_iterator = &i->simple_iterator;
  1224. return i;
  1225. }
  1226. struct ast_channel *ast_channel_iterator_next(struct ast_channel_iterator *i)
  1227. {
  1228. return ao2_iterator_next(i->active_iterator);
  1229. }
  1230. /* Legacy function, not currently used for lookups, but we need a cmp_fn */
  1231. static int ast_channel_cmp_cb(void *obj, void *arg, int flags)
  1232. {
  1233. ast_log(LOG_ERROR, "BUG! Should never be called!\n");
  1234. return CMP_STOP;
  1235. }
  1236. struct ast_channel *ast_channel_get_by_name_prefix(const char *name, size_t name_len)
  1237. {
  1238. struct ast_channel *chan;
  1239. char *l_name = (char *) name;
  1240. chan = ast_channel_callback(ast_channel_by_name_cb, l_name, &name_len,
  1241. (name_len == 0) /* optimize if it is a complete name match */ ? OBJ_KEY : 0);
  1242. if (chan) {
  1243. return chan;
  1244. }
  1245. if (ast_strlen_zero(l_name)) {
  1246. /* We didn't have a name to search for so quit. */
  1247. return NULL;
  1248. }
  1249. /* Now try a search for uniqueid. */
  1250. return ast_channel_callback(ast_channel_by_uniqueid_cb, l_name, &name_len, 0);
  1251. }
  1252. struct ast_channel *ast_channel_get_by_name(const char *name)
  1253. {
  1254. return ast_channel_get_by_name_prefix(name, 0);
  1255. }
  1256. struct ast_channel *ast_channel_get_by_exten(const char *exten, const char *context)
  1257. {
  1258. char *l_exten = (char *) exten;
  1259. char *l_context = (char *) context;
  1260. return ast_channel_callback(ast_channel_by_exten_cb, l_context, l_exten, 0);
  1261. }
  1262. int ast_is_deferrable_frame(const struct ast_frame *frame)
  1263. {
  1264. /* Do not add a default entry in this switch statement. Each new
  1265. * frame type should be addressed directly as to whether it should
  1266. * be queued up or not.
  1267. */
  1268. switch (frame->frametype) {
  1269. case AST_FRAME_BRIDGE_ACTION:
  1270. case AST_FRAME_BRIDGE_ACTION_SYNC:
  1271. case AST_FRAME_CONTROL:
  1272. case AST_FRAME_TEXT:
  1273. case AST_FRAME_TEXT_DATA:
  1274. case AST_FRAME_IMAGE:
  1275. case AST_FRAME_HTML:
  1276. return 1;
  1277. case AST_FRAME_DTMF_END:
  1278. case AST_FRAME_DTMF_BEGIN:
  1279. case AST_FRAME_VOICE:
  1280. case AST_FRAME_VIDEO:
  1281. case AST_FRAME_NULL:
  1282. case AST_FRAME_IAX:
  1283. case AST_FRAME_CNG:
  1284. case AST_FRAME_MODEM:
  1285. case AST_FRAME_RTCP:
  1286. return 0;
  1287. }
  1288. return 0;
  1289. }
  1290. /*! \brief Wait, look for hangups and condition arg */
  1291. int ast_safe_sleep_conditional(struct ast_channel *chan, int timeout_ms, int (*cond)(void*), void *data)
  1292. {
  1293. struct ast_frame *f;
  1294. struct ast_silence_generator *silgen = NULL;
  1295. int res = 0;
  1296. struct timeval start;
  1297. int ms;
  1298. AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
  1299. AST_LIST_HEAD_INIT_NOLOCK(&deferred_frames);
  1300. /* If no other generator is present, start silencegen while waiting */
  1301. if (ast_opt_transmit_silence && !ast_channel_generatordata(chan)) {
  1302. silgen = ast_channel_start_silence_generator(chan);
  1303. }
  1304. start = ast_tvnow();
  1305. while ((ms = ast_remaining_ms(start, timeout_ms))) {
  1306. struct ast_frame *dup_f = NULL;
  1307. if (cond && ((*cond)(data) == 0)) {
  1308. break;
  1309. }
  1310. ms = ast_waitfor(chan, ms);
  1311. if (ms < 0) {
  1312. res = -1;
  1313. break;
  1314. }
  1315. if (ms > 0) {
  1316. f = ast_read(chan);
  1317. if (!f) {
  1318. res = -1;
  1319. break;
  1320. }
  1321. if (!ast_is_deferrable_frame(f)) {
  1322. ast_frfree(f);
  1323. continue;
  1324. }
  1325. if ((dup_f = ast_frisolate(f))) {
  1326. if (dup_f != f) {
  1327. ast_frfree(f);
  1328. }
  1329. AST_LIST_INSERT_HEAD(&deferred_frames, dup_f, frame_list);
  1330. }
  1331. }
  1332. }
  1333. /* stop silgen if present */
  1334. if (silgen) {
  1335. ast_channel_stop_silence_generator(chan, silgen);
  1336. }
  1337. /* We need to free all the deferred frames, but we only need to
  1338. * queue the deferred frames if there was no error and no
  1339. * hangup was received
  1340. */
  1341. ast_channel_lock(chan);
  1342. while ((f = AST_LIST_REMOVE_HEAD(&deferred_frames, frame_list))) {
  1343. if (!res) {
  1344. ast_queue_frame_head(chan, f);
  1345. }
  1346. ast_frfree(f);
  1347. }
  1348. ast_channel_unlock(chan);
  1349. return res;
  1350. }
  1351. /*! \brief Wait, look for hangups */
  1352. int ast_safe_sleep(struct ast_channel *chan, int ms)
  1353. {
  1354. return ast_safe_sleep_conditional(chan, ms, NULL, NULL);
  1355. }
  1356. struct ast_channel *ast_channel_release(struct ast_channel *chan)
  1357. {
  1358. /* Safe, even if already unlinked. */
  1359. ao2_unlink(channels, chan);
  1360. return ast_channel_unref(chan);
  1361. }
  1362. void ast_party_name_init(struct ast_party_name *init)
  1363. {
  1364. init->str = NULL;
  1365. init->char_set = AST_PARTY_CHAR_SET_ISO8859_1;
  1366. init->presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
  1367. init->valid = 0;
  1368. }
  1369. void ast_party_name_copy(struct ast_party_name *dest, const struct ast_party_name *src)
  1370. {
  1371. if (dest == src) {
  1372. /* Don't copy to self */
  1373. return;
  1374. }
  1375. ast_free(dest->str);
  1376. dest->str = ast_strdup(src->str);
  1377. dest->char_set = src->char_set;
  1378. dest->presentation = src->presentation;
  1379. dest->valid = src->valid;
  1380. }
  1381. void ast_party_name_set_init(struct ast_party_name *init, const struct ast_party_name *guide)
  1382. {
  1383. init->str = NULL;
  1384. init->char_set = guide->char_set;
  1385. init->presentation = guide->presentation;
  1386. init->valid = guide->valid;
  1387. }
  1388. void ast_party_name_set(struct ast_party_name *dest, const struct ast_party_name *src)
  1389. {
  1390. if (dest == src) {
  1391. /* Don't set to self */
  1392. return;
  1393. }
  1394. if (src->str && src->str != dest->str) {
  1395. ast_free(dest->str);
  1396. dest->str = ast_strdup(src->str);
  1397. }
  1398. dest->char_set = src->char_set;
  1399. dest->presentation = src->presentation;
  1400. dest->valid = src->valid;
  1401. }
  1402. void ast_party_name_free(struct ast_party_name *doomed)
  1403. {
  1404. ast_free(doomed->str);
  1405. doomed->str = NULL;
  1406. }
  1407. void ast_party_number_init(struct ast_party_number *init)
  1408. {
  1409. init->str = NULL;
  1410. init->plan = 0;/* Unknown */
  1411. init->presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
  1412. init->valid = 0;
  1413. }
  1414. void ast_party_number_copy(struct ast_party_number *dest, const struct ast_party_number *src)
  1415. {
  1416. if (dest == src) {
  1417. /* Don't copy to self */
  1418. return;
  1419. }
  1420. ast_free(dest->str);
  1421. dest->str = ast_strdup(src->str);
  1422. dest->plan = src->plan;
  1423. dest->presentation = src->presentation;
  1424. dest->valid = src->valid;
  1425. }
  1426. void ast_party_number_set_init(struct ast_party_number *init, const struct ast_party_number *guide)
  1427. {
  1428. init->str = NULL;
  1429. init->plan = guide->plan;
  1430. init->presentation = guide->presentation;
  1431. init->valid = guide->valid;
  1432. }
  1433. void ast_party_number_set(struct ast_party_number *dest, const struct ast_party_number *src)
  1434. {
  1435. if (dest == src) {
  1436. /* Don't set to self */
  1437. return;
  1438. }
  1439. if (src->str && src->str != dest->str) {
  1440. ast_free(dest->str);
  1441. dest->str = ast_strdup(src->str);
  1442. }
  1443. dest->plan = src->plan;
  1444. dest->presentation = src->presentation;
  1445. dest->valid = src->valid;
  1446. }
  1447. void ast_party_number_free(struct ast_party_number *doomed)
  1448. {
  1449. ast_free(doomed->str);
  1450. doomed->str = NULL;
  1451. }
  1452. void ast_party_subaddress_init(struct ast_party_subaddress *init)
  1453. {
  1454. init->str = NULL;
  1455. init->type = 0;
  1456. init->odd_even_indicator = 0;
  1457. init->valid = 0;
  1458. }
  1459. void ast_party_subaddress_copy(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
  1460. {
  1461. if (dest == src) {
  1462. /* Don't copy to self */
  1463. return;
  1464. }
  1465. ast_free(dest->str);
  1466. dest->str = ast_strdup(src->str);
  1467. dest->type = src->type;
  1468. dest->odd_even_indicator = src->odd_even_indicator;
  1469. dest->valid = src->valid;
  1470. }
  1471. void ast_party_subaddress_set_init(struct ast_party_subaddress *init, const struct ast_party_subaddress *guide)
  1472. {
  1473. init->str = NULL;
  1474. init->type = guide->type;
  1475. init->odd_even_indicator = guide->odd_even_indicator;
  1476. init->valid = guide->valid;
  1477. }
  1478. void ast_party_subaddress_set(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
  1479. {
  1480. if (dest == src) {
  1481. /* Don't set to self */
  1482. return;
  1483. }
  1484. if (src->str && src->str != dest->str) {
  1485. ast_free(dest->str);
  1486. dest->str = ast_strdup(src->str);
  1487. }
  1488. dest->type = src->type;
  1489. dest->odd_even_indicator = src->odd_even_indicator;
  1490. dest->valid = src->valid;
  1491. }
  1492. void ast_party_subaddress_free(struct ast_party_subaddress *doomed)
  1493. {
  1494. ast_free(doomed->str);
  1495. doomed->str = NULL;
  1496. }
  1497. void ast_set_party_id_all(struct ast_set_party_id *update_id)
  1498. {
  1499. update_id->name = 1;
  1500. update_id->number = 1;
  1501. update_id->subaddress = 1;
  1502. }
  1503. void ast_party_id_init(struct ast_party_id *init)
  1504. {
  1505. ast_party_name_init(&init->name);
  1506. ast_party_number_init(&init->number);
  1507. ast_party_subaddress_init(&init->subaddress);
  1508. init->tag = NULL;
  1509. }
  1510. void ast_party_id_copy(struct ast_party_id *dest, const struct ast_party_id *src)
  1511. {
  1512. if (dest == src) {
  1513. /* Don't copy to self */
  1514. return;
  1515. }
  1516. ast_party_name_copy(&dest->name, &src->name);
  1517. ast_party_number_copy(&dest->number, &src->number);
  1518. ast_party_subaddress_copy(&dest->subaddress, &src->subaddress);
  1519. ast_free(dest->tag);
  1520. dest->tag = ast_strdup(src->tag);
  1521. }
  1522. void ast_party_id_set_init(struct ast_party_id *init, const struct ast_party_id *guide)
  1523. {
  1524. ast_party_name_set_init(&init->name, &guide->name);
  1525. ast_party_number_set_init(&init->number, &guide->number);
  1526. ast_party_subaddress_set_init(&init->subaddress, &guide->subaddress);
  1527. init->tag = NULL;
  1528. }
  1529. void ast_party_id_set(struct ast_party_id *dest, const struct ast_party_id *src, const struct ast_set_party_id *update)
  1530. {
  1531. if (dest == src) {
  1532. /* Don't set to self */
  1533. return;
  1534. }
  1535. if (!update || update->name) {
  1536. ast_party_name_set(&dest->name, &src->name);
  1537. }
  1538. if (!update || update->number) {
  1539. ast_party_number_set(&dest->number, &src->number);
  1540. }
  1541. if (!update || update->subaddress) {
  1542. ast_party_subaddress_set(&dest->subaddress, &src->subaddress);
  1543. }
  1544. if (src->tag && src->tag != dest->tag) {
  1545. ast_free(dest->tag);
  1546. dest->tag = ast_strdup(src->tag);
  1547. }
  1548. }
  1549. void ast_party_id_free(struct ast_party_id *doomed)
  1550. {
  1551. ast_party_name_free(&doomed->name);
  1552. ast_party_number_free(&doomed->number);
  1553. ast_party_subaddress_free(&doomed->subaddress);
  1554. ast_free(doomed->tag);
  1555. doomed->tag = NULL;
  1556. }
  1557. int ast_party_id_presentation(const struct ast_party_id *id)
  1558. {
  1559. int number_priority;
  1560. int number_value;
  1561. int number_screening;
  1562. int name_priority;
  1563. int name_value;
  1564. /* Determine name presentation priority. */
  1565. if (!id->name.valid) {
  1566. name_value = AST_PRES_UNAVAILABLE;
  1567. name_priority = 3;
  1568. } else {
  1569. name_value = id->name.presentation & AST_PRES_RESTRICTION;
  1570. switch (name_value) {
  1571. case AST_PRES_RESTRICTED:
  1572. name_priority = 0;
  1573. break;
  1574. case AST_PRES_ALLOWED:
  1575. name_priority = 1;
  1576. break;
  1577. case AST_PRES_UNAVAILABLE:
  1578. name_priority = 2;
  1579. break;
  1580. default:
  1581. name_value = AST_PRES_UNAVAILABLE;
  1582. name_priority = 3;
  1583. break;
  1584. }
  1585. }
  1586. /* Determine number presentation priority. */
  1587. if (!id->number.valid) {
  1588. number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
  1589. number_value = AST_PRES_UNAVAILABLE;
  1590. number_priority = 3;
  1591. } else {
  1592. number_screening = id->number.presentation & AST_PRES_NUMBER_TYPE;
  1593. number_value = id->number.presentation & AST_PRES_RESTRICTION;
  1594. switch (number_value) {
  1595. case AST_PRES_RESTRICTED:
  1596. number_priority = 0;
  1597. break;
  1598. case AST_PRES_ALLOWED:
  1599. number_priority = 1;
  1600. break;
  1601. case AST_PRES_UNAVAILABLE:
  1602. number_priority = 2;
  1603. break;
  1604. default:
  1605. number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
  1606. number_value = AST_PRES_UNAVAILABLE;
  1607. number_priority = 3;
  1608. break;
  1609. }
  1610. }
  1611. /* Select the wining presentation value. */
  1612. if (name_priority < number_priority) {
  1613. number_value = name_value;
  1614. }
  1615. if (number_value == AST_PRES_UNAVAILABLE) {
  1616. return AST_PRES_NUMBER_NOT_AVAILABLE;
  1617. }
  1618. return number_value | number_screening;
  1619. }
  1620. void ast_party_id_invalidate(struct ast_party_id *id)
  1621. {
  1622. id->name.valid = 0;
  1623. id->number.valid = 0;
  1624. id->subaddress.valid = 0;
  1625. }
  1626. void ast_party_id_reset(struct ast_party_id *id)
  1627. {
  1628. ast_party_id_free(id);
  1629. ast_party_id_init(id);
  1630. }
  1631. struct ast_party_id ast_party_id_merge(struct ast_party_id *base, struct ast_party_id *overlay)
  1632. {
  1633. struct ast_party_id merged;
  1634. merged = *base;
  1635. if (overlay->name.valid) {
  1636. merged.name = overlay->name;
  1637. }
  1638. if (overlay->number.valid) {
  1639. merged.number = overlay->number;
  1640. }
  1641. if (overlay->subaddress.valid) {
  1642. merged.subaddress = overlay->subaddress;
  1643. }
  1644. /* Note the actual structure is returned and not a pointer to it! */
  1645. return merged;
  1646. }
  1647. void ast_party_id_merge_copy(struct ast_party_id *dest, struct ast_party_id *base, struct ast_party_id *overlay)
  1648. {
  1649. struct ast_party_id merged;
  1650. merged = ast_party_id_merge(base, overlay);
  1651. ast_party_id_copy(dest, &merged);
  1652. }
  1653. void ast_party_dialed_init(struct ast_party_dialed *init)
  1654. {
  1655. init->number.str = NULL;
  1656. init->number.plan = 0;/* Unknown */
  1657. ast_party_subaddress_init(&init->subaddress);
  1658. init->transit_network_select = 0;
  1659. }
  1660. void ast_party_dialed_copy(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
  1661. {
  1662. if (dest == src) {
  1663. /* Don't copy to self */
  1664. return;
  1665. }
  1666. ast_free(dest->number.str);
  1667. dest->number.str = ast_strdup(src->number.str);
  1668. dest->number.plan = src->number.plan;
  1669. ast_party_subaddress_copy(&dest->subaddress, &src->subaddress);
  1670. dest->transit_network_select = src->transit_network_select;
  1671. }
  1672. void ast_party_dialed_set_init(struct ast_party_dialed *init, const struct ast_party_dialed *guide)
  1673. {
  1674. init->number.str = NULL;
  1675. init->number.plan = guide->number.plan;
  1676. ast_party_subaddress_set_init(&init->subaddress, &guide->subaddress);
  1677. init->transit_network_select = guide->transit_network_select;
  1678. }
  1679. void ast_party_dialed_set(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
  1680. {
  1681. if (src->number.str && src->number.str != dest->number.str) {
  1682. ast_free(dest->number.str);
  1683. dest->number.str = ast_strdup(src->number.str);
  1684. }
  1685. dest->number.plan = src->number.plan;
  1686. ast_party_subaddress_set(&dest->subaddress, &src->subaddress);
  1687. dest->transit_network_select = src->transit_network_select;
  1688. }
  1689. void ast_party_dialed_free(struct ast_party_dialed *doomed)
  1690. {
  1691. ast_free(doomed->number.str);
  1692. doomed->number.str = NULL;
  1693. ast_party_subaddress_free(&doomed->subaddress);
  1694. }
  1695. void ast_party_caller_init(struct ast_party_caller *init)
  1696. {
  1697. ast_party_id_init(&init->id);
  1698. ast_party_id_init(&init->ani);
  1699. ast_party_id_init(&init->priv);
  1700. init->ani2 = 0;
  1701. }
  1702. void ast_party_caller_copy(struct ast_party_caller *dest, const struct ast_party_caller *src)
  1703. {
  1704. if (dest == src) {
  1705. /* Don't copy to self */
  1706. return;
  1707. }
  1708. ast_party_id_copy(&dest->id, &src->id);
  1709. ast_party_id_copy(&dest->ani, &src->ani);
  1710. ast_party_id_copy(&dest->priv, &src->priv);
  1711. dest->ani2 = src->ani2;
  1712. }
  1713. void ast_party_caller_set_init(struct ast_party_caller *init, const struct ast_party_caller *guide)
  1714. {
  1715. ast_party_id_set_init(&init->id, &guide->id);
  1716. ast_party_id_set_init(&init->ani, &guide->ani);
  1717. ast_party_id_set_init(&init->priv, &guide->priv);
  1718. init->ani2 = guide->ani2;
  1719. }
  1720. void ast_party_caller_set(struct ast_party_caller *dest, const struct ast_party_caller *src, const struct ast_set_party_caller *update)
  1721. {
  1722. ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
  1723. ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
  1724. ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
  1725. dest->ani2 = src->ani2;
  1726. }
  1727. void ast_party_caller_free(struct ast_party_caller *doomed)
  1728. {
  1729. ast_party_id_free(&doomed->id);
  1730. ast_party_id_free(&doomed->ani);
  1731. ast_party_id_free(&doomed->priv);
  1732. }
  1733. void ast_party_connected_line_init(struct ast_party_connected_line *init)
  1734. {
  1735. ast_party_id_init(&init->id);
  1736. ast_party_id_init(&init->ani);
  1737. ast_party_id_init(&init->priv);
  1738. init->ani2 = 0;
  1739. init->source = AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN;
  1740. }
  1741. void ast_party_connected_line_copy(struct ast_party_connected_line *dest, const struct ast_party_connected_line *src)
  1742. {
  1743. if (dest == src) {
  1744. /* Don't copy to self */
  1745. return;
  1746. }
  1747. ast_party_id_copy(&dest->id, &src->id);
  1748. ast_party_id_copy(&dest->ani, &src->ani);
  1749. ast_party_id_copy(&dest->priv, &src->priv);
  1750. dest->ani2 = src->ani2;
  1751. dest->source = src->source;
  1752. }
  1753. void ast_party_connected_line_set_init(struct ast_party_connected_line *init, const struct ast_party_connected_line *guide)
  1754. {
  1755. ast_party_id_set_init(&init->id, &guide->id);
  1756. ast_party_id_set_init(&init->ani, &guide->ani);
  1757. ast_party_id_set_init(&init->priv, &guide->priv);
  1758. init->ani2 = guide->ani2;
  1759. init->source = guide->source;
  1760. }
  1761. void ast_party_connected_line_set(struct ast_party_connected_line *dest, const struct ast_party_connected_line *src, const struct ast_set_party_connected_line *update)
  1762. {
  1763. ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
  1764. ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
  1765. ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
  1766. dest->ani2 = src->ani2;
  1767. dest->source = src->source;
  1768. }
  1769. void ast_party_connected_line_collect_caller(struct ast_party_connected_line *connected, struct ast_party_caller *caller)
  1770. {
  1771. connected->id = caller->id;
  1772. connected->ani = caller->ani;
  1773. connected->priv = caller->priv;
  1774. connected->ani2 = caller->ani2;
  1775. connected->source = AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN;
  1776. }
  1777. void ast_party_connected_line_free(struct ast_party_connected_line *doomed)
  1778. {
  1779. ast_party_id_free(&doomed->id);
  1780. ast_party_id_free(&doomed->ani);
  1781. ast_party_id_free(&doomed->priv);
  1782. }
  1783. void ast_party_redirecting_reason_init(struct ast_party_redirecting_reason *init)
  1784. {
  1785. init->str = NULL;
  1786. init->code = AST_REDIRECTING_REASON_UNKNOWN;
  1787. }
  1788. void ast_party_redirecting_reason_copy(struct ast_party_redirecting_reason *dest, const struct ast_party_redirecting_reason *src)
  1789. {
  1790. if (dest == src) {
  1791. return;
  1792. }
  1793. ast_free(dest->str);
  1794. dest->str = ast_strdup(src->str);
  1795. dest->code = src->code;
  1796. }
  1797. void ast_party_redirecting_reason_set_init(struct ast_party_redirecting_reason *init, const struct ast_party_redirecting_reason *guide)
  1798. {
  1799. init->str = NULL;
  1800. init->code = guide->code;
  1801. }
  1802. void ast_party_redirecting_reason_set(struct ast_party_redirecting_reason *dest, const struct ast_party_redirecting_reason *src)
  1803. {
  1804. if (dest == src) {
  1805. return;
  1806. }
  1807. if (src->str && src->str != dest->str) {
  1808. ast_free(dest->str);
  1809. dest->str = ast_strdup(src->str);
  1810. }
  1811. dest->code = src->code;
  1812. }
  1813. void ast_party_redirecting_reason_free(struct ast_party_redirecting_reason *doomed)
  1814. {
  1815. ast_free(doomed->str);
  1816. }
  1817. void ast_party_redirecting_init(struct ast_party_redirecting *init)
  1818. {
  1819. ast_party_id_init(&init->orig);
  1820. ast_party_id_init(&init->from);
  1821. ast_party_id_init(&init->to);
  1822. ast_party_id_init(&init->priv_orig);
  1823. ast_party_id_init(&init->priv_from);
  1824. ast_party_id_init(&init->priv_to);
  1825. ast_party_redirecting_reason_init(&init->reason);
  1826. ast_party_redirecting_reason_init(&init->orig_reason);
  1827. init->count = 0;
  1828. }
  1829. void ast_party_redirecting_copy(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src)
  1830. {
  1831. if (dest == src) {
  1832. /* Don't copy to self */
  1833. return;
  1834. }
  1835. ast_party_id_copy(&dest->orig, &src->orig);
  1836. ast_party_id_copy(&dest->from, &src->from);
  1837. ast_party_id_copy(&dest->to, &src->to);
  1838. ast_party_id_copy(&dest->priv_orig, &src->priv_orig);
  1839. ast_party_id_copy(&dest->priv_from, &src->priv_from);
  1840. ast_party_id_copy(&dest->priv_to, &src->priv_to);
  1841. ast_party_redirecting_reason_copy(&dest->reason, &src->reason);
  1842. ast_party_redirecting_reason_copy(&dest->orig_reason, &src->orig_reason);
  1843. dest->count = src->count;
  1844. }
  1845. void ast_party_redirecting_set_init(struct ast_party_redirecting *init, const struct ast_party_redirecting *guide)
  1846. {
  1847. ast_party_id_set_init(&init->orig, &guide->orig);
  1848. ast_party_id_set_init(&init->from, &guide->from);
  1849. ast_party_id_set_init(&init->to, &guide->to);
  1850. ast_party_id_set_init(&init->priv_orig, &guide->priv_orig);
  1851. ast_party_id_set_init(&init->priv_from, &guide->priv_from);
  1852. ast_party_id_set_init(&init->priv_to, &guide->priv_to);
  1853. ast_party_redirecting_reason_set_init(&init->reason, &guide->reason);
  1854. ast_party_redirecting_reason_set_init(&init->orig_reason, &guide->orig_reason);
  1855. init->count = guide->count;
  1856. }
  1857. void ast_party_redirecting_set(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src, const struct ast_set_party_redirecting *update)
  1858. {
  1859. ast_party_id_set(&dest->orig, &src->orig, update ? &update->orig : NULL);
  1860. ast_party_id_set(&dest->from, &src->from, update ? &update->from : NULL);
  1861. ast_party_id_set(&dest->to, &src->to, update ? &update->to : NULL);
  1862. ast_party_id_set(&dest->priv_orig, &src->priv_orig, update ? &update->priv_orig : NULL);
  1863. ast_party_id_set(&dest->priv_from, &src->priv_from, update ? &update->priv_from : NULL);
  1864. ast_party_id_set(&dest->priv_to, &src->priv_to, update ? &update->priv_to : NULL);
  1865. ast_party_redirecting_reason_set(&dest->reason, &src->reason);
  1866. ast_party_redirecting_reason_set(&dest->orig_reason, &src->orig_reason);
  1867. dest->count = src->count;
  1868. }
  1869. void ast_party_redirecting_free(struct ast_party_redirecting *doomed)
  1870. {
  1871. ast_party_id_free(&doomed->orig);
  1872. ast_party_id_free(&doomed->from);
  1873. ast_party_id_free(&doomed->to);
  1874. ast_party_id_free(&doomed->priv_orig);
  1875. ast_party_id_free(&doomed->priv_from);
  1876. ast_party_id_free(&doomed->priv_to);
  1877. ast_party_redirecting_reason_free(&doomed->reason);
  1878. ast_party_redirecting_reason_free(&doomed->orig_reason);
  1879. }
  1880. /*! \brief Free a channel structure */
  1881. static void ast_channel_destructor(void *obj)
  1882. {
  1883. struct ast_channel *chan = obj;
  1884. #ifdef HAVE_EPOLL
  1885. int i;
  1886. #endif
  1887. struct ast_var_t *vardata;
  1888. struct ast_frame *f;
  1889. struct varshead *headp;
  1890. struct ast_datastore *datastore;
  1891. char device_name[AST_CHANNEL_NAME];
  1892. struct ast_callid *callid;
  1893. ast_debug(1, "Channel %p '%s' destroying\n", chan, ast_channel_name(chan));
  1894. /* Stop monitoring */
  1895. if (ast_channel_monitor(chan)) {
  1896. ast_channel_monitor(chan)->stop(chan, 0);
  1897. }
  1898. /* If there is native format music-on-hold state, free it */
  1899. if (ast_channel_music_state(chan)) {
  1900. ast_moh_cleanup(chan);
  1901. }
  1902. ast_pbx_hangup_handler_destroy(chan);
  1903. /* Things that may possibly raise Stasis messages shouldn't occur after this point */
  1904. ast_set_flag(ast_channel_flags(chan), AST_FLAG_DEAD);
  1905. if (ast_channel_internal_is_finalized(chan)) {
  1906. /* A channel snapshot should not be in the process of being staged now. */
  1907. ast_assert(!ast_test_flag(ast_channel_flags(chan), AST_FLAG_SNAPSHOT_STAGE));
  1908. ast_channel_lock(chan);
  1909. ast_channel_publish_snapshot(chan);
  1910. ast_channel_unlock(chan);
  1911. publish_cache_clear(chan);
  1912. }
  1913. ast_channel_lock(chan);
  1914. /* Get rid of each of the data stores on the channel */
  1915. while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry)))
  1916. /* Free the data store */
  1917. ast_datastore_free(datastore);
  1918. /* While the channel is locked, take the reference to its callid while we tear down the call. */
  1919. callid = ast_channel_callid(chan);
  1920. ast_channel_callid_cleanup(chan);
  1921. ast_channel_unlock(chan);
  1922. /* Lock and unlock the channel just to be sure nobody has it locked still
  1923. due to a reference that was stored in a datastore. (i.e. app_chanspy) */
  1924. ast_channel_lock(chan);
  1925. ast_channel_unlock(chan);
  1926. if (ast_channel_tech_pvt(chan)) {
  1927. ast_log_callid(LOG_WARNING, callid, "Channel '%s' may not have been hung up properly\n", ast_channel_name(chan));
  1928. ast_free(ast_channel_tech_pvt(chan));
  1929. }
  1930. if (ast_channel_sched(chan)) {
  1931. ast_sched_context_destroy(ast_channel_sched(chan));
  1932. }
  1933. if (ast_channel_internal_is_finalized(chan)) {
  1934. char *dashptr;
  1935. ast_copy_string(device_name, ast_channel_name(chan), sizeof(device_name));
  1936. if ((dashptr = strrchr(device_name, '-'))) {
  1937. *dashptr = '\0';
  1938. }
  1939. } else {
  1940. device_name[0] = '\0';
  1941. }
  1942. /* Free translators */
  1943. if (ast_channel_readtrans(chan))
  1944. ast_translator_free_path(ast_channel_readtrans(chan));
  1945. if (ast_channel_writetrans(chan))
  1946. ast_translator_free_path(ast_channel_writetrans(chan));
  1947. if (ast_channel_pbx(chan))
  1948. ast_log_callid(LOG_WARNING, callid, "PBX may not have been terminated properly on '%s'\n", ast_channel_name(chan));
  1949. /* Free formats */
  1950. ast_channel_set_oldwriteformat(chan, NULL);
  1951. ast_channel_set_rawreadformat(chan, NULL);
  1952. ast_channel_set_rawwriteformat(chan, NULL);
  1953. ast_channel_set_readformat(chan, NULL);
  1954. ast_channel_set_writeformat(chan, NULL);
  1955. ast_party_dialed_free(ast_channel_dialed(chan));
  1956. ast_party_caller_free(ast_channel_caller(chan));
  1957. ast_party_connected_line_free(ast_channel_connected(chan));
  1958. ast_party_connected_line_free(ast_channel_connected_indicated(chan));
  1959. ast_party_redirecting_free(ast_channel_redirecting(chan));
  1960. /* Close pipes if appropriate */
  1961. ast_channel_internal_alertpipe_close(chan);
  1962. if (ast_channel_timer(chan)) {
  1963. ast_timer_close(ast_channel_timer(chan));
  1964. ast_channel_timer_set(chan, NULL);
  1965. }
  1966. #ifdef HAVE_EPOLL
  1967. for (i = 0; i < AST_MAX_FDS; i++) {
  1968. if (ast_channel_internal_epfd_data(chan, i)) {
  1969. ast_free(ast_channel_internal_epfd_data(chan, i));
  1970. }
  1971. }
  1972. close(ast_channel_epfd(chan));
  1973. #endif
  1974. while ((f = AST_LIST_REMOVE_HEAD(ast_channel_readq(chan), frame_list)))
  1975. ast_frfree(f);
  1976. /* loop over the variables list, freeing all data and deleting list items */
  1977. /* no need to lock the list, as the channel is already locked */
  1978. headp = ast_channel_varshead(chan);
  1979. while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
  1980. ast_var_delete(vardata);
  1981. ast_app_group_discard(chan);
  1982. /* Destroy the jitterbuffer */
  1983. ast_jb_destroy(chan);
  1984. if (ast_channel_cdr(chan)) {
  1985. ast_cdr_free(ast_channel_cdr(chan));
  1986. ast_channel_cdr_set(chan, NULL);
  1987. }
  1988. if (ast_channel_zone(chan)) {
  1989. ast_channel_zone_set(chan, ast_tone_zone_unref(ast_channel_zone(chan)));
  1990. }
  1991. ast_channel_internal_cleanup(chan);
  1992. if (device_name[0]) {
  1993. /*
  1994. * We have a device name to notify of a new state.
  1995. *
  1996. * Queue an unknown state, because, while we know that this particular
  1997. * instance is dead, we don't know the state of all other possible
  1998. * instances.
  1999. */
  2000. ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE) ? AST_DEVSTATE_NOT_CACHABLE : AST_DEVSTATE_CACHABLE), device_name);
  2001. }
  2002. ast_channel_nativeformats_set(chan, NULL);
  2003. if (callid) {
  2004. ast_callid_unref(callid);
  2005. }
  2006. ast_channel_named_callgroups_set(chan, NULL);
  2007. ast_channel_named_pickupgroups_set(chan, NULL);
  2008. ast_atomic_fetchadd_int(&chancount, -1);
  2009. }
  2010. /*! \brief Free a dummy channel structure */
  2011. static void ast_dummy_channel_destructor(void *obj)
  2012. {
  2013. struct ast_channel *chan = obj;
  2014. struct ast_datastore *datastore;
  2015. struct ast_var_t *vardata;
  2016. struct varshead *headp;
  2017. ast_pbx_hangup_handler_destroy(chan);
  2018. /* Get rid of each of the data stores on the channel */
  2019. while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry))) {
  2020. /* Free the data store */
  2021. ast_datastore_free(datastore);
  2022. }
  2023. ast_party_dialed_free(ast_channel_dialed(chan));
  2024. ast_party_caller_free(ast_channel_caller(chan));
  2025. ast_party_connected_line_free(ast_channel_connected(chan));
  2026. ast_party_connected_line_free(ast_channel_connected_indicated(chan));
  2027. ast_party_redirecting_free(ast_channel_redirecting(chan));
  2028. /* loop over the variables list, freeing all data and deleting list items */
  2029. /* no need to lock the list, as the channel is already locked */
  2030. headp = ast_channel_varshead(chan);
  2031. while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
  2032. ast_var_delete(vardata);
  2033. if (ast_channel_cdr(chan)) {
  2034. ast_cdr_free(ast_channel_cdr(chan));
  2035. ast_channel_cdr_set(chan, NULL);
  2036. }
  2037. ast_channel_internal_cleanup(chan);
  2038. }
  2039. struct ast_datastore *ast_channel_datastore_alloc(const struct ast_datastore_info *info, const char *uid)
  2040. {
  2041. return ast_datastore_alloc(info, uid);
  2042. }
  2043. int ast_channel_datastore_free(struct ast_datastore *datastore)
  2044. {
  2045. return ast_datastore_free(datastore);
  2046. }
  2047. int ast_channel_datastore_inherit(struct ast_channel *from, struct ast_channel *to)
  2048. {
  2049. struct ast_datastore *datastore = NULL, *datastore2;
  2050. AST_LIST_TRAVERSE(ast_channel_datastores(from), datastore, entry) {
  2051. if (datastore->inheritance > 0) {
  2052. datastore2 = ast_datastore_alloc(datastore->info, datastore->uid);
  2053. if (datastore2) {
  2054. datastore2->data = datastore->info->duplicate ? datastore->info->duplicate(datastore->data) : NULL;
  2055. datastore2->inheritance = datastore->inheritance == DATASTORE_INHERIT_FOREVER ? DATASTORE_INHERIT_FOREVER : datastore->inheritance - 1;
  2056. AST_LIST_INSERT_TAIL(ast_channel_datastores(to), datastore2, entry);
  2057. }
  2058. }
  2059. }
  2060. return 0;
  2061. }
  2062. int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
  2063. {
  2064. int res = 0;
  2065. AST_LIST_INSERT_HEAD(ast_channel_datastores(chan), datastore, entry);
  2066. return res;
  2067. }
  2068. int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
  2069. {
  2070. return AST_LIST_REMOVE(ast_channel_datastores(chan), datastore, entry) ? 0 : -1;
  2071. }
  2072. struct ast_datastore *ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
  2073. {
  2074. struct ast_datastore *datastore = NULL;
  2075. if (info == NULL)
  2076. return NULL;
  2077. AST_LIST_TRAVERSE(ast_channel_datastores(chan), datastore, entry) {
  2078. if (datastore->info != info) {
  2079. continue;
  2080. }
  2081. if (uid == NULL) {
  2082. /* matched by type only */
  2083. break;
  2084. }
  2085. if ((datastore->uid != NULL) && !strcasecmp(uid, datastore->uid)) {
  2086. /* Matched by type AND uid */
  2087. break;
  2088. }
  2089. }
  2090. return datastore;
  2091. }
  2092. /*! Set the file descriptor on the channel */
  2093. void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
  2094. {
  2095. #ifdef HAVE_EPOLL
  2096. struct epoll_event ev;
  2097. struct ast_epoll_data *aed = NULL;
  2098. if (ast_channel_fd_isset(chan, which)) {
  2099. epoll_ctl(ast_channel_epfd(chan), EPOLL_CTL_DEL, ast_channel_fd(chan, which), &ev);
  2100. aed = ast_channel_internal_epfd_data(chan, which);
  2101. }
  2102. /* If this new fd is valid, add it to the epoll */
  2103. if (fd > -1) {
  2104. if (!aed && (!(aed = ast_calloc(1, sizeof(*aed)))))
  2105. return;
  2106. ast_channel_internal_epfd_data_set(chan, which, aed);
  2107. aed->chan = chan;
  2108. aed->which = which;
  2109. ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP;
  2110. ev.data.ptr = aed;
  2111. epoll_ctl(ast_channel_epfd(chan), EPOLL_CTL_ADD, fd, &ev);
  2112. } else if (aed) {
  2113. /* We don't have to keep around this epoll data structure now */
  2114. ast_free(aed);
  2115. ast_channel_epfd_data_set(chan, which, NULL);
  2116. }
  2117. #endif
  2118. ast_channel_internal_fd_set(chan, which, fd);
  2119. return;
  2120. }
  2121. /*! Add a channel to an optimized waitfor */
  2122. void ast_poll_channel_add(struct ast_channel *chan0, struct ast_channel *chan1)
  2123. {
  2124. #ifdef HAVE_EPOLL
  2125. struct epoll_event ev;
  2126. int i = 0;
  2127. if (ast_channel_epfd(chan0) == -1)
  2128. return;
  2129. /* Iterate through the file descriptors on chan1, adding them to chan0 */
  2130. for (i = 0; i < AST_MAX_FDS; i++) {
  2131. if (!ast_channel_fd_isset(chan1, i)) {
  2132. continue;
  2133. }
  2134. ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP;
  2135. ev.data.ptr = ast_channel_internal_epfd_data(chan1, i);
  2136. epoll_ctl(ast_channel_epfd(chan0), EPOLL_CTL_ADD, ast_channel_fd(chan1, i), &ev);
  2137. }
  2138. #endif
  2139. return;
  2140. }
  2141. /*! Delete a channel from an optimized waitfor */
  2142. void ast_poll_channel_del(struct ast_channel *chan0, struct ast_channel *chan1)
  2143. {
  2144. #ifdef HAVE_EPOLL
  2145. struct epoll_event ev;
  2146. int i = 0;
  2147. if (ast_channel_epfd(chan0) == -1)
  2148. return;
  2149. for (i = 0; i < AST_MAX_FDS; i++) {
  2150. if (!ast_channel_fd_isset(chan1, i)) {
  2151. continue;
  2152. }
  2153. epoll_ctl(ast_channel_epfd(chan0), EPOLL_CTL_DEL, ast_channel_fd(chan1, i), &ev);
  2154. }
  2155. #endif
  2156. return;
  2157. }
  2158. void ast_channel_clear_softhangup(struct ast_channel *chan, int flag)
  2159. {
  2160. ast_channel_lock(chan);
  2161. ast_channel_softhangup_internal_flag_clear(chan, flag);
  2162. if (!ast_channel_softhangup_internal_flag(chan)) {
  2163. struct ast_frame *fr;
  2164. /* If we have completely cleared the softhangup flag,
  2165. * then we need to fully abort the hangup process. This requires
  2166. * pulling the END_OF_Q frame out of the channel frame queue if it
  2167. * still happens to be there. */
  2168. fr = AST_LIST_LAST(ast_channel_readq(chan));
  2169. if (fr && fr->frametype == AST_FRAME_CONTROL &&
  2170. fr->subclass.integer == AST_CONTROL_END_OF_Q) {
  2171. AST_LIST_REMOVE(ast_channel_readq(chan), fr, frame_list);
  2172. ast_frfree(fr);
  2173. }
  2174. }
  2175. ast_channel_unlock(chan);
  2176. }
  2177. /*! \brief Softly hangup a channel, don't lock */
  2178. int ast_softhangup_nolock(struct ast_channel *chan, int cause)
  2179. {
  2180. ast_debug(1, "Soft-Hanging (%#04x) up channel '%s'\n", (unsigned)cause, ast_channel_name(chan));
  2181. /* Inform channel driver that we need to be hung up, if it cares */
  2182. ast_channel_softhangup_internal_flag_add(chan, cause);
  2183. ast_queue_frame(chan, &ast_null_frame);
  2184. /* Interrupt any poll call or such */
  2185. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING))
  2186. pthread_kill(ast_channel_blocker(chan), SIGURG);
  2187. return 0;
  2188. }
  2189. /*! \brief Softly hangup a channel, lock */
  2190. int ast_softhangup(struct ast_channel *chan, int cause)
  2191. {
  2192. RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
  2193. int res;
  2194. ast_channel_lock(chan);
  2195. res = ast_softhangup_nolock(chan, cause);
  2196. blob = ast_json_pack("{s: i, s: b}",
  2197. "cause", cause,
  2198. "soft", 1);
  2199. ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), blob);
  2200. ast_channel_unlock(chan);
  2201. return res;
  2202. }
  2203. static void free_translation(struct ast_channel *clonechan)
  2204. {
  2205. if (ast_channel_writetrans(clonechan)) {
  2206. ast_translator_free_path(ast_channel_writetrans(clonechan));
  2207. }
  2208. if (ast_channel_readtrans(clonechan)) {
  2209. ast_translator_free_path(ast_channel_readtrans(clonechan));
  2210. }
  2211. ast_channel_writetrans_set(clonechan, NULL);
  2212. ast_channel_readtrans_set(clonechan, NULL);
  2213. }
  2214. void ast_set_hangupsource(struct ast_channel *chan, const char *source, int force)
  2215. {
  2216. RAII_VAR(struct ast_channel *, bridge, ast_channel_bridge_peer(chan), ast_channel_cleanup);
  2217. ast_channel_lock(chan);
  2218. if (force || ast_strlen_zero(ast_channel_hangupsource(chan))) {
  2219. ast_channel_hangupsource_set(chan, source);
  2220. }
  2221. ast_channel_unlock(chan);
  2222. if (bridge) {
  2223. ast_channel_lock(bridge);
  2224. if (force || ast_strlen_zero(ast_channel_hangupsource(bridge))) {
  2225. ast_channel_hangupsource_set(bridge, source);
  2226. }
  2227. ast_channel_unlock(bridge);
  2228. }
  2229. }
  2230. int ast_channel_has_audio_frame_or_monitor(struct ast_channel *chan)
  2231. {
  2232. return ast_channel_monitor(chan)
  2233. || !ast_audiohook_write_list_empty(ast_channel_audiohooks(chan))
  2234. || !ast_framehook_list_contains_no_active(ast_channel_framehooks(chan));
  2235. }
  2236. int ast_channel_has_hook_requiring_audio(struct ast_channel *chan)
  2237. {
  2238. return ast_channel_monitor(chan)
  2239. || !ast_audiohook_write_list_empty(ast_channel_audiohooks(chan))
  2240. || !ast_framehook_list_contains_no_active_of_type(ast_channel_framehooks(chan), AST_FRAME_VOICE);
  2241. }
  2242. static void destroy_hooks(struct ast_channel *chan)
  2243. {
  2244. if (ast_channel_audiohooks(chan)) {
  2245. ast_audiohook_detach_list(ast_channel_audiohooks(chan));
  2246. ast_channel_audiohooks_set(chan, NULL);
  2247. }
  2248. ast_framehook_list_destroy(chan);
  2249. }
  2250. /*! \brief Hangup a channel */
  2251. void ast_hangup(struct ast_channel *chan)
  2252. {
  2253. /* Be NULL safe for RAII_VAR() usage. */
  2254. if (!chan) {
  2255. return;
  2256. }
  2257. ast_debug(1, "Channel %p '%s' hanging up. Refs: %d\n", chan, ast_channel_name(chan),
  2258. ao2_ref(chan, 0));
  2259. ast_autoservice_stop(chan);
  2260. ast_channel_lock(chan);
  2261. while (ast_channel_masq(chan) || ast_channel_masqr(chan)) {
  2262. CHANNEL_DEADLOCK_AVOIDANCE(chan);
  2263. }
  2264. /* Mark as a zombie so a masquerade cannot be setup on this channel. */
  2265. ast_set_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE);
  2266. ast_channel_unlock(chan);
  2267. /*
  2268. * XXX if running the hangup handlers here causes problems
  2269. * because the handlers take too long to execute, we could move
  2270. * the meat of this function into another thread. A thread
  2271. * where channels go to die.
  2272. *
  2273. * If this is done, ast_autoservice_chan_hangup_peer() will no
  2274. * longer be needed.
  2275. */
  2276. ast_pbx_hangup_handler_run(chan);
  2277. ao2_unlink(channels, chan);
  2278. ast_channel_lock(chan);
  2279. destroy_hooks(chan);
  2280. free_translation(chan);
  2281. /* Close audio stream */
  2282. if (ast_channel_stream(chan)) {
  2283. ast_closestream(ast_channel_stream(chan));
  2284. ast_channel_stream_set(chan, NULL);
  2285. }
  2286. /* Close video stream */
  2287. if (ast_channel_vstream(chan)) {
  2288. ast_closestream(ast_channel_vstream(chan));
  2289. ast_channel_vstream_set(chan, NULL);
  2290. }
  2291. if (ast_channel_sched(chan)) {
  2292. ast_sched_context_destroy(ast_channel_sched(chan));
  2293. ast_channel_sched_set(chan, NULL);
  2294. }
  2295. if (ast_channel_generatordata(chan)) { /* Clear any tone stuff remaining */
  2296. if (ast_channel_generator(chan) && ast_channel_generator(chan)->release) {
  2297. ast_channel_generator(chan)->release(chan, ast_channel_generatordata(chan));
  2298. }
  2299. }
  2300. ast_channel_generatordata_set(chan, NULL);
  2301. ast_channel_generator_set(chan, NULL);
  2302. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING)) {
  2303. ast_log(LOG_WARNING, "Hard hangup called by thread LWP %d on %s, while blocked by thread LWP %d in procedure %s! Expect a failure\n",
  2304. ast_get_tid(), ast_channel_name(chan), ast_channel_blocker_tid(chan),
  2305. ast_channel_blockproc(chan));
  2306. ast_assert(0);
  2307. }
  2308. if (ast_channel_tech(chan)->hangup) {
  2309. ast_channel_tech(chan)->hangup(chan);
  2310. }
  2311. ast_channel_unlock(chan);
  2312. ast_cc_offer(chan);
  2313. ast_channel_unref(chan);
  2314. }
  2315. /*!
  2316. * \internal
  2317. * \brief Set channel answered time if not already set.
  2318. * \since 13.11.0
  2319. *
  2320. * \param chan Channel to set answered time.
  2321. *
  2322. * \return Nothing
  2323. */
  2324. static void set_channel_answer_time(struct ast_channel *chan)
  2325. {
  2326. if (ast_tvzero(ast_channel_answertime(chan))) {
  2327. struct timeval answertime;
  2328. answertime = ast_tvnow();
  2329. ast_channel_answertime_set(chan, &answertime);
  2330. }
  2331. }
  2332. int ast_raw_answer(struct ast_channel *chan)
  2333. {
  2334. int res = 0;
  2335. ast_channel_lock(chan);
  2336. /* You can't answer an outbound call */
  2337. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING)) {
  2338. ast_channel_unlock(chan);
  2339. return 0;
  2340. }
  2341. /* Stop if we're a zombie or need a soft hangup */
  2342. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
  2343. ast_channel_unlock(chan);
  2344. return -1;
  2345. }
  2346. /*
  2347. * Mark when incoming channel answered so we can know how
  2348. * long the channel has been up.
  2349. */
  2350. set_channel_answer_time(chan);
  2351. ast_channel_unlock(chan);
  2352. switch (ast_channel_state(chan)) {
  2353. case AST_STATE_RINGING:
  2354. case AST_STATE_RING:
  2355. ast_channel_lock(chan);
  2356. if (ast_channel_tech(chan)->answer) {
  2357. res = ast_channel_tech(chan)->answer(chan);
  2358. }
  2359. ast_setstate(chan, AST_STATE_UP);
  2360. ast_channel_unlock(chan);
  2361. break;
  2362. case AST_STATE_UP:
  2363. break;
  2364. default:
  2365. break;
  2366. }
  2367. ast_indicate(chan, -1);
  2368. return res;
  2369. }
  2370. int __ast_answer(struct ast_channel *chan, unsigned int delay)
  2371. {
  2372. int res = 0;
  2373. enum ast_channel_state old_state;
  2374. old_state = ast_channel_state(chan);
  2375. if ((res = ast_raw_answer(chan))) {
  2376. return res;
  2377. }
  2378. switch (old_state) {
  2379. case AST_STATE_RINGING:
  2380. case AST_STATE_RING:
  2381. /* wait for media to start flowing, but don't wait any longer
  2382. * than 'delay' or 500 milliseconds, whichever is longer
  2383. */
  2384. do {
  2385. AST_LIST_HEAD_NOLOCK(, ast_frame) frames;
  2386. struct ast_frame *cur;
  2387. struct ast_frame *new_frame;
  2388. int timeout_ms = MAX(delay, 500);
  2389. unsigned int done = 0;
  2390. struct timeval start;
  2391. AST_LIST_HEAD_INIT_NOLOCK(&frames);
  2392. start = ast_tvnow();
  2393. for (;;) {
  2394. int ms = ast_remaining_ms(start, timeout_ms);
  2395. ms = ast_waitfor(chan, ms);
  2396. if (ms < 0) {
  2397. ast_log(LOG_WARNING, "Error condition occurred when polling channel %s for a voice frame: %s\n", ast_channel_name(chan), strerror(errno));
  2398. res = -1;
  2399. break;
  2400. }
  2401. if (ms == 0) {
  2402. ast_debug(2, "Didn't receive a media frame from %s within %u ms of answering. Continuing anyway\n", ast_channel_name(chan), MAX(delay, 500));
  2403. break;
  2404. }
  2405. cur = ast_read(chan);
  2406. if (!cur || ((cur->frametype == AST_FRAME_CONTROL) &&
  2407. (cur->subclass.integer == AST_CONTROL_HANGUP))) {
  2408. if (cur) {
  2409. ast_frfree(cur);
  2410. }
  2411. res = -1;
  2412. ast_debug(2, "Hangup of channel %s detected in answer routine\n", ast_channel_name(chan));
  2413. break;
  2414. }
  2415. if ((new_frame = ast_frisolate(cur)) != cur) {
  2416. ast_frfree(cur);
  2417. }
  2418. AST_LIST_INSERT_HEAD(&frames, new_frame, frame_list);
  2419. /* if a specific delay period was requested, continue
  2420. * until that delay has passed. don't stop just because
  2421. * incoming media has arrived.
  2422. */
  2423. if (delay) {
  2424. continue;
  2425. }
  2426. switch (new_frame->frametype) {
  2427. /* all of these frametypes qualify as 'media' */
  2428. case AST_FRAME_VOICE:
  2429. case AST_FRAME_VIDEO:
  2430. case AST_FRAME_TEXT:
  2431. case AST_FRAME_TEXT_DATA:
  2432. case AST_FRAME_DTMF_BEGIN:
  2433. case AST_FRAME_DTMF_END:
  2434. case AST_FRAME_IMAGE:
  2435. case AST_FRAME_HTML:
  2436. case AST_FRAME_MODEM:
  2437. case AST_FRAME_RTCP:
  2438. done = 1;
  2439. break;
  2440. case AST_FRAME_CONTROL:
  2441. case AST_FRAME_IAX:
  2442. case AST_FRAME_BRIDGE_ACTION:
  2443. case AST_FRAME_BRIDGE_ACTION_SYNC:
  2444. case AST_FRAME_NULL:
  2445. case AST_FRAME_CNG:
  2446. break;
  2447. }
  2448. if (done) {
  2449. break;
  2450. }
  2451. }
  2452. ast_channel_lock(chan);
  2453. while ((cur = AST_LIST_REMOVE_HEAD(&frames, frame_list))) {
  2454. if (res == 0) {
  2455. ast_queue_frame_head(chan, cur);
  2456. }
  2457. ast_frfree(cur);
  2458. }
  2459. ast_channel_unlock(chan);
  2460. } while (0);
  2461. break;
  2462. default:
  2463. break;
  2464. }
  2465. return res;
  2466. }
  2467. int ast_answer(struct ast_channel *chan)
  2468. {
  2469. return __ast_answer(chan, 0);
  2470. }
  2471. inline int ast_auto_answer(struct ast_channel *chan)
  2472. {
  2473. if (ast_channel_state(chan) == AST_STATE_UP) {
  2474. /* Already answered */
  2475. return 0;
  2476. }
  2477. return ast_answer(chan);
  2478. }
  2479. int ast_channel_get_duration(struct ast_channel *chan)
  2480. {
  2481. ast_assert(NULL != chan);
  2482. if (ast_tvzero(ast_channel_creationtime(chan))) {
  2483. return 0;
  2484. }
  2485. return (ast_tvdiff_ms(ast_tvnow(), ast_channel_creationtime(chan)) / 1000);
  2486. }
  2487. int ast_channel_get_up_time(struct ast_channel *chan)
  2488. {
  2489. ast_assert(NULL != chan);
  2490. if (ast_tvzero(ast_channel_answertime(chan))) {
  2491. return 0;
  2492. }
  2493. return (ast_tvdiff_ms(ast_tvnow(), ast_channel_answertime(chan)) / 1000);
  2494. }
  2495. static void deactivate_generator_nolock(struct ast_channel *chan)
  2496. {
  2497. if (ast_channel_generatordata(chan)) {
  2498. struct ast_generator *generator = ast_channel_generator(chan);
  2499. if (generator && generator->release) {
  2500. generator->release(chan, ast_channel_generatordata(chan));
  2501. }
  2502. ast_channel_generatordata_set(chan, NULL);
  2503. ast_channel_generator_set(chan, NULL);
  2504. ast_channel_set_fd(chan, AST_GENERATOR_FD, -1);
  2505. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT);
  2506. ast_settimeout(chan, 0, NULL, NULL);
  2507. }
  2508. }
  2509. void ast_deactivate_generator(struct ast_channel *chan)
  2510. {
  2511. ast_channel_lock(chan);
  2512. deactivate_generator_nolock(chan);
  2513. ast_channel_unlock(chan);
  2514. }
  2515. static void generator_write_format_change(struct ast_channel *chan)
  2516. {
  2517. struct ast_generator *generator;
  2518. ast_channel_lock(chan);
  2519. generator = ast_channel_generator(chan);
  2520. if (generator && generator->write_format_change) {
  2521. generator->write_format_change(chan, ast_channel_generatordata(chan));
  2522. }
  2523. ast_channel_unlock(chan);
  2524. }
  2525. static int generator_force(const void *data)
  2526. {
  2527. /* Called if generator doesn't have data */
  2528. void *tmp;
  2529. int res;
  2530. int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples) = NULL;
  2531. struct ast_channel *chan = (struct ast_channel *)data;
  2532. ast_channel_lock(chan);
  2533. tmp = ast_channel_generatordata(chan);
  2534. ast_channel_generatordata_set(chan, NULL);
  2535. if (ast_channel_generator(chan))
  2536. generate = ast_channel_generator(chan)->generate;
  2537. ast_channel_unlock(chan);
  2538. if (!tmp || !generate) {
  2539. return 0;
  2540. }
  2541. res = generate(chan, tmp, 0, ast_format_get_sample_rate(ast_channel_writeformat(chan)) / 50);
  2542. ast_channel_lock(chan);
  2543. if (ast_channel_generator(chan) && generate == ast_channel_generator(chan)->generate) {
  2544. ast_channel_generatordata_set(chan, tmp);
  2545. }
  2546. ast_channel_unlock(chan);
  2547. if (res) {
  2548. ast_debug(1, "Auto-deactivating generator\n");
  2549. ast_deactivate_generator(chan);
  2550. }
  2551. return 0;
  2552. }
  2553. int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
  2554. {
  2555. int res = 0;
  2556. void *generatordata = NULL;
  2557. ast_channel_lock(chan);
  2558. if (ast_channel_generatordata(chan)) {
  2559. struct ast_generator *generator_old = ast_channel_generator(chan);
  2560. if (generator_old && generator_old->release) {
  2561. generator_old->release(chan, ast_channel_generatordata(chan));
  2562. }
  2563. }
  2564. if (gen->alloc && !(generatordata = gen->alloc(chan, params))) {
  2565. res = -1;
  2566. }
  2567. ast_channel_generatordata_set(chan, generatordata);
  2568. if (!res) {
  2569. ast_settimeout(chan, 50, generator_force, chan);
  2570. ast_channel_generator_set(chan, gen);
  2571. }
  2572. ast_channel_unlock(chan);
  2573. ast_prod(chan);
  2574. return res;
  2575. }
  2576. /*! \brief Wait for x amount of time on a file descriptor to have input. */
  2577. int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
  2578. {
  2579. int winner = -1;
  2580. ast_waitfor_nandfds(NULL, 0, fds, n, exception, &winner, ms);
  2581. return winner;
  2582. }
  2583. /*! \brief Wait for x amount of time on a file descriptor to have input. */
  2584. #ifdef HAVE_EPOLL
  2585. static struct ast_channel *ast_waitfor_nandfds_classic(struct ast_channel **c, int n, int *fds, int nfds,
  2586. int *exception, int *outfd, int *ms)
  2587. #else
  2588. struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
  2589. int *exception, int *outfd, int *ms)
  2590. #endif
  2591. {
  2592. struct timeval start = { 0 , 0 };
  2593. struct pollfd *pfds = NULL;
  2594. int res;
  2595. long rms;
  2596. int x, y, max;
  2597. int sz;
  2598. struct timeval now = { 0, 0 };
  2599. struct timeval whentohangup = { 0, 0 }, diff;
  2600. struct ast_channel *winner = NULL;
  2601. struct fdmap {
  2602. int chan;
  2603. int fdno;
  2604. } *fdmap = NULL;
  2605. if (outfd) {
  2606. *outfd = -99999;
  2607. }
  2608. if (exception) {
  2609. *exception = 0;
  2610. }
  2611. if ((sz = n * AST_MAX_FDS + nfds)) {
  2612. pfds = ast_alloca(sizeof(*pfds) * sz);
  2613. fdmap = ast_alloca(sizeof(*fdmap) * sz);
  2614. } else {
  2615. /* nothing to allocate and no FDs to check */
  2616. return NULL;
  2617. }
  2618. for (x = 0; x < n; x++) {
  2619. ast_channel_lock(c[x]);
  2620. if (!ast_tvzero(*ast_channel_whentohangup(c[x]))) {
  2621. if (ast_tvzero(whentohangup))
  2622. now = ast_tvnow();
  2623. diff = ast_tvsub(*ast_channel_whentohangup(c[x]), now);
  2624. if (diff.tv_sec < 0 || ast_tvzero(diff)) {
  2625. ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
  2626. /* Should already be hungup */
  2627. ast_channel_softhangup_internal_flag_add(c[x], AST_SOFTHANGUP_TIMEOUT);
  2628. ast_channel_unlock(c[x]);
  2629. return c[x];
  2630. }
  2631. if (ast_tvzero(whentohangup) || ast_tvcmp(diff, whentohangup) < 0)
  2632. whentohangup = diff;
  2633. }
  2634. ast_channel_unlock(c[x]);
  2635. }
  2636. /* Wait full interval */
  2637. rms = *ms;
  2638. /* INT_MAX, not LONG_MAX, because it matters on 64-bit */
  2639. if (!ast_tvzero(whentohangup) && whentohangup.tv_sec < INT_MAX / 1000) {
  2640. rms = whentohangup.tv_sec * 1000 + whentohangup.tv_usec / 1000; /* timeout in milliseconds */
  2641. if (*ms >= 0 && *ms < rms) { /* original *ms still smaller */
  2642. rms = *ms;
  2643. }
  2644. } else if (!ast_tvzero(whentohangup) && rms < 0) {
  2645. /* Tiny corner case... call would need to last >24 days */
  2646. rms = INT_MAX;
  2647. }
  2648. /*
  2649. * Build the pollfd array, putting the channels' fds first,
  2650. * followed by individual fds. Order is important because
  2651. * individual fd's must have priority over channel fds.
  2652. */
  2653. max = 0;
  2654. for (x = 0; x < n; x++) {
  2655. for (y = 0; y < AST_MAX_FDS; y++) {
  2656. fdmap[max].fdno = y; /* fd y is linked to this pfds */
  2657. fdmap[max].chan = x; /* channel x is linked to this pfds */
  2658. max += ast_add_fd(&pfds[max], ast_channel_fd(c[x], y));
  2659. }
  2660. ast_channel_lock(c[x]);
  2661. CHECK_BLOCKING(c[x]);
  2662. ast_channel_unlock(c[x]);
  2663. }
  2664. /* Add the individual fds */
  2665. for (x = 0; x < nfds; x++) {
  2666. fdmap[max].chan = -1;
  2667. max += ast_add_fd(&pfds[max], fds[x]);
  2668. }
  2669. if (*ms > 0) {
  2670. start = ast_tvnow();
  2671. }
  2672. if (sizeof(int) == 4) { /* XXX fix timeout > 600000 on linux x86-32 */
  2673. do {
  2674. int kbrms = rms;
  2675. if (kbrms > 600000) {
  2676. kbrms = 600000;
  2677. }
  2678. res = ast_poll(pfds, max, kbrms);
  2679. if (!res) {
  2680. rms -= kbrms;
  2681. }
  2682. } while (!res && (rms > 0));
  2683. } else {
  2684. res = ast_poll(pfds, max, rms);
  2685. }
  2686. for (x = 0; x < n; x++) {
  2687. ast_channel_lock(c[x]);
  2688. ast_clear_flag(ast_channel_flags(c[x]), AST_FLAG_BLOCKING);
  2689. ast_channel_unlock(c[x]);
  2690. }
  2691. if (res < 0) { /* Simulate a timeout if we were interrupted */
  2692. if (errno != EINTR) {
  2693. *ms = -1;
  2694. }
  2695. return NULL;
  2696. }
  2697. if (!ast_tvzero(whentohangup)) { /* if we have a timeout, check who expired */
  2698. now = ast_tvnow();
  2699. for (x = 0; x < n; x++) {
  2700. if (!ast_tvzero(*ast_channel_whentohangup(c[x])) && ast_tvcmp(*ast_channel_whentohangup(c[x]), now) <= 0) {
  2701. ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
  2702. ast_channel_softhangup_internal_flag_add(c[x], AST_SOFTHANGUP_TIMEOUT);
  2703. if (winner == NULL) {
  2704. winner = c[x];
  2705. }
  2706. }
  2707. }
  2708. }
  2709. if (res == 0) { /* no fd ready, reset timeout and done */
  2710. *ms = 0; /* XXX use 0 since we may not have an exact timeout. */
  2711. return winner;
  2712. }
  2713. /*
  2714. * Then check if any channel or fd has a pending event.
  2715. * Remember to check channels first and fds last, as they
  2716. * must have priority on setting 'winner'
  2717. */
  2718. for (x = 0; x < max; x++) {
  2719. res = pfds[x].revents;
  2720. if (res == 0) {
  2721. continue;
  2722. }
  2723. if (fdmap[x].chan >= 0) { /* this is a channel */
  2724. winner = c[fdmap[x].chan]; /* override previous winners */
  2725. ast_channel_lock(winner);
  2726. if (res & POLLPRI) {
  2727. ast_set_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
  2728. } else {
  2729. ast_clear_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
  2730. }
  2731. ast_channel_fdno_set(winner, fdmap[x].fdno);
  2732. ast_channel_unlock(winner);
  2733. } else { /* this is an fd */
  2734. if (outfd) {
  2735. *outfd = pfds[x].fd;
  2736. }
  2737. if (exception) {
  2738. *exception = (res & POLLPRI) ? -1 : 0;
  2739. }
  2740. winner = NULL;
  2741. }
  2742. }
  2743. if (*ms > 0) {
  2744. *ms -= ast_tvdiff_ms(ast_tvnow(), start);
  2745. if (*ms < 0) {
  2746. *ms = 0;
  2747. }
  2748. }
  2749. return winner;
  2750. }
  2751. #ifdef HAVE_EPOLL
  2752. static struct ast_channel *ast_waitfor_nandfds_simple(struct ast_channel *chan, int *ms)
  2753. {
  2754. struct timeval start = { 0 , 0 };
  2755. int res = 0;
  2756. struct epoll_event ev[1];
  2757. long diff, rms = *ms;
  2758. struct ast_channel *winner = NULL;
  2759. struct ast_epoll_data *aed = NULL;
  2760. ast_channel_lock(chan);
  2761. /* Figure out their timeout */
  2762. if (!ast_tvzero(*ast_channel_whentohangup(chan))) {
  2763. if ((diff = ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow())) < 0) {
  2764. /* They should already be hungup! */
  2765. ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT);
  2766. ast_channel_unlock(chan);
  2767. return NULL;
  2768. }
  2769. /* If this value is smaller then the current one... make it priority */
  2770. if (rms > diff) {
  2771. rms = diff;
  2772. }
  2773. }
  2774. /* Time to make this channel block... */
  2775. CHECK_BLOCKING(chan);
  2776. ast_channel_unlock(chan);
  2777. if (*ms > 0) {
  2778. start = ast_tvnow();
  2779. }
  2780. /* We don't have to add any file descriptors... they are already added, we just have to wait! */
  2781. res = epoll_wait(ast_channel_epfd(chan), ev, 1, rms);
  2782. /* Stop blocking */
  2783. ast_channel_lock(chan);
  2784. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
  2785. ast_channel_unlock(chan);
  2786. /* Simulate a timeout if we were interrupted */
  2787. if (res < 0) {
  2788. if (errno != EINTR) {
  2789. *ms = -1;
  2790. }
  2791. return NULL;
  2792. }
  2793. /* If this channel has a timeout see if it expired */
  2794. if (!ast_tvzero(*ast_channel_whentohangup(chan))) {
  2795. if (ast_tvdiff_ms(ast_tvnow(), *ast_channel_whentohangup(chan)) >= 0) {
  2796. ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT);
  2797. winner = chan;
  2798. }
  2799. }
  2800. /* No fd ready, reset timeout and be done for now */
  2801. if (!res) {
  2802. *ms = 0;
  2803. return winner;
  2804. }
  2805. /* See what events are pending */
  2806. aed = ev[0].data.ptr;
  2807. ast_channel_lock(chan);
  2808. ast_channel_fdno_set(chan, aed->which);
  2809. if (ev[0].events & EPOLLPRI) {
  2810. ast_set_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
  2811. } else {
  2812. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
  2813. }
  2814. ast_channel_unlock(chan);
  2815. if (*ms > 0) {
  2816. *ms -= ast_tvdiff_ms(ast_tvnow(), start);
  2817. if (*ms < 0) {
  2818. *ms = 0;
  2819. }
  2820. }
  2821. return chan;
  2822. }
  2823. static struct ast_channel *ast_waitfor_nandfds_complex(struct ast_channel **c, int n, int *ms)
  2824. {
  2825. struct timeval start = { 0 , 0 };
  2826. int res = 0, i;
  2827. struct epoll_event ev[25] = { { 0, } };
  2828. struct timeval now = { 0, 0 };
  2829. long whentohangup = 0, diff = 0, rms = *ms;
  2830. struct ast_channel *winner = NULL;
  2831. for (i = 0; i < n; i++) {
  2832. ast_channel_lock(c[i]);
  2833. if (!ast_tvzero(*ast_channel_whentohangup(c[i]))) {
  2834. if (whentohangup == 0) {
  2835. now = ast_tvnow();
  2836. }
  2837. if ((diff = ast_tvdiff_ms(*ast_channel_whentohangup(c[i]), now)) < 0) {
  2838. ast_channel_softhangup_internal_flag_add(c[i], AST_SOFTHANGUP_TIMEOUT);
  2839. ast_channel_unlock(c[i]);
  2840. return c[i];
  2841. }
  2842. if (!whentohangup || whentohangup > diff) {
  2843. whentohangup = diff;
  2844. }
  2845. }
  2846. CHECK_BLOCKING(c[i]);
  2847. ast_channel_unlock(c[i]);
  2848. }
  2849. rms = *ms;
  2850. if (whentohangup) {
  2851. rms = whentohangup;
  2852. if (*ms >= 0 && *ms < rms) {
  2853. rms = *ms;
  2854. }
  2855. }
  2856. if (*ms > 0) {
  2857. start = ast_tvnow();
  2858. }
  2859. res = epoll_wait(ast_channel_epfd(c[0]), ev, 25, rms);
  2860. for (i = 0; i < n; i++) {
  2861. ast_channel_lock(c[i]);
  2862. ast_clear_flag(ast_channel_flags(c[i]), AST_FLAG_BLOCKING);
  2863. ast_channel_unlock(c[i]);
  2864. }
  2865. if (res < 0) {
  2866. if (errno != EINTR) {
  2867. *ms = -1;
  2868. }
  2869. return NULL;
  2870. }
  2871. if (whentohangup) {
  2872. now = ast_tvnow();
  2873. for (i = 0; i < n; i++) {
  2874. if (!ast_tvzero(*ast_channel_whentohangup(c[i])) && ast_tvdiff_ms(now, *ast_channel_whentohangup(c[i])) >= 0) {
  2875. ast_channel_softhangup_internal_flag_add(c[i], AST_SOFTHANGUP_TIMEOUT);
  2876. if (!winner) {
  2877. winner = c[i];
  2878. }
  2879. }
  2880. }
  2881. }
  2882. if (!res) {
  2883. *ms = 0;
  2884. return winner;
  2885. }
  2886. for (i = 0; i < res; i++) {
  2887. struct ast_epoll_data *aed = ev[i].data.ptr;
  2888. if (!ev[i].events || !aed) {
  2889. continue;
  2890. }
  2891. winner = aed->chan;
  2892. ast_channel_lock(winner);
  2893. if (ev[i].events & EPOLLPRI) {
  2894. ast_set_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
  2895. } else {
  2896. ast_clear_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
  2897. }
  2898. ast_channel_fdno_set(winner, aed->which);
  2899. ast_channel_unlock(winner);
  2900. }
  2901. if (*ms > 0) {
  2902. *ms -= ast_tvdiff_ms(ast_tvnow(), start);
  2903. if (*ms < 0) {
  2904. *ms = 0;
  2905. }
  2906. }
  2907. return winner;
  2908. }
  2909. struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
  2910. int *exception, int *outfd, int *ms)
  2911. {
  2912. /* Clear all provided values in one place. */
  2913. if (outfd) {
  2914. *outfd = -99999;
  2915. }
  2916. if (exception) {
  2917. *exception = 0;
  2918. }
  2919. /* If no epoll file descriptor is available resort to classic nandfds */
  2920. if (!n || nfds || ast_channel_epfd(c[0]) == -1) {
  2921. return ast_waitfor_nandfds_classic(c, n, fds, nfds, exception, outfd, ms);
  2922. } else if (!nfds && n == 1) {
  2923. return ast_waitfor_nandfds_simple(c[0], ms);
  2924. } else {
  2925. return ast_waitfor_nandfds_complex(c, n, ms);
  2926. }
  2927. }
  2928. #endif
  2929. struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms)
  2930. {
  2931. return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms);
  2932. }
  2933. int ast_waitfor(struct ast_channel *c, int ms)
  2934. {
  2935. if (ms < 0) {
  2936. do {
  2937. ms = 100000;
  2938. ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
  2939. } while (!ms);
  2940. } else {
  2941. ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
  2942. }
  2943. return ms;
  2944. }
  2945. int ast_waitfordigit(struct ast_channel *c, int ms)
  2946. {
  2947. return ast_waitfordigit_full(c, ms, -1, -1);
  2948. }
  2949. int ast_settimeout(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data)
  2950. {
  2951. return ast_settimeout_full(c, rate, func, data, 0);
  2952. }
  2953. int ast_settimeout_full(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data, unsigned int is_ao2_obj)
  2954. {
  2955. int res;
  2956. unsigned int real_rate = rate, max_rate;
  2957. ast_channel_lock(c);
  2958. if (ast_channel_timingfd(c) == -1) {
  2959. ast_channel_unlock(c);
  2960. return -1;
  2961. }
  2962. if (!func) {
  2963. rate = 0;
  2964. data = NULL;
  2965. }
  2966. if (rate && rate > (max_rate = ast_timer_get_max_rate(ast_channel_timer(c)))) {
  2967. real_rate = max_rate;
  2968. }
  2969. ast_debug(1, "Scheduling timer at (%u requested / %u actual) timer ticks per second\n", rate, real_rate);
  2970. res = ast_timer_set_rate(ast_channel_timer(c), real_rate);
  2971. if (ast_channel_timingdata(c) && ast_test_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ)) {
  2972. ao2_ref(ast_channel_timingdata(c), -1);
  2973. }
  2974. ast_channel_timingfunc_set(c, func);
  2975. ast_channel_timingdata_set(c, data);
  2976. if (data && is_ao2_obj) {
  2977. ao2_ref(data, 1);
  2978. ast_set_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ);
  2979. } else {
  2980. ast_clear_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ);
  2981. }
  2982. if (func == NULL && rate == 0 && ast_channel_fdno(c) == AST_TIMING_FD) {
  2983. /* Clearing the timing func and setting the rate to 0
  2984. * means that we don't want to be reading from the timingfd
  2985. * any more. Setting c->fdno to -1 means we won't have any
  2986. * errant reads from the timingfd, meaning we won't potentially
  2987. * miss any important frames.
  2988. */
  2989. ast_channel_fdno_set(c, -1);
  2990. }
  2991. ast_channel_unlock(c);
  2992. return res;
  2993. }
  2994. int ast_waitfordigit_full(struct ast_channel *c, int timeout_ms, int audiofd, int cmdfd)
  2995. {
  2996. struct timeval start = ast_tvnow();
  2997. int ms;
  2998. /* Stop if we're a zombie or need a soft hangup */
  2999. if (ast_test_flag(ast_channel_flags(c), AST_FLAG_ZOMBIE) || ast_check_hangup(c))
  3000. return -1;
  3001. /* Only look for the end of DTMF, don't bother with the beginning and don't emulate things */
  3002. ast_channel_set_flag(c, AST_FLAG_END_DTMF_ONLY);
  3003. /* Wait for a digit, no more than timeout_ms milliseconds total.
  3004. * Or, wait indefinitely if timeout_ms is <0.
  3005. */
  3006. while ((ms = ast_remaining_ms(start, timeout_ms))) {
  3007. struct ast_channel *rchan;
  3008. int outfd = -1;
  3009. errno = 0;
  3010. /* While ast_waitfor_nandfds tries to help by reducing the timeout by how much was waited,
  3011. * it is unhelpful if it waited less than a millisecond.
  3012. */
  3013. rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
  3014. if (!rchan && outfd < 0 && ms) {
  3015. if (errno == 0 || errno == EINTR)
  3016. continue;
  3017. ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
  3018. ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
  3019. return -1;
  3020. } else if (outfd > -1) {
  3021. /* The FD we were watching has something waiting */
  3022. ast_log(LOG_WARNING, "The FD we were waiting for has something waiting. Waitfordigit returning numeric 1\n");
  3023. ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
  3024. return 1;
  3025. } else if (rchan) {
  3026. int res;
  3027. struct ast_frame *f = ast_read(c);
  3028. if (!f) {
  3029. ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
  3030. return -1;
  3031. }
  3032. switch (f->frametype) {
  3033. case AST_FRAME_DTMF_BEGIN:
  3034. break;
  3035. case AST_FRAME_DTMF_END:
  3036. res = f->subclass.integer;
  3037. ast_frfree(f);
  3038. ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
  3039. return res;
  3040. case AST_FRAME_CONTROL:
  3041. switch (f->subclass.integer) {
  3042. case AST_CONTROL_HANGUP:
  3043. ast_frfree(f);
  3044. ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
  3045. return -1;
  3046. case AST_CONTROL_STREAM_STOP:
  3047. case AST_CONTROL_STREAM_SUSPEND:
  3048. case AST_CONTROL_STREAM_RESTART:
  3049. case AST_CONTROL_STREAM_REVERSE:
  3050. case AST_CONTROL_STREAM_FORWARD:
  3051. /* Fall-through and treat as if it were a DTMF signal. Items
  3052. * that perform stream control will handle this. */
  3053. res = f->subclass.integer;
  3054. ast_frfree(f);
  3055. ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
  3056. return res;
  3057. case AST_CONTROL_PVT_CAUSE_CODE:
  3058. case AST_CONTROL_RINGING:
  3059. case AST_CONTROL_ANSWER:
  3060. case AST_CONTROL_SRCUPDATE:
  3061. case AST_CONTROL_SRCCHANGE:
  3062. case AST_CONTROL_CONNECTED_LINE:
  3063. case AST_CONTROL_REDIRECTING:
  3064. case AST_CONTROL_UPDATE_RTP_PEER:
  3065. case AST_CONTROL_HOLD:
  3066. case AST_CONTROL_UNHOLD:
  3067. case -1:
  3068. /* Unimportant */
  3069. break;
  3070. default:
  3071. ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", f->subclass.integer);
  3072. break;
  3073. }
  3074. break;
  3075. case AST_FRAME_VOICE:
  3076. /* Write audio if appropriate */
  3077. if (audiofd > -1) {
  3078. if (write(audiofd, f->data.ptr, f->datalen) < 0) {
  3079. ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
  3080. }
  3081. }
  3082. default:
  3083. /* Ignore */
  3084. break;
  3085. }
  3086. ast_frfree(f);
  3087. }
  3088. }
  3089. ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
  3090. return 0; /* Time is up */
  3091. }
  3092. enum DtmfDirection {
  3093. DTMF_RECEIVED,
  3094. DTMF_SENT
  3095. };
  3096. static const char *dtmf_direction_to_string(enum DtmfDirection direction)
  3097. {
  3098. switch (direction) {
  3099. case DTMF_RECEIVED:
  3100. return "Received";
  3101. case DTMF_SENT:
  3102. return "Sent";
  3103. }
  3104. return "?";
  3105. }
  3106. static void send_dtmf_begin_event(struct ast_channel *chan,
  3107. enum DtmfDirection direction, const char digit)
  3108. {
  3109. RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
  3110. char digit_str[] = { digit, '\0' };
  3111. blob = ast_json_pack("{ s: s, s: s }",
  3112. "digit", digit_str,
  3113. "direction", dtmf_direction_to_string(direction));
  3114. if (!blob) {
  3115. return;
  3116. }
  3117. ast_channel_publish_cached_blob(chan, ast_channel_dtmf_begin_type(), blob);
  3118. }
  3119. static void send_dtmf_end_event(struct ast_channel *chan,
  3120. enum DtmfDirection direction, const char digit, long duration_ms)
  3121. {
  3122. RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
  3123. char digit_str[] = { digit, '\0' };
  3124. blob = ast_json_pack("{ s: s, s: s, s: i }",
  3125. "digit", digit_str,
  3126. "direction", dtmf_direction_to_string(direction),
  3127. "duration_ms", duration_ms);
  3128. if (!blob) {
  3129. return;
  3130. }
  3131. ast_channel_publish_cached_blob(chan, ast_channel_dtmf_end_type(), blob);
  3132. }
  3133. static void ast_read_generator_actions(struct ast_channel *chan, struct ast_frame *f)
  3134. {
  3135. struct ast_generator *generator;
  3136. void *gendata;
  3137. int res;
  3138. int samples;
  3139. generator = ast_channel_generator(chan);
  3140. if (!generator
  3141. || !generator->generate
  3142. || f->frametype != AST_FRAME_VOICE
  3143. || !ast_channel_generatordata(chan)
  3144. || ast_channel_timingfunc(chan)) {
  3145. return;
  3146. }
  3147. /*
  3148. * We must generate frames in phase locked mode since
  3149. * we have no internal timer available.
  3150. */
  3151. if (ast_format_cmp(f->subclass.format, ast_channel_writeformat(chan)) == AST_FORMAT_CMP_NOT_EQUAL) {
  3152. float factor;
  3153. factor = ((float) ast_format_get_sample_rate(ast_channel_writeformat(chan))) / ((float) ast_format_get_sample_rate(f->subclass.format));
  3154. samples = (int) (((float) f->samples) * factor);
  3155. } else {
  3156. samples = f->samples;
  3157. }
  3158. gendata = ast_channel_generatordata(chan);
  3159. ast_channel_generatordata_set(chan, NULL); /* reset, to let writes go through */
  3160. /*
  3161. * This unlock is here based on two assumptions that hold true at
  3162. * this point in the code. 1) this function is only called from
  3163. * within __ast_read() and 2) all generators call ast_write() in
  3164. * their generate callback.
  3165. *
  3166. * The reason this is added is so that when ast_write is called,
  3167. * the lock that occurs there will not recursively lock the
  3168. * channel. Doing this will allow deadlock avoidance to work in
  3169. * deeper functions.
  3170. */
  3171. ast_channel_unlock(chan);
  3172. res = generator->generate(chan, gendata, f->datalen, samples);
  3173. ast_channel_lock(chan);
  3174. if (generator == ast_channel_generator(chan)) {
  3175. ast_channel_generatordata_set(chan, gendata);
  3176. if (res) {
  3177. ast_debug(1, "Auto-deactivating generator\n");
  3178. ast_deactivate_generator(chan);
  3179. }
  3180. }
  3181. }
  3182. static inline void queue_dtmf_readq(struct ast_channel *chan, struct ast_frame *f)
  3183. {
  3184. struct ast_frame *fr = ast_channel_dtmff(chan);
  3185. fr->frametype = AST_FRAME_DTMF_END;
  3186. fr->subclass.integer = f->subclass.integer;
  3187. fr->len = f->len;
  3188. /* The only time this function will be called is for a frame that just came
  3189. * out of the channel driver. So, we want to stick it on the tail of the
  3190. * readq. */
  3191. ast_queue_frame(chan, fr);
  3192. }
  3193. /*!
  3194. * \brief Determine whether or not we should ignore DTMF in the readq
  3195. */
  3196. static inline int should_skip_dtmf(struct ast_channel *chan)
  3197. {
  3198. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF | AST_FLAG_EMULATE_DTMF)) {
  3199. /* We're in the middle of emulating a digit, or DTMF has been
  3200. * explicitly deferred. Skip this digit, then. */
  3201. return 1;
  3202. }
  3203. if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
  3204. ast_tvdiff_ms(ast_tvnow(), *ast_channel_dtmf_tv(chan)) < AST_MIN_DTMF_GAP) {
  3205. /* We're not in the middle of a digit, but it hasn't been long enough
  3206. * since the last digit, so we'll have to skip DTMF for now. */
  3207. return 1;
  3208. }
  3209. return 0;
  3210. }
  3211. /*!
  3212. * \brief calculates the number of samples to jump forward with in a monitor stream.
  3213. * \note When using ast_seekstream() with the read and write streams of a monitor,
  3214. * the number of samples to seek forward must be of the same sample rate as the stream
  3215. * or else the jump will not be calculated correctly.
  3216. *
  3217. * \retval number of samples to seek forward after rate conversion.
  3218. */
  3219. static inline int calc_monitor_jump(int samples, int sample_rate, int seek_rate)
  3220. {
  3221. int diff = sample_rate - seek_rate;
  3222. if (diff > 0) {
  3223. samples = samples / (float) (sample_rate / seek_rate);
  3224. } else if (diff < 0) {
  3225. samples = samples * (float) (seek_rate / sample_rate);
  3226. }
  3227. return samples;
  3228. }
  3229. static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio)
  3230. {
  3231. struct ast_frame *f = NULL; /* the return value */
  3232. int prestate;
  3233. int cause = 0;
  3234. /* this function is very long so make sure there is only one return
  3235. * point at the end (there are only two exceptions to this).
  3236. */
  3237. ast_channel_lock(chan);
  3238. /* Stop if we're a zombie or need a soft hangup */
  3239. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
  3240. if (ast_channel_generator(chan))
  3241. ast_deactivate_generator(chan);
  3242. /*
  3243. * It is possible for chan->_softhangup to be set and there
  3244. * still be control frames that need to be read. Instead of
  3245. * just going to 'done' in the case of ast_check_hangup(), we
  3246. * need to queue the end-of-Q frame so that it can mark the end
  3247. * of the read queue. If there are frames to be read,
  3248. * ast_queue_control() will be called repeatedly, but will only
  3249. * queue the first end-of-Q frame.
  3250. */
  3251. if (ast_channel_softhangup_internal_flag(chan)) {
  3252. ast_queue_control(chan, AST_CONTROL_END_OF_Q);
  3253. } else {
  3254. goto done;
  3255. }
  3256. } else {
  3257. #ifdef AST_DEVMODE
  3258. /*
  3259. * The ast_waitfor() code records which of the channel's file
  3260. * descriptors reported that data is available. In theory,
  3261. * ast_read() should only be called after ast_waitfor() reports
  3262. * that a channel has data available for reading. However,
  3263. * there still may be some edge cases throughout the code where
  3264. * ast_read() is called improperly. This can potentially cause
  3265. * problems, so if this is a developer build, make a lot of
  3266. * noise if this happens so that it can be addressed.
  3267. *
  3268. * One of the potential problems is blocking on a dead channel.
  3269. */
  3270. if (ast_channel_fdno(chan) == -1) {
  3271. ast_log(LOG_ERROR,
  3272. "ast_read() on chan '%s' called with no recorded file descriptor.\n",
  3273. ast_channel_name(chan));
  3274. }
  3275. #endif
  3276. }
  3277. prestate = ast_channel_state(chan);
  3278. if (ast_channel_timingfd(chan) > -1 && ast_channel_fdno(chan) == AST_TIMING_FD) {
  3279. enum ast_timer_event res;
  3280. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
  3281. res = ast_timer_get_event(ast_channel_timer(chan));
  3282. switch (res) {
  3283. case AST_TIMING_EVENT_EXPIRED:
  3284. if (ast_timer_ack(ast_channel_timer(chan), 1) < 0) {
  3285. ast_log(LOG_ERROR, "Failed to acknoweldge timer in ast_read\n");
  3286. goto done;
  3287. }
  3288. if (ast_channel_timingfunc(chan)) {
  3289. /* save a copy of func/data before unlocking the channel */
  3290. ast_timing_func_t func = ast_channel_timingfunc(chan);
  3291. void *data = ast_channel_timingdata(chan);
  3292. int got_ref = 0;
  3293. if (data && ast_test_flag(ast_channel_flags(chan), AST_FLAG_TIMINGDATA_IS_AO2_OBJ)) {
  3294. ao2_ref(data, 1);
  3295. got_ref = 1;
  3296. }
  3297. ast_channel_fdno_set(chan, -1);
  3298. ast_channel_unlock(chan);
  3299. func(data);
  3300. if (got_ref) {
  3301. ao2_ref(data, -1);
  3302. }
  3303. } else {
  3304. ast_timer_set_rate(ast_channel_timer(chan), 0);
  3305. ast_channel_fdno_set(chan, -1);
  3306. ast_channel_unlock(chan);
  3307. }
  3308. /* cannot 'goto done' because the channel is already unlocked */
  3309. return &ast_null_frame;
  3310. case AST_TIMING_EVENT_CONTINUOUS:
  3311. if (AST_LIST_EMPTY(ast_channel_readq(chan)) ||
  3312. !AST_LIST_NEXT(AST_LIST_FIRST(ast_channel_readq(chan)), frame_list)) {
  3313. ast_timer_disable_continuous(ast_channel_timer(chan));
  3314. }
  3315. break;
  3316. }
  3317. } else if (ast_channel_fd_isset(chan, AST_GENERATOR_FD) && ast_channel_fdno(chan) == AST_GENERATOR_FD) {
  3318. /* if the AST_GENERATOR_FD is set, call the generator with args
  3319. * set to -1 so it can do whatever it needs to.
  3320. */
  3321. void *tmp = ast_channel_generatordata(chan);
  3322. ast_channel_generatordata_set(chan, NULL); /* reset to let ast_write get through */
  3323. ast_channel_generator(chan)->generate(chan, tmp, -1, -1);
  3324. ast_channel_generatordata_set(chan, tmp);
  3325. f = &ast_null_frame;
  3326. ast_channel_fdno_set(chan, -1);
  3327. goto done;
  3328. } else if (ast_channel_fd_isset(chan, AST_JITTERBUFFER_FD) && ast_channel_fdno(chan) == AST_JITTERBUFFER_FD) {
  3329. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
  3330. }
  3331. /* Read and ignore anything on the alertpipe, but read only
  3332. one sizeof(blah) per frame that we send from it */
  3333. if (ast_channel_internal_alert_read(chan) == AST_ALERT_READ_FATAL) {
  3334. f = &ast_null_frame;
  3335. goto done;
  3336. }
  3337. /* Check for pending read queue */
  3338. if (!AST_LIST_EMPTY(ast_channel_readq(chan))) {
  3339. int skip_dtmf = should_skip_dtmf(chan);
  3340. AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_readq(chan), f, frame_list) {
  3341. /* We have to be picky about which frame we pull off of the readq because
  3342. * there are cases where we want to leave DTMF frames on the queue until
  3343. * some later time. */
  3344. if ( (f->frametype == AST_FRAME_DTMF_BEGIN || f->frametype == AST_FRAME_DTMF_END) && skip_dtmf) {
  3345. continue;
  3346. }
  3347. AST_LIST_REMOVE_CURRENT(frame_list);
  3348. break;
  3349. }
  3350. AST_LIST_TRAVERSE_SAFE_END;
  3351. if (!f) {
  3352. /* There were no acceptable frames on the readq. */
  3353. f = &ast_null_frame;
  3354. ast_channel_alert_write(chan);
  3355. }
  3356. /* Interpret hangup and end-of-Q frames to return NULL */
  3357. /* XXX why not the same for frames from the channel ? */
  3358. if (f->frametype == AST_FRAME_CONTROL) {
  3359. switch (f->subclass.integer) {
  3360. case AST_CONTROL_HANGUP:
  3361. ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
  3362. cause = f->data.uint32;
  3363. /* Fall through */
  3364. case AST_CONTROL_END_OF_Q:
  3365. ast_frfree(f);
  3366. f = NULL;
  3367. break;
  3368. default:
  3369. break;
  3370. }
  3371. }
  3372. } else {
  3373. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION)) {
  3374. if (ast_channel_tech(chan)->exception)
  3375. f = ast_channel_tech(chan)->exception(chan);
  3376. else {
  3377. ast_log(LOG_WARNING, "Exception flag set on '%s', but no exception handler\n", ast_channel_name(chan));
  3378. f = &ast_null_frame;
  3379. }
  3380. /* Clear the exception flag */
  3381. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
  3382. } else if (ast_channel_tech(chan) && ast_channel_tech(chan)->read)
  3383. f = ast_channel_tech(chan)->read(chan);
  3384. else
  3385. ast_log(LOG_WARNING, "No read routine on channel %s\n", ast_channel_name(chan));
  3386. }
  3387. /* Perform the framehook read event here. After the frame enters the framehook list
  3388. * there is no telling what will happen, <insert mad scientist laugh here>!!! */
  3389. f = ast_framehook_list_read_event(ast_channel_framehooks(chan), f);
  3390. /*
  3391. * Reset the recorded file descriptor that triggered this read so that we can
  3392. * easily detect when ast_read() is called without properly using ast_waitfor().
  3393. */
  3394. ast_channel_fdno_set(chan, -1);
  3395. if (f) {
  3396. struct ast_frame *readq_tail = AST_LIST_LAST(ast_channel_readq(chan));
  3397. struct ast_control_read_action_payload *read_action_payload;
  3398. struct ast_party_connected_line connected;
  3399. int hooked = 0;
  3400. /* if the channel driver returned more than one frame, stuff the excess
  3401. into the readq for the next ast_read call
  3402. */
  3403. if (AST_LIST_NEXT(f, frame_list)) {
  3404. ast_queue_frame(chan, AST_LIST_NEXT(f, frame_list));
  3405. ast_frfree(AST_LIST_NEXT(f, frame_list));
  3406. AST_LIST_NEXT(f, frame_list) = NULL;
  3407. }
  3408. switch (f->frametype) {
  3409. case AST_FRAME_CONTROL:
  3410. if (f->subclass.integer == AST_CONTROL_ANSWER) {
  3411. if (prestate == AST_STATE_UP && ast_channel_is_bridged(chan)) {
  3412. ast_debug(1, "Dropping duplicate answer!\n");
  3413. ast_frfree(f);
  3414. f = &ast_null_frame;
  3415. } else {
  3416. /*
  3417. * Mark when outgoing channel answered so we can know how
  3418. * long the channel has been up.
  3419. */
  3420. set_channel_answer_time(chan);
  3421. ast_setstate(chan, AST_STATE_UP);
  3422. }
  3423. } else if (f->subclass.integer == AST_CONTROL_READ_ACTION) {
  3424. read_action_payload = f->data.ptr;
  3425. switch (read_action_payload->action) {
  3426. case AST_FRAME_READ_ACTION_CONNECTED_LINE_MACRO:
  3427. ast_party_connected_line_init(&connected);
  3428. ast_party_connected_line_copy(&connected, ast_channel_connected(chan));
  3429. if (ast_connected_line_parse_data(read_action_payload->payload,
  3430. read_action_payload->payload_size, &connected)) {
  3431. ast_party_connected_line_free(&connected);
  3432. break;
  3433. }
  3434. ast_channel_unlock(chan);
  3435. if (ast_channel_connected_line_sub(NULL, chan, &connected, 0) &&
  3436. ast_channel_connected_line_macro(NULL, chan, &connected, 1, 0)) {
  3437. ast_indicate_data(chan, AST_CONTROL_CONNECTED_LINE,
  3438. read_action_payload->payload,
  3439. read_action_payload->payload_size);
  3440. }
  3441. ast_party_connected_line_free(&connected);
  3442. ast_channel_lock(chan);
  3443. break;
  3444. case AST_FRAME_READ_ACTION_SEND_TEXT:
  3445. ast_channel_unlock(chan);
  3446. ast_sendtext(chan, (const char *) read_action_payload->payload);
  3447. ast_channel_lock(chan);
  3448. break;
  3449. }
  3450. ast_frfree(f);
  3451. f = &ast_null_frame;
  3452. }
  3453. break;
  3454. case AST_FRAME_DTMF_END:
  3455. send_dtmf_end_event(chan, DTMF_RECEIVED, f->subclass.integer, f->len);
  3456. ast_log(LOG_DTMF, "DTMF end '%c' received on %s, duration %ld ms\n", f->subclass.integer, ast_channel_name(chan), f->len);
  3457. /* Queue it up if DTMF is deferred, or if DTMF emulation is forced. */
  3458. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF) || ast_test_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF)) {
  3459. queue_dtmf_readq(chan, f);
  3460. ast_frfree(f);
  3461. f = &ast_null_frame;
  3462. } else if (!ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF | AST_FLAG_END_DTMF_ONLY)) {
  3463. if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
  3464. ast_tvdiff_ms(ast_tvnow(), *ast_channel_dtmf_tv(chan)) < AST_MIN_DTMF_GAP) {
  3465. /* If it hasn't been long enough, defer this digit */
  3466. queue_dtmf_readq(chan, f);
  3467. ast_frfree(f);
  3468. f = &ast_null_frame;
  3469. } else {
  3470. /* There was no begin, turn this into a begin and send the end later */
  3471. struct timeval tv = ast_tvnow();
  3472. f->frametype = AST_FRAME_DTMF_BEGIN;
  3473. ast_set_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF);
  3474. ast_channel_dtmf_digit_to_emulate_set(chan, f->subclass.integer);
  3475. ast_channel_dtmf_tv_set(chan, &tv);
  3476. if (f->len) {
  3477. if (f->len > option_dtmfminduration)
  3478. ast_channel_emulate_dtmf_duration_set(chan, f->len);
  3479. else
  3480. ast_channel_emulate_dtmf_duration_set(chan, option_dtmfminduration);
  3481. } else
  3482. ast_channel_emulate_dtmf_duration_set(chan, AST_DEFAULT_EMULATE_DTMF_DURATION);
  3483. ast_log(LOG_DTMF, "DTMF begin emulation of '%c' with duration %u queued on %s\n", f->subclass.integer, ast_channel_emulate_dtmf_duration(chan), ast_channel_name(chan));
  3484. }
  3485. if (ast_channel_audiohooks(chan)) {
  3486. struct ast_frame *old_frame = f;
  3487. /*!
  3488. * \todo XXX It is possible to write a digit to the audiohook twice
  3489. * if the digit was originally read while the channel was in autoservice. */
  3490. f = ast_audiohook_write_list(chan, ast_channel_audiohooks(chan), AST_AUDIOHOOK_DIRECTION_READ, f);
  3491. if (old_frame != f)
  3492. ast_frfree(old_frame);
  3493. }
  3494. } else {
  3495. struct timeval now = ast_tvnow();
  3496. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF)) {
  3497. ast_log(LOG_DTMF, "DTMF end accepted with begin '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
  3498. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF);
  3499. if (!f->len)
  3500. f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
  3501. /* detect tones that were received on
  3502. * the wire with durations shorter than
  3503. * option_dtmfminduration and set f->len
  3504. * to the actual duration of the DTMF
  3505. * frames on the wire. This will cause
  3506. * dtmf emulation to be triggered later
  3507. * on.
  3508. */
  3509. if (ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan)) < option_dtmfminduration) {
  3510. f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
  3511. ast_log(LOG_DTMF, "DTMF end '%c' detected to have actual duration %ld on the wire, emulation will be triggered on %s\n", f->subclass.integer, f->len, ast_channel_name(chan));
  3512. }
  3513. } else if (!f->len) {
  3514. ast_log(LOG_DTMF, "DTMF end accepted without begin '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
  3515. f->len = option_dtmfminduration;
  3516. }
  3517. if (f->len < option_dtmfminduration && !ast_test_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY)) {
  3518. ast_log(LOG_DTMF, "DTMF end '%c' has duration %ld but want minimum %u, emulating on %s\n", f->subclass.integer, f->len, option_dtmfminduration, ast_channel_name(chan));
  3519. ast_set_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF);
  3520. ast_channel_dtmf_digit_to_emulate_set(chan, f->subclass.integer);
  3521. ast_channel_emulate_dtmf_duration_set(chan, option_dtmfminduration - f->len);
  3522. ast_frfree(f);
  3523. f = &ast_null_frame;
  3524. } else {
  3525. ast_log(LOG_DTMF, "DTMF end passthrough '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
  3526. if (f->len < option_dtmfminduration) {
  3527. f->len = option_dtmfminduration;
  3528. }
  3529. ast_channel_dtmf_tv_set(chan, &now);
  3530. }
  3531. if (ast_channel_audiohooks(chan)) {
  3532. struct ast_frame *old_frame = f;
  3533. f = ast_audiohook_write_list(chan, ast_channel_audiohooks(chan), AST_AUDIOHOOK_DIRECTION_READ, f);
  3534. if (old_frame != f)
  3535. ast_frfree(old_frame);
  3536. }
  3537. }
  3538. break;
  3539. case AST_FRAME_DTMF_BEGIN:
  3540. send_dtmf_begin_event(chan, DTMF_RECEIVED, f->subclass.integer);
  3541. ast_log(LOG_DTMF, "DTMF begin '%c' received on %s\n", f->subclass.integer, ast_channel_name(chan));
  3542. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF | AST_FLAG_END_DTMF_ONLY | AST_FLAG_EMULATE_DTMF) ||
  3543. (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
  3544. ast_tvdiff_ms(ast_tvnow(), *ast_channel_dtmf_tv(chan)) < AST_MIN_DTMF_GAP) ) {
  3545. ast_log(LOG_DTMF, "DTMF begin ignored '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
  3546. ast_frfree(f);
  3547. f = &ast_null_frame;
  3548. } else {
  3549. struct timeval now = ast_tvnow();
  3550. ast_set_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF);
  3551. ast_channel_dtmf_tv_set(chan, &now);
  3552. ast_log(LOG_DTMF, "DTMF begin passthrough '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
  3553. }
  3554. break;
  3555. case AST_FRAME_NULL:
  3556. /* The EMULATE_DTMF flag must be cleared here as opposed to when the duration
  3557. * is reached , because we want to make sure we pass at least one
  3558. * voice frame through before starting the next digit, to ensure a gap
  3559. * between DTMF digits. */
  3560. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF)) {
  3561. struct timeval now = ast_tvnow();
  3562. if (!ast_channel_emulate_dtmf_duration(chan)) {
  3563. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF);
  3564. ast_channel_dtmf_digit_to_emulate_set(chan, 0);
  3565. } else if (ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan)) >= ast_channel_emulate_dtmf_duration(chan)) {
  3566. ast_channel_emulate_dtmf_duration_set(chan, 0);
  3567. ast_frfree(f);
  3568. f = ast_channel_dtmff(chan);
  3569. f->frametype = AST_FRAME_DTMF_END;
  3570. f->subclass.integer = ast_channel_dtmf_digit_to_emulate(chan);
  3571. f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
  3572. ast_channel_dtmf_tv_set(chan, &now);
  3573. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF);
  3574. ast_channel_dtmf_digit_to_emulate_set(chan, 0);
  3575. ast_log(LOG_DTMF, "DTMF end emulation of '%c' queued on %s\n", f->subclass.integer, ast_channel_name(chan));
  3576. if (ast_channel_audiohooks(chan)) {
  3577. struct ast_frame *old_frame = f;
  3578. f = ast_audiohook_write_list(chan, ast_channel_audiohooks(chan), AST_AUDIOHOOK_DIRECTION_READ, f);
  3579. if (old_frame != f) {
  3580. ast_frfree(old_frame);
  3581. }
  3582. }
  3583. }
  3584. }
  3585. break;
  3586. case AST_FRAME_VOICE:
  3587. /* The EMULATE_DTMF flag must be cleared here as opposed to when the duration
  3588. * is reached , because we want to make sure we pass at least one
  3589. * voice frame through before starting the next digit, to ensure a gap
  3590. * between DTMF digits. */
  3591. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF) && !ast_channel_emulate_dtmf_duration(chan)) {
  3592. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF);
  3593. ast_channel_dtmf_digit_to_emulate_set(chan, 0);
  3594. }
  3595. if (dropaudio || ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF)) {
  3596. if (dropaudio)
  3597. ast_read_generator_actions(chan, f);
  3598. ast_frfree(f);
  3599. f = &ast_null_frame;
  3600. }
  3601. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF) && !ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF)) {
  3602. struct timeval now = ast_tvnow();
  3603. if (ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan)) >= ast_channel_emulate_dtmf_duration(chan)) {
  3604. ast_channel_emulate_dtmf_duration_set(chan, 0);
  3605. ast_frfree(f);
  3606. f = ast_channel_dtmff(chan);
  3607. f->frametype = AST_FRAME_DTMF_END;
  3608. f->subclass.integer = ast_channel_dtmf_digit_to_emulate(chan);
  3609. f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
  3610. ast_channel_dtmf_tv_set(chan, &now);
  3611. if (ast_channel_audiohooks(chan)) {
  3612. struct ast_frame *old_frame = f;
  3613. f = ast_audiohook_write_list(chan, ast_channel_audiohooks(chan), AST_AUDIOHOOK_DIRECTION_READ, f);
  3614. if (old_frame != f)
  3615. ast_frfree(old_frame);
  3616. }
  3617. ast_log(LOG_DTMF, "DTMF end emulation of '%c' queued on %s\n", f->subclass.integer, ast_channel_name(chan));
  3618. } else {
  3619. /* Drop voice frames while we're still in the middle of the digit */
  3620. ast_frfree(f);
  3621. f = &ast_null_frame;
  3622. }
  3623. break;
  3624. }
  3625. if (f->frametype != AST_FRAME_VOICE) {
  3626. break;
  3627. }
  3628. if (ast_format_cmp(f->subclass.format, ast_channel_rawreadformat(chan)) != AST_FORMAT_CMP_EQUAL
  3629. && ast_format_cmp(f->subclass.format, ast_channel_readformat(chan)) != AST_FORMAT_CMP_EQUAL) {
  3630. struct ast_format *core_format;
  3631. /*
  3632. * Note: This frame may not be one of the current native
  3633. * formats. We may have gotten it out of the read queue from
  3634. * a previous multi-frame translation, from a framehook
  3635. * injected frame, or the device we're talking to isn't
  3636. * respecting negotiated formats. Regardless we will accept
  3637. * all frames.
  3638. *
  3639. * Update the read translation path to handle the new format
  3640. * that just came in. If the core wants slinear we need to
  3641. * setup a new translation path because the core is usually
  3642. * doing something with the audio itself and may not handle
  3643. * any other format. e.g., Softmix bridge, holding bridge
  3644. * announcer channel, recording, AMD... Otherwise, we'll
  3645. * setup to pass the frame as is to the core. In this case
  3646. * the core doesn't care. The channel is likely in
  3647. * autoservice, safesleep, or the channel is in a bridge.
  3648. * Let the bridge technology deal with format compatibility
  3649. * between the channels in the bridge.
  3650. *
  3651. * Beware of the transcode_via_slin and genericplc options as
  3652. * they force any transcoding to go through slin on a bridge.
  3653. * Unfortunately transcode_via_slin is enabled by default and
  3654. * genericplc is enabled in the codecs.conf.sample file.
  3655. *
  3656. * XXX Only updating translation to slinear frames has some
  3657. * corner cases if slinear is one of the native formats and
  3658. * there are different sample rates involved. We might wind
  3659. * up with conflicting translation paths between channels
  3660. * where the read translation path on this channel reduces
  3661. * the sample rate followed by a write translation path on
  3662. * the peer channel that increases the sample rate.
  3663. */
  3664. core_format = ast_channel_readformat(chan);
  3665. if (!ast_format_cache_is_slinear(core_format)) {
  3666. core_format = f->subclass.format;
  3667. }
  3668. if (ast_set_read_format_path(chan, f->subclass.format, core_format)) {
  3669. /* Drop frame. We couldn't make it compatible with the core. */
  3670. ast_frfree(f);
  3671. f = &ast_null_frame;
  3672. break;
  3673. }
  3674. }
  3675. /*
  3676. * Send frame to audiohooks if present, if frametype is linear, to preserve
  3677. * functional compatibility with previous behavior. If not linear, hold off
  3678. * until transcoding is done where we are more likely to have a linear frame
  3679. */
  3680. if (ast_channel_audiohooks(chan) && ast_format_cache_is_slinear(f->subclass.format)) {
  3681. /* Place hooked after declaration */
  3682. struct ast_frame *old_frame = f;
  3683. hooked = 1;
  3684. f = ast_audiohook_write_list(chan, ast_channel_audiohooks(chan), AST_AUDIOHOOK_DIRECTION_READ, f);
  3685. if (old_frame != f) {
  3686. ast_frfree(old_frame);
  3687. }
  3688. }
  3689. if (ast_channel_monitor(chan) && ast_channel_monitor(chan)->read_stream) {
  3690. /* XXX what does this do ? */
  3691. #ifndef MONITOR_CONSTANT_DELAY
  3692. int jump = ast_channel_outsmpl(chan) - ast_channel_insmpl(chan) - 4 * f->samples;
  3693. if (jump >= 0) {
  3694. jump = calc_monitor_jump((ast_channel_outsmpl(chan) - ast_channel_insmpl(chan)),
  3695. ast_format_get_sample_rate(f->subclass.format),
  3696. ast_format_get_sample_rate(ast_channel_monitor(chan)->read_stream->fmt->format));
  3697. if (ast_seekstream(ast_channel_monitor(chan)->read_stream, jump, SEEK_FORCECUR) == -1) {
  3698. ast_log(LOG_WARNING, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n");
  3699. }
  3700. ast_channel_insmpl_set(chan, ast_channel_insmpl(chan) + (ast_channel_outsmpl(chan) - ast_channel_insmpl(chan)) + f->samples);
  3701. } else {
  3702. ast_channel_insmpl_set(chan, ast_channel_insmpl(chan) + f->samples);
  3703. }
  3704. #else
  3705. int jump = calc_monitor_jump((ast_channel_outsmpl(chan) - ast_channel_insmpl(chan)),
  3706. ast_format_get_sample_rate(f->subclass.format),
  3707. ast_format_get_sample_rate(ast_channel_monitor(chan)->read_stream->fmt->format));
  3708. if (jump - MONITOR_DELAY >= 0) {
  3709. if (ast_seekstream(ast_channel_monitor(chan)->read_stream, jump - f->samples, SEEK_FORCECUR) == -1) {
  3710. ast_log(LOG_WARNING, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n");
  3711. }
  3712. ast_channel_insmpl(chan) += ast_channel_outsmpl(chan) - ast_channel_insmpl(chan);
  3713. } else {
  3714. ast_channel_insmpl(chan) += f->samples;
  3715. }
  3716. #endif
  3717. if (ast_channel_monitor(chan)->state == AST_MONITOR_RUNNING) {
  3718. if (ast_writestream(ast_channel_monitor(chan)->read_stream, f) < 0)
  3719. ast_log(LOG_WARNING, "Failed to write data to channel monitor read stream\n");
  3720. }
  3721. }
  3722. if (ast_channel_readtrans(chan)
  3723. && ast_format_cmp(f->subclass.format, ast_channel_rawreadformat(chan)) == AST_FORMAT_CMP_EQUAL) {
  3724. f = ast_translate(ast_channel_readtrans(chan), f, 1);
  3725. if (!f) {
  3726. f = &ast_null_frame;
  3727. }
  3728. }
  3729. /* Second chance at hooking a linear frame, also the last chance */
  3730. if (ast_channel_audiohooks(chan) && !hooked) {
  3731. struct ast_frame *old_frame = f;
  3732. f = ast_audiohook_write_list(chan, ast_channel_audiohooks(chan), AST_AUDIOHOOK_DIRECTION_READ, f);
  3733. if (old_frame != f) {
  3734. ast_frfree(old_frame);
  3735. }
  3736. }
  3737. /*
  3738. * It is possible for the translation process on the channel to have
  3739. * produced multiple frames from the single input frame we passed it; if
  3740. * this happens, queue the additional frames *before* the frames we may
  3741. * have queued earlier. if the readq was empty, put them at the head of
  3742. * the queue, and if it was not, put them just after the frame that was
  3743. * at the end of the queue.
  3744. */
  3745. if (AST_LIST_NEXT(f, frame_list)) {
  3746. struct ast_frame *cur, *multi_frame = AST_LIST_NEXT(f, frame_list);
  3747. /* Mark these frames as being re-queued */
  3748. for (cur = multi_frame; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
  3749. ast_set_flag(cur, AST_FRFLAG_REQUEUED);
  3750. }
  3751. if (!readq_tail) {
  3752. ast_queue_frame_head(chan, multi_frame);
  3753. } else {
  3754. __ast_queue_frame(chan, multi_frame, 0, readq_tail);
  3755. }
  3756. ast_frfree(multi_frame);
  3757. AST_LIST_NEXT(f, frame_list) = NULL;
  3758. }
  3759. /*
  3760. * Run generator sitting on the line if timing device not available
  3761. * and synchronous generation of outgoing frames is necessary
  3762. */
  3763. ast_read_generator_actions(chan, f);
  3764. break;
  3765. default:
  3766. /* Just pass it on! */
  3767. break;
  3768. }
  3769. } else {
  3770. /* Make sure we always return NULL in the future */
  3771. if (!ast_channel_softhangup_internal_flag(chan)) {
  3772. ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
  3773. }
  3774. if (cause)
  3775. ast_channel_hangupcause_set(chan, cause);
  3776. if (ast_channel_generator(chan))
  3777. ast_deactivate_generator(chan);
  3778. /* We no longer End the CDR here */
  3779. }
  3780. /* High bit prints debugging */
  3781. if (ast_channel_fin(chan) & DEBUGCHAN_FLAG)
  3782. ast_frame_dump(ast_channel_name(chan), f, "<<");
  3783. ast_channel_fin_set(chan, FRAMECOUNT_INC(ast_channel_fin(chan)));
  3784. done:
  3785. if (ast_channel_music_state(chan) && ast_channel_generator(chan) && ast_channel_generator(chan)->digit && f && f->frametype == AST_FRAME_DTMF_END)
  3786. ast_channel_generator(chan)->digit(chan, f->subclass.integer);
  3787. if (ast_channel_audiohooks(chan) && ast_audiohook_write_list_empty(ast_channel_audiohooks(chan))) {
  3788. /* The list gets recreated if audiohooks are added again later */
  3789. ast_audiohook_detach_list(ast_channel_audiohooks(chan));
  3790. ast_channel_audiohooks_set(chan, NULL);
  3791. }
  3792. ast_channel_unlock(chan);
  3793. return f;
  3794. }
  3795. struct ast_frame *ast_read(struct ast_channel *chan)
  3796. {
  3797. return __ast_read(chan, 0);
  3798. }
  3799. struct ast_frame *ast_read_noaudio(struct ast_channel *chan)
  3800. {
  3801. return __ast_read(chan, 1);
  3802. }
  3803. int ast_indicate(struct ast_channel *chan, int condition)
  3804. {
  3805. return ast_indicate_data(chan, condition, NULL, 0);
  3806. }
  3807. static int attribute_const is_visible_indication(enum ast_control_frame_type condition)
  3808. {
  3809. /* Don't include a default case here so that we get compiler warnings
  3810. * when a new type is added. */
  3811. switch (condition) {
  3812. case AST_CONTROL_PROGRESS:
  3813. case AST_CONTROL_PROCEEDING:
  3814. case AST_CONTROL_VIDUPDATE:
  3815. case AST_CONTROL_SRCUPDATE:
  3816. case AST_CONTROL_SRCCHANGE:
  3817. case AST_CONTROL_RADIO_KEY:
  3818. case AST_CONTROL_RADIO_UNKEY:
  3819. case AST_CONTROL_OPTION:
  3820. case AST_CONTROL_WINK:
  3821. case AST_CONTROL_FLASH:
  3822. case AST_CONTROL_OFFHOOK:
  3823. case AST_CONTROL_TAKEOFFHOOK:
  3824. case AST_CONTROL_ANSWER:
  3825. case AST_CONTROL_HANGUP:
  3826. case AST_CONTROL_CONNECTED_LINE:
  3827. case AST_CONTROL_REDIRECTING:
  3828. case AST_CONTROL_TRANSFER:
  3829. case AST_CONTROL_T38_PARAMETERS:
  3830. case _XXX_AST_CONTROL_T38:
  3831. case AST_CONTROL_CC:
  3832. case AST_CONTROL_READ_ACTION:
  3833. case AST_CONTROL_AOC:
  3834. case AST_CONTROL_END_OF_Q:
  3835. case AST_CONTROL_MCID:
  3836. case AST_CONTROL_UPDATE_RTP_PEER:
  3837. case AST_CONTROL_PVT_CAUSE_CODE:
  3838. case AST_CONTROL_MASQUERADE_NOTIFY:
  3839. case AST_CONTROL_STREAM_STOP:
  3840. case AST_CONTROL_STREAM_SUSPEND:
  3841. case AST_CONTROL_STREAM_REVERSE:
  3842. case AST_CONTROL_STREAM_FORWARD:
  3843. case AST_CONTROL_STREAM_RESTART:
  3844. case AST_CONTROL_RECORD_CANCEL:
  3845. case AST_CONTROL_RECORD_STOP:
  3846. case AST_CONTROL_RECORD_SUSPEND:
  3847. case AST_CONTROL_RECORD_MUTE:
  3848. break;
  3849. case AST_CONTROL_INCOMPLETE:
  3850. case AST_CONTROL_CONGESTION:
  3851. case AST_CONTROL_BUSY:
  3852. case AST_CONTROL_RINGING:
  3853. case AST_CONTROL_RING:
  3854. case AST_CONTROL_HOLD:
  3855. /* You can hear these */
  3856. return 1;
  3857. case AST_CONTROL_UNHOLD:
  3858. /* This is a special case. You stop hearing this. */
  3859. break;
  3860. }
  3861. return 0;
  3862. }
  3863. void ast_channel_hangupcause_hash_set(struct ast_channel *chan, const struct ast_control_pvt_cause_code *cause_code, int datalen)
  3864. {
  3865. char causevar[256];
  3866. if (ast_channel_dialed_causes_add(chan, cause_code, datalen)) {
  3867. ast_log(LOG_WARNING, "Unable to store hangup cause for %s on %s\n", cause_code->chan_name, ast_channel_name(chan));
  3868. }
  3869. if (cause_code->emulate_sip_cause) {
  3870. snprintf(causevar, sizeof(causevar), "HASH(SIP_CAUSE,%s)", cause_code->chan_name);
  3871. ast_func_write(chan, causevar, cause_code->code);
  3872. }
  3873. }
  3874. enum ama_flags ast_channel_string2amaflag(const char *flag)
  3875. {
  3876. if (!strcasecmp(flag, "default"))
  3877. return DEFAULT_AMA_FLAGS;
  3878. if (!strcasecmp(flag, "omit"))
  3879. return AST_AMA_OMIT;
  3880. if (!strcasecmp(flag, "billing"))
  3881. return AST_AMA_BILLING;
  3882. if (!strcasecmp(flag, "documentation"))
  3883. return AST_AMA_DOCUMENTATION;
  3884. return AST_AMA_NONE;
  3885. }
  3886. const char *ast_channel_amaflags2string(enum ama_flags flag)
  3887. {
  3888. switch (flag) {
  3889. case AST_AMA_OMIT:
  3890. return "OMIT";
  3891. case AST_AMA_BILLING:
  3892. return "BILLING";
  3893. case AST_AMA_DOCUMENTATION:
  3894. return "DOCUMENTATION";
  3895. default:
  3896. return "Unknown";
  3897. }
  3898. }
  3899. /*!
  3900. * \internal
  3901. * \brief Preprocess connected line update.
  3902. * \since 12.0.0
  3903. *
  3904. * \param chan channel to change the indication
  3905. * \param data pointer to payload data
  3906. * \param datalen size of payload data
  3907. *
  3908. * \note This function assumes chan is locked.
  3909. *
  3910. * \retval 0 keep going.
  3911. * \retval -1 quit now.
  3912. */
  3913. static int indicate_connected_line(struct ast_channel *chan, const void *data, size_t datalen)
  3914. {
  3915. struct ast_party_connected_line *chan_connected = ast_channel_connected(chan);
  3916. struct ast_party_connected_line *chan_indicated = ast_channel_connected_indicated(chan);
  3917. struct ast_party_connected_line connected;
  3918. unsigned char current[1024];
  3919. unsigned char proposed[1024];
  3920. int current_size;
  3921. int proposed_size;
  3922. int res;
  3923. ast_party_connected_line_set_init(&connected, chan_connected);
  3924. res = ast_connected_line_parse_data(data, datalen, &connected);
  3925. if (!res) {
  3926. ast_channel_set_connected_line(chan, &connected, NULL);
  3927. }
  3928. ast_party_connected_line_free(&connected);
  3929. if (res) {
  3930. return -1;
  3931. }
  3932. current_size = ast_connected_line_build_data(current, sizeof(current),
  3933. chan_indicated, NULL);
  3934. proposed_size = ast_connected_line_build_data(proposed, sizeof(proposed),
  3935. chan_connected, NULL);
  3936. if (current_size == -1 || proposed_size == -1) {
  3937. return -1;
  3938. }
  3939. if (current_size == proposed_size && !memcmp(current, proposed, current_size)) {
  3940. ast_debug(1, "%s: Dropping redundant connected line update \"%s\" <%s>.\n",
  3941. ast_channel_name(chan),
  3942. S_COR(chan_connected->id.name.valid, chan_connected->id.name.str, ""),
  3943. S_COR(chan_connected->id.number.valid, chan_connected->id.number.str, ""));
  3944. return -1;
  3945. }
  3946. ast_party_connected_line_copy(chan_indicated, chan_connected);
  3947. return 0;
  3948. }
  3949. /*!
  3950. * \internal
  3951. * \brief Preprocess redirecting update.
  3952. * \since 12.0.0
  3953. *
  3954. * \param chan channel to change the indication
  3955. * \param data pointer to payload data
  3956. * \param datalen size of payload data
  3957. *
  3958. * \note This function assumes chan is locked.
  3959. *
  3960. * \retval 0 keep going.
  3961. * \retval -1 quit now.
  3962. */
  3963. static int indicate_redirecting(struct ast_channel *chan, const void *data, size_t datalen)
  3964. {
  3965. struct ast_party_redirecting redirecting;
  3966. int res;
  3967. ast_party_redirecting_set_init(&redirecting, ast_channel_redirecting(chan));
  3968. res = ast_redirecting_parse_data(data, datalen, &redirecting);
  3969. if (!res) {
  3970. ast_channel_set_redirecting(chan, &redirecting, NULL);
  3971. }
  3972. ast_party_redirecting_free(&redirecting);
  3973. return res ? -1 : 0;
  3974. }
  3975. static int indicate_data_internal(struct ast_channel *chan, int _condition, const void *data, size_t datalen)
  3976. {
  3977. /* By using an enum, we'll get compiler warnings for values not handled
  3978. * in switch statements. */
  3979. enum ast_control_frame_type condition = _condition;
  3980. struct ast_tone_zone_sound *ts = NULL;
  3981. const struct ast_control_t38_parameters *t38_parameters;
  3982. int res;
  3983. switch (condition) {
  3984. case AST_CONTROL_CONNECTED_LINE:
  3985. if (indicate_connected_line(chan, data, datalen)) {
  3986. res = 0;
  3987. return res;
  3988. }
  3989. break;
  3990. case AST_CONTROL_REDIRECTING:
  3991. if (indicate_redirecting(chan, data, datalen)) {
  3992. res = 0;
  3993. return res;
  3994. }
  3995. break;
  3996. case AST_CONTROL_HOLD:
  3997. case AST_CONTROL_UNHOLD:
  3998. ast_channel_hold_state_set(chan, _condition);
  3999. break;
  4000. case AST_CONTROL_T38_PARAMETERS:
  4001. t38_parameters = data;
  4002. switch (t38_parameters->request_response) {
  4003. case AST_T38_REQUEST_NEGOTIATE:
  4004. case AST_T38_NEGOTIATED:
  4005. ast_channel_set_is_t38_active_nolock(chan, 1);
  4006. break;
  4007. case AST_T38_REQUEST_TERMINATE:
  4008. case AST_T38_TERMINATED:
  4009. case AST_T38_REFUSED:
  4010. ast_channel_set_is_t38_active_nolock(chan, 0);
  4011. break;
  4012. default:
  4013. break;
  4014. }
  4015. break;
  4016. default:
  4017. break;
  4018. }
  4019. if (is_visible_indication(condition)) {
  4020. /* A new visible indication is requested. */
  4021. ast_channel_visible_indication_set(chan, _condition);
  4022. } else if (condition == AST_CONTROL_UNHOLD || _condition < 0) {
  4023. /* Visible indication is cleared/stopped. */
  4024. ast_channel_visible_indication_set(chan, 0);
  4025. }
  4026. if (ast_channel_tech(chan)->indicate) {
  4027. /* See if the channel driver can handle this condition. */
  4028. res = ast_channel_tech(chan)->indicate(chan, _condition, data, datalen);
  4029. } else {
  4030. res = -1;
  4031. }
  4032. if (!res) {
  4033. /* The channel driver successfully handled this indication */
  4034. res = 0;
  4035. return res;
  4036. }
  4037. /* The channel driver does not support this indication, let's fake
  4038. * it by doing our own tone generation if applicable. */
  4039. /*!\note If we compare the enumeration type, which does not have any
  4040. * negative constants, the compiler may optimize this code away.
  4041. * Therefore, we must perform an integer comparison here. */
  4042. if (_condition < 0) {
  4043. /* Stop any tones that are playing */
  4044. ast_playtones_stop(chan);
  4045. res = 0;
  4046. return res;
  4047. }
  4048. /* Handle conditions that we have tones for. */
  4049. switch (condition) {
  4050. case _XXX_AST_CONTROL_T38:
  4051. /* deprecated T.38 control frame */
  4052. res = -1;
  4053. return res;
  4054. case AST_CONTROL_T38_PARAMETERS:
  4055. /* there is no way to provide 'default' behavior for these
  4056. * control frames, so we need to return failure, but there
  4057. * is also no value in the log message below being emitted
  4058. * since failure to handle these frames is not an 'error'
  4059. * so just return right now. in addition, we want to return
  4060. * whatever value the channel driver returned, in case it
  4061. * has some meaning.*/
  4062. return res;
  4063. case AST_CONTROL_RINGING:
  4064. ts = ast_get_indication_tone(ast_channel_zone(chan), "ring");
  4065. /* It is common practice for channel drivers to return -1 if trying
  4066. * to indicate ringing on a channel which is up. The idea is to let the
  4067. * core generate the ringing inband. However, we don't want the
  4068. * warning message about not being able to handle the specific indication
  4069. * to print nor do we want ast_indicate_data to return an "error" for this
  4070. * condition
  4071. */
  4072. if (ast_channel_state(chan) == AST_STATE_UP) {
  4073. res = 0;
  4074. }
  4075. break;
  4076. case AST_CONTROL_BUSY:
  4077. ts = ast_get_indication_tone(ast_channel_zone(chan), "busy");
  4078. break;
  4079. case AST_CONTROL_INCOMPLETE:
  4080. case AST_CONTROL_CONGESTION:
  4081. ts = ast_get_indication_tone(ast_channel_zone(chan), "congestion");
  4082. break;
  4083. case AST_CONTROL_PVT_CAUSE_CODE:
  4084. ast_channel_hangupcause_hash_set(chan, data, datalen);
  4085. res = 0;
  4086. break;
  4087. case AST_CONTROL_PROGRESS:
  4088. case AST_CONTROL_PROCEEDING:
  4089. case AST_CONTROL_VIDUPDATE:
  4090. case AST_CONTROL_SRCUPDATE:
  4091. case AST_CONTROL_SRCCHANGE:
  4092. case AST_CONTROL_RADIO_KEY:
  4093. case AST_CONTROL_RADIO_UNKEY:
  4094. case AST_CONTROL_OPTION:
  4095. case AST_CONTROL_WINK:
  4096. case AST_CONTROL_FLASH:
  4097. case AST_CONTROL_OFFHOOK:
  4098. case AST_CONTROL_TAKEOFFHOOK:
  4099. case AST_CONTROL_ANSWER:
  4100. case AST_CONTROL_HANGUP:
  4101. case AST_CONTROL_RING:
  4102. case AST_CONTROL_HOLD:
  4103. case AST_CONTROL_UNHOLD:
  4104. case AST_CONTROL_TRANSFER:
  4105. case AST_CONTROL_CONNECTED_LINE:
  4106. case AST_CONTROL_REDIRECTING:
  4107. case AST_CONTROL_CC:
  4108. case AST_CONTROL_READ_ACTION:
  4109. case AST_CONTROL_AOC:
  4110. case AST_CONTROL_END_OF_Q:
  4111. case AST_CONTROL_MCID:
  4112. case AST_CONTROL_MASQUERADE_NOTIFY:
  4113. case AST_CONTROL_UPDATE_RTP_PEER:
  4114. case AST_CONTROL_STREAM_STOP:
  4115. case AST_CONTROL_STREAM_SUSPEND:
  4116. case AST_CONTROL_STREAM_REVERSE:
  4117. case AST_CONTROL_STREAM_FORWARD:
  4118. case AST_CONTROL_STREAM_RESTART:
  4119. case AST_CONTROL_RECORD_CANCEL:
  4120. case AST_CONTROL_RECORD_STOP:
  4121. case AST_CONTROL_RECORD_SUSPEND:
  4122. case AST_CONTROL_RECORD_MUTE:
  4123. /* Nothing left to do for these. */
  4124. res = 0;
  4125. break;
  4126. }
  4127. if (ts) {
  4128. /* We have a tone to play, yay. */
  4129. ast_debug(1, "Driver for channel '%s' does not support indication %u, emulating it\n", ast_channel_name(chan), condition);
  4130. res = ast_playtones_start(chan, 0, ts->data, 1);
  4131. if (!res) {
  4132. ast_test_suite_event_notify("RINGING_INBAND",
  4133. "Channel: %s\r\n",
  4134. ast_channel_name(chan));
  4135. }
  4136. ts = ast_tone_zone_sound_unref(ts);
  4137. }
  4138. if (res) {
  4139. /* not handled */
  4140. ast_log(LOG_WARNING, "Unable to handle indication %u for '%s'\n", condition, ast_channel_name(chan));
  4141. }
  4142. return res;
  4143. }
  4144. int ast_indicate_data(struct ast_channel *chan, int _condition, const void *data, size_t datalen)
  4145. {
  4146. int res;
  4147. /* this frame is used by framehooks. if it is set, we must free it at the end of this function */
  4148. struct ast_frame *awesome_frame = NULL;
  4149. ast_channel_lock(chan);
  4150. /* Don't bother if the channel is about to go away, anyway. */
  4151. if ((ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE)
  4152. || (ast_check_hangup(chan) && !ast_channel_is_leaving_bridge(chan)))
  4153. && _condition != AST_CONTROL_MASQUERADE_NOTIFY) {
  4154. res = -1;
  4155. goto indicate_cleanup;
  4156. }
  4157. if (!ast_framehook_list_is_empty(ast_channel_framehooks(chan))) {
  4158. /* Do framehooks now, do it, go, go now */
  4159. struct ast_frame frame = {
  4160. .frametype = AST_FRAME_CONTROL,
  4161. .subclass.integer = _condition,
  4162. .data.ptr = (void *) data, /* this cast from const is only okay because we do the ast_frdup below */
  4163. .datalen = datalen
  4164. };
  4165. /* we have now committed to freeing this frame */
  4166. awesome_frame = ast_frdup(&frame);
  4167. /* who knows what we will get back! the anticipation is killing me. */
  4168. awesome_frame = ast_framehook_list_write_event(ast_channel_framehooks(chan),
  4169. awesome_frame);
  4170. if (!awesome_frame
  4171. || awesome_frame->frametype != AST_FRAME_CONTROL) {
  4172. res = 0;
  4173. goto indicate_cleanup;
  4174. }
  4175. _condition = awesome_frame->subclass.integer;
  4176. data = awesome_frame->data.ptr;
  4177. datalen = awesome_frame->datalen;
  4178. }
  4179. res = indicate_data_internal(chan, _condition, data, datalen);
  4180. indicate_cleanup:
  4181. ast_channel_unlock(chan);
  4182. if (awesome_frame) {
  4183. ast_frfree(awesome_frame);
  4184. }
  4185. return res;
  4186. }
  4187. int ast_recvchar(struct ast_channel *chan, int timeout)
  4188. {
  4189. int c;
  4190. char *buf = ast_recvtext(chan, timeout);
  4191. if (buf == NULL)
  4192. return -1; /* error or timeout */
  4193. c = *(unsigned char *)buf;
  4194. ast_free(buf);
  4195. return c;
  4196. }
  4197. char *ast_recvtext(struct ast_channel *chan, int timeout)
  4198. {
  4199. int res;
  4200. char *buf = NULL;
  4201. struct timeval start = ast_tvnow();
  4202. int ms;
  4203. while ((ms = ast_remaining_ms(start, timeout))) {
  4204. struct ast_frame *f;
  4205. if (ast_check_hangup(chan)) {
  4206. break;
  4207. }
  4208. res = ast_waitfor(chan, ms);
  4209. if (res <= 0) {/* timeout or error */
  4210. break;
  4211. }
  4212. f = ast_read(chan);
  4213. if (f == NULL) {
  4214. break; /* no frame */
  4215. }
  4216. if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_HANGUP) {
  4217. ast_frfree(f);
  4218. break;
  4219. } else if (f->frametype == AST_FRAME_TEXT) { /* what we want */
  4220. buf = ast_strndup((char *) f->data.ptr, f->datalen); /* dup and break */
  4221. ast_frfree(f);
  4222. break;
  4223. }
  4224. ast_frfree(f);
  4225. }
  4226. return buf;
  4227. }
  4228. int ast_sendtext_data(struct ast_channel *chan, struct ast_msg_data *msg)
  4229. {
  4230. int res = 0;
  4231. const char *body = ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_BODY);
  4232. const char *content_type = ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_CONTENT_TYPE);
  4233. ast_channel_lock(chan);
  4234. /* Stop if we're a zombie or need a soft hangup */
  4235. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
  4236. ast_channel_unlock(chan);
  4237. return -1;
  4238. }
  4239. CHECK_BLOCKING(chan);
  4240. if (ast_channel_tech(chan)->write_text
  4241. && (ast_strlen_zero(content_type) || ast_begins_with(content_type, "text/"))
  4242. && (ast_format_cap_has_type(ast_channel_nativeformats(chan), AST_MEDIA_TYPE_TEXT))) {
  4243. struct ast_frame f;
  4244. /* T.140 payload does not include the null byte terminator */
  4245. size_t body_len = strlen(body);
  4246. /* Process as T.140 text (moved here from ast_sendtext() */
  4247. memset(&f, 0, sizeof(f));
  4248. f.src = "DIALPLAN";
  4249. f.subclass.format = ast_format_t140;
  4250. f.frametype = AST_FRAME_TEXT;
  4251. f.datalen = body_len;
  4252. f.mallocd = AST_MALLOCD_DATA;
  4253. f.data.ptr = ast_strdup(body);
  4254. if (f.data.ptr) {
  4255. res = ast_channel_tech(chan)->write_text(chan, &f);
  4256. } else {
  4257. res = -1;
  4258. }
  4259. ast_frfree(&f);
  4260. } else if ((ast_channel_tech(chan)->properties & AST_CHAN_TP_SEND_TEXT_DATA)
  4261. && ast_channel_tech(chan)->send_text_data) {
  4262. /* Send enhanced message to a channel driver that supports it */
  4263. ast_debug(1, "Sending TEXT_DATA from '%s' to %s:%s %s\n",
  4264. ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_FROM),
  4265. ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_TO),
  4266. ast_channel_name(chan), body);
  4267. res = ast_channel_tech(chan)->send_text_data(chan, msg);
  4268. } else if (ast_channel_tech(chan)->send_text
  4269. && (ast_strlen_zero(content_type) || ast_begins_with(content_type, "text/"))) {
  4270. /* Send the body of an enhanced message to a channel driver that supports only a char str */
  4271. ast_debug(1, "Sending TEXT to %s: %s\n", ast_channel_name(chan), body);
  4272. res = ast_channel_tech(chan)->send_text(chan, body);
  4273. } else {
  4274. ast_debug(1, "Channel technology does not support sending content type '%s' on channel '%s'\n",
  4275. S_OR(content_type, "text/plain"), ast_channel_name(chan));
  4276. res = -1;
  4277. }
  4278. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
  4279. ast_channel_unlock(chan);
  4280. return res;
  4281. }
  4282. int ast_sendtext(struct ast_channel *chan, const char *text)
  4283. {
  4284. struct ast_msg_data *msg;
  4285. int rc;
  4286. struct ast_msg_data_attribute attrs[] =
  4287. {
  4288. {
  4289. .type = AST_MSG_DATA_ATTR_BODY,
  4290. .value = (char *)text,
  4291. }
  4292. };
  4293. if (ast_strlen_zero(text)) {
  4294. return 0;
  4295. }
  4296. msg = ast_msg_data_alloc(AST_MSG_DATA_SOURCE_TYPE_UNKNOWN, attrs, ARRAY_LEN(attrs));
  4297. if (!msg) {
  4298. return -1;
  4299. }
  4300. rc = ast_sendtext_data(chan, msg);
  4301. ast_free(msg);
  4302. return rc;
  4303. }
  4304. int ast_senddigit_begin(struct ast_channel *chan, char digit)
  4305. {
  4306. /* Device does not support DTMF tones, lets fake
  4307. * it by doing our own generation. */
  4308. static const char * const dtmf_tones[] = {
  4309. "941+1336", /* 0 */
  4310. "697+1209", /* 1 */
  4311. "697+1336", /* 2 */
  4312. "697+1477", /* 3 */
  4313. "770+1209", /* 4 */
  4314. "770+1336", /* 5 */
  4315. "770+1477", /* 6 */
  4316. "852+1209", /* 7 */
  4317. "852+1336", /* 8 */
  4318. "852+1477", /* 9 */
  4319. "697+1633", /* A */
  4320. "770+1633", /* B */
  4321. "852+1633", /* C */
  4322. "941+1633", /* D */
  4323. "941+1209", /* * */
  4324. "941+1477" /* # */
  4325. };
  4326. if (!ast_channel_tech(chan)->send_digit_begin)
  4327. return 0;
  4328. ast_channel_lock(chan);
  4329. ast_channel_sending_dtmf_digit_set(chan, digit);
  4330. ast_channel_sending_dtmf_tv_set(chan, ast_tvnow());
  4331. ast_channel_unlock(chan);
  4332. if (!ast_channel_tech(chan)->send_digit_begin(chan, digit))
  4333. return 0;
  4334. if (digit >= '0' && digit <='9')
  4335. ast_playtones_start(chan, 0, dtmf_tones[digit-'0'], 0);
  4336. else if (digit >= 'A' && digit <= 'D')
  4337. ast_playtones_start(chan, 0, dtmf_tones[digit-'A'+10], 0);
  4338. else if (digit == '*')
  4339. ast_playtones_start(chan, 0, dtmf_tones[14], 0);
  4340. else if (digit == '#')
  4341. ast_playtones_start(chan, 0, dtmf_tones[15], 0);
  4342. else {
  4343. /* not handled */
  4344. ast_debug(1, "Unable to generate DTMF tone '%c' for '%s'\n", digit, ast_channel_name(chan));
  4345. }
  4346. return 0;
  4347. }
  4348. int ast_senddigit_end(struct ast_channel *chan, char digit, unsigned int duration)
  4349. {
  4350. int res = -1;
  4351. if (ast_channel_tech(chan)->send_digit_end)
  4352. res = ast_channel_tech(chan)->send_digit_end(chan, digit, duration);
  4353. ast_channel_lock(chan);
  4354. if (ast_channel_sending_dtmf_digit(chan) == digit) {
  4355. ast_channel_sending_dtmf_digit_set(chan, 0);
  4356. }
  4357. ast_channel_unlock(chan);
  4358. if (res && ast_channel_generator(chan))
  4359. ast_playtones_stop(chan);
  4360. return 0;
  4361. }
  4362. int ast_senddigit(struct ast_channel *chan, char digit, unsigned int duration)
  4363. {
  4364. if (duration < AST_DEFAULT_EMULATE_DTMF_DURATION) {
  4365. duration = AST_DEFAULT_EMULATE_DTMF_DURATION;
  4366. }
  4367. if (ast_channel_tech(chan)->send_digit_begin) {
  4368. ast_senddigit_begin(chan, digit);
  4369. ast_safe_sleep(chan, duration);
  4370. }
  4371. return ast_senddigit_end(chan, digit, duration);
  4372. }
  4373. int ast_senddigit_external(struct ast_channel *chan, char digit, unsigned int duration)
  4374. {
  4375. if (duration < AST_DEFAULT_EMULATE_DTMF_DURATION) {
  4376. duration = AST_DEFAULT_EMULATE_DTMF_DURATION;
  4377. }
  4378. if (ast_channel_tech(chan)->send_digit_begin) {
  4379. ast_senddigit_begin(chan, digit);
  4380. usleep(duration * 1000);
  4381. }
  4382. return ast_senddigit_end(chan, digit, duration);
  4383. }
  4384. int ast_prod(struct ast_channel *chan)
  4385. {
  4386. struct ast_frame a = { AST_FRAME_VOICE };
  4387. char nothing[128];
  4388. /* Send an empty audio frame to get things moving */
  4389. if (ast_channel_state(chan) != AST_STATE_UP) {
  4390. ast_debug(1, "Prodding channel '%s'\n", ast_channel_name(chan));
  4391. a.subclass.format = ast_channel_rawwriteformat(chan);
  4392. a.data.ptr = nothing + AST_FRIENDLY_OFFSET;
  4393. a.src = "ast_prod"; /* this better match check in ast_write */
  4394. if (ast_write(chan, &a))
  4395. ast_log(LOG_WARNING, "Prodding channel '%s' failed\n", ast_channel_name(chan));
  4396. }
  4397. return 0;
  4398. }
  4399. int ast_write_video(struct ast_channel *chan, struct ast_frame *fr)
  4400. {
  4401. int res;
  4402. if (!ast_channel_tech(chan)->write_video)
  4403. return 0;
  4404. res = ast_write(chan, fr);
  4405. if (!res)
  4406. res = 1;
  4407. return res;
  4408. }
  4409. struct plc_ds {
  4410. /* A buffer in which to store SLIN PLC
  4411. * samples generated by the generic PLC
  4412. * functionality in plc.c
  4413. */
  4414. int16_t *samples_buf;
  4415. /* The current number of samples in the
  4416. * samples_buf
  4417. */
  4418. size_t num_samples;
  4419. plc_state_t plc_state;
  4420. };
  4421. static void plc_ds_destroy(void *data)
  4422. {
  4423. struct plc_ds *plc = data;
  4424. ast_free(plc->samples_buf);
  4425. ast_free(plc);
  4426. }
  4427. static const struct ast_datastore_info plc_ds_info = {
  4428. .type = "plc",
  4429. .destroy = plc_ds_destroy,
  4430. };
  4431. static void adjust_frame_for_plc(struct ast_channel *chan, struct ast_frame *frame, struct ast_datastore *datastore)
  4432. {
  4433. int num_new_samples = frame->samples;
  4434. struct plc_ds *plc = datastore->data;
  4435. /* As a general note, let me explain the somewhat odd calculations used when taking
  4436. * the frame offset into account here. According to documentation in frame.h, the frame's
  4437. * offset field indicates the number of bytes that the audio is offset. The plc->samples_buf
  4438. * is not an array of bytes, but rather an array of 16-bit integers since it holds SLIN
  4439. * samples. So I had two choices to make here with the offset.
  4440. *
  4441. * 1. Make the offset AST_FRIENDLY_OFFSET bytes. The main downside for this is that
  4442. * I can't just add AST_FRIENDLY_OFFSET to the plc->samples_buf and have the pointer
  4443. * arithmetic come out right. I would have to do some odd casting or division for this to
  4444. * work as I wanted.
  4445. * 2. Make the offset AST_FRIENDLY_OFFSET * 2 bytes. This allows the pointer arithmetic
  4446. * to work out better with the plc->samples_buf. The downside here is that the buffer's
  4447. * allocation contains an extra 64 bytes of unused space.
  4448. *
  4449. * I decided to go with option 2. This is why in the calloc statement and the statement that
  4450. * sets the frame's offset, AST_FRIENDLY_OFFSET is multiplied by 2.
  4451. */
  4452. /* If this audio frame has no samples to fill in, ignore it */
  4453. if (!num_new_samples) {
  4454. return;
  4455. }
  4456. /* First, we need to be sure that our buffer is large enough to accomodate
  4457. * the samples we need to fill in. This will likely only occur on the first
  4458. * frame we write.
  4459. */
  4460. if (plc->num_samples < num_new_samples) {
  4461. ast_free(plc->samples_buf);
  4462. plc->samples_buf = ast_calloc(1, (num_new_samples * sizeof(*plc->samples_buf)) + (AST_FRIENDLY_OFFSET * 2));
  4463. if (!plc->samples_buf) {
  4464. ast_channel_datastore_remove(chan, datastore);
  4465. ast_datastore_free(datastore);
  4466. return;
  4467. }
  4468. plc->num_samples = num_new_samples;
  4469. }
  4470. if (frame->datalen == 0) {
  4471. plc_fillin(&plc->plc_state, plc->samples_buf + AST_FRIENDLY_OFFSET, frame->samples);
  4472. frame->data.ptr = plc->samples_buf + AST_FRIENDLY_OFFSET;
  4473. frame->datalen = num_new_samples * 2;
  4474. frame->offset = AST_FRIENDLY_OFFSET * 2;
  4475. } else {
  4476. plc_rx(&plc->plc_state, frame->data.ptr, frame->samples);
  4477. }
  4478. }
  4479. static void apply_plc(struct ast_channel *chan, struct ast_frame *frame)
  4480. {
  4481. struct ast_datastore *datastore;
  4482. struct plc_ds *plc;
  4483. datastore = ast_channel_datastore_find(chan, &plc_ds_info, NULL);
  4484. if (datastore) {
  4485. plc = datastore->data;
  4486. adjust_frame_for_plc(chan, frame, datastore);
  4487. return;
  4488. }
  4489. datastore = ast_datastore_alloc(&plc_ds_info, NULL);
  4490. if (!datastore) {
  4491. return;
  4492. }
  4493. plc = ast_calloc(1, sizeof(*plc));
  4494. if (!plc) {
  4495. ast_datastore_free(datastore);
  4496. return;
  4497. }
  4498. datastore->data = plc;
  4499. ast_channel_datastore_add(chan, datastore);
  4500. adjust_frame_for_plc(chan, frame, datastore);
  4501. }
  4502. int ast_write(struct ast_channel *chan, struct ast_frame *fr)
  4503. {
  4504. int res = -1;
  4505. struct ast_frame *f = NULL;
  4506. int count = 0;
  4507. int hooked = 0;
  4508. /*Deadlock avoidance*/
  4509. while(ast_channel_trylock(chan)) {
  4510. /*cannot goto done since the channel is not locked*/
  4511. if(count++ > 10) {
  4512. ast_debug(1, "Deadlock avoided for write to channel '%s'\n", ast_channel_name(chan));
  4513. return 0;
  4514. }
  4515. usleep(1);
  4516. }
  4517. /* Stop if we're a zombie or need a soft hangup */
  4518. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan))
  4519. goto done;
  4520. /* Perform the framehook write event here. After the frame enters the framehook list
  4521. * there is no telling what will happen, how awesome is that!!! */
  4522. if (!(fr = ast_framehook_list_write_event(ast_channel_framehooks(chan), fr))) {
  4523. res = 0;
  4524. goto done;
  4525. }
  4526. if (ast_channel_generatordata(chan) && (!fr->src || strcasecmp(fr->src, "ast_prod"))) {
  4527. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT)) {
  4528. ast_deactivate_generator(chan);
  4529. } else {
  4530. if (fr->frametype == AST_FRAME_DTMF_END) {
  4531. /* There is a generator running while we're in the middle of a digit.
  4532. * It's probably inband DTMF, so go ahead and pass it so it can
  4533. * stop the generator */
  4534. ast_channel_unlock(chan);
  4535. res = ast_senddigit_end(chan, fr->subclass.integer, fr->len);
  4536. ast_channel_lock(chan);
  4537. } else if (fr->frametype == AST_FRAME_CONTROL
  4538. && fr->subclass.integer == AST_CONTROL_UNHOLD) {
  4539. /*
  4540. * This is a side case where Echo is basically being called
  4541. * and the person put themselves on hold and took themselves
  4542. * off hold.
  4543. */
  4544. indicate_data_internal(chan, fr->subclass.integer, fr->data.ptr,
  4545. fr->datalen);
  4546. }
  4547. res = 0; /* XXX explain, why 0 ? */
  4548. goto done;
  4549. }
  4550. }
  4551. /* High bit prints debugging */
  4552. if (ast_channel_fout(chan) & DEBUGCHAN_FLAG)
  4553. ast_frame_dump(ast_channel_name(chan), fr, ">>");
  4554. switch (fr->frametype) {
  4555. case AST_FRAME_CONTROL:
  4556. indicate_data_internal(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
  4557. res = 0;
  4558. break;
  4559. case AST_FRAME_DTMF_BEGIN:
  4560. if (ast_channel_audiohooks(chan)) {
  4561. struct ast_frame *old_frame = fr;
  4562. fr = ast_audiohook_write_list(chan, ast_channel_audiohooks(chan), AST_AUDIOHOOK_DIRECTION_WRITE, fr);
  4563. if (old_frame != fr)
  4564. f = fr;
  4565. }
  4566. send_dtmf_begin_event(chan, DTMF_SENT, fr->subclass.integer);
  4567. ast_channel_unlock(chan);
  4568. res = ast_senddigit_begin(chan, fr->subclass.integer);
  4569. ast_channel_lock(chan);
  4570. break;
  4571. case AST_FRAME_DTMF_END:
  4572. if (ast_channel_audiohooks(chan)) {
  4573. struct ast_frame *new_frame = fr;
  4574. new_frame = ast_audiohook_write_list(chan, ast_channel_audiohooks(chan), AST_AUDIOHOOK_DIRECTION_WRITE, fr);
  4575. if (new_frame != fr) {
  4576. ast_frfree(new_frame);
  4577. }
  4578. }
  4579. send_dtmf_end_event(chan, DTMF_SENT, fr->subclass.integer, fr->len);
  4580. ast_channel_unlock(chan);
  4581. res = ast_senddigit_end(chan, fr->subclass.integer, fr->len);
  4582. ast_channel_lock(chan);
  4583. break;
  4584. case AST_FRAME_TEXT:
  4585. CHECK_BLOCKING(chan);
  4586. if (ast_format_cmp(fr->subclass.format, ast_format_t140) == AST_FORMAT_CMP_EQUAL) {
  4587. res = (ast_channel_tech(chan)->write_text == NULL) ? 0 :
  4588. ast_channel_tech(chan)->write_text(chan, fr);
  4589. } else {
  4590. res = (ast_channel_tech(chan)->send_text == NULL) ? 0 :
  4591. ast_channel_tech(chan)->send_text(chan, (char *) fr->data.ptr);
  4592. }
  4593. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
  4594. break;
  4595. case AST_FRAME_HTML:
  4596. CHECK_BLOCKING(chan);
  4597. res = (ast_channel_tech(chan)->send_html == NULL) ? 0 :
  4598. ast_channel_tech(chan)->send_html(chan, fr->subclass.integer, (char *) fr->data.ptr, fr->datalen);
  4599. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
  4600. break;
  4601. case AST_FRAME_VIDEO:
  4602. /* XXX Handle translation of video codecs one day XXX */
  4603. CHECK_BLOCKING(chan);
  4604. res = (ast_channel_tech(chan)->write_video == NULL) ? 0 :
  4605. ast_channel_tech(chan)->write_video(chan, fr);
  4606. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
  4607. break;
  4608. case AST_FRAME_MODEM:
  4609. CHECK_BLOCKING(chan);
  4610. res = (ast_channel_tech(chan)->write == NULL) ? 0 :
  4611. ast_channel_tech(chan)->write(chan, fr);
  4612. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
  4613. break;
  4614. case AST_FRAME_VOICE:
  4615. if (ast_channel_tech(chan)->write == NULL)
  4616. break; /*! \todo XXX should return 0 maybe ? */
  4617. if (ast_opt_generic_plc && ast_format_cmp(fr->subclass.format, ast_format_slin) == AST_FORMAT_CMP_EQUAL) {
  4618. apply_plc(chan, fr);
  4619. }
  4620. f = fr;
  4621. /*
  4622. * Send frame to audiohooks if present, if frametype is linear (else, later as per
  4623. * previous behavior)
  4624. */
  4625. if (ast_channel_audiohooks(chan)) {
  4626. if (ast_format_cache_is_slinear(fr->subclass.format)) {
  4627. hooked = 1;
  4628. f = ast_audiohook_write_list(chan, ast_channel_audiohooks(chan), AST_AUDIOHOOK_DIRECTION_WRITE, fr);
  4629. }
  4630. }
  4631. /* If the frame is in the raw write format, then it's easy... just use the frame - otherwise we will have to translate */
  4632. if (ast_format_cmp(fr->subclass.format, ast_channel_rawwriteformat(chan)) != AST_FORMAT_CMP_EQUAL) {
  4633. if (ast_format_cmp(ast_channel_writeformat(chan), fr->subclass.format) != AST_FORMAT_CMP_EQUAL) {
  4634. struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  4635. /*
  4636. * We are not setup to write this frame. Things may have changed
  4637. * on the peer side of the world and we try to adjust the format to
  4638. * make it compatible again. However, bad things can happen if we
  4639. * cannot setup a new translation path. Problems range from no
  4640. * audio, one-way audio, to garbled audio. The best we can do is
  4641. * request the call to hangup since we could not make it compatible.
  4642. *
  4643. * Being continuously spammed by this message likely indicates a
  4644. * problem with the peer because it cannot make up its mind about
  4645. * which format to use.
  4646. */
  4647. ast_debug(1, "Channel %s changing write format from %s to %s, native formats %s\n",
  4648. ast_channel_name(chan),
  4649. ast_format_get_name(ast_channel_writeformat(chan)),
  4650. ast_format_get_name(fr->subclass.format),
  4651. ast_format_cap_get_names(ast_channel_nativeformats(chan), &codec_buf));
  4652. if (ast_set_write_format(chan, fr->subclass.format)) {
  4653. /* Could not handle the new write format. Induce a hangup. */
  4654. break;
  4655. }
  4656. }
  4657. if (ast_channel_writetrans(chan)) {
  4658. struct ast_frame *trans_frame = ast_translate(ast_channel_writetrans(chan), f, 0);
  4659. if (trans_frame != f && f != fr) {
  4660. /*
  4661. * If translate gives us a new frame and so did the audio
  4662. * hook then we need to free the one from the audio hook.
  4663. */
  4664. ast_frfree(f);
  4665. }
  4666. f = trans_frame;
  4667. }
  4668. }
  4669. if (!f) {
  4670. res = 0;
  4671. break;
  4672. }
  4673. if (ast_channel_audiohooks(chan) && !hooked) {
  4674. struct ast_frame *prev = NULL, *new_frame, *cur, *dup;
  4675. int freeoldlist = 0;
  4676. if (f != fr) {
  4677. freeoldlist = 1;
  4678. }
  4679. /* Since ast_audiohook_write may return a new frame, and the cur frame is
  4680. * an item in a list of frames, create a new list adding each cur frame back to it
  4681. * regardless if the cur frame changes or not. */
  4682. for (cur = f; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
  4683. new_frame = ast_audiohook_write_list(chan, ast_channel_audiohooks(chan), AST_AUDIOHOOK_DIRECTION_WRITE, cur);
  4684. /* if this frame is different than cur, preserve the end of the list,
  4685. * free the old frames, and set cur to be the new frame */
  4686. if (new_frame != cur) {
  4687. /* doing an ast_frisolate here seems silly, but we are not guaranteed the new_frame
  4688. * isn't part of local storage, meaning if ast_audiohook_write is called multiple
  4689. * times it may override the previous frame we got from it unless we dup it */
  4690. if ((dup = ast_frisolate(new_frame))) {
  4691. AST_LIST_NEXT(dup, frame_list) = AST_LIST_NEXT(cur, frame_list);
  4692. if (freeoldlist) {
  4693. AST_LIST_NEXT(cur, frame_list) = NULL;
  4694. ast_frfree(cur);
  4695. }
  4696. if (new_frame != dup) {
  4697. ast_frfree(new_frame);
  4698. }
  4699. cur = dup;
  4700. }
  4701. }
  4702. /* now, regardless if cur is new or not, add it to the new list,
  4703. * if the new list has not started, cur will become the first item. */
  4704. if (prev) {
  4705. AST_LIST_NEXT(prev, frame_list) = cur;
  4706. } else {
  4707. f = cur; /* set f to be the beginning of our new list */
  4708. }
  4709. prev = cur;
  4710. }
  4711. }
  4712. /* If Monitor is running on this channel, then we have to write frames out there too */
  4713. /* the translator on chan->writetrans may have returned multiple frames
  4714. from the single frame we passed in; if so, feed each one of them to the
  4715. monitor */
  4716. if (ast_channel_monitor(chan) && ast_channel_monitor(chan)->write_stream) {
  4717. struct ast_frame *cur;
  4718. for (cur = f; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
  4719. /* XXX must explain this code */
  4720. #ifndef MONITOR_CONSTANT_DELAY
  4721. int jump = ast_channel_insmpl(chan) - ast_channel_outsmpl(chan) - 4 * cur->samples;
  4722. if (jump >= 0) {
  4723. jump = calc_monitor_jump((ast_channel_insmpl(chan) - ast_channel_outsmpl(chan)),
  4724. ast_format_get_sample_rate(f->subclass.format),
  4725. ast_format_get_sample_rate(ast_channel_monitor(chan)->write_stream->fmt->format));
  4726. if (ast_seekstream(ast_channel_monitor(chan)->write_stream, jump, SEEK_FORCECUR) == -1) {
  4727. ast_log(LOG_WARNING, "Failed to perform seek in monitoring write stream, synchronization between the files may be broken\n");
  4728. }
  4729. ast_channel_outsmpl_set(chan, ast_channel_outsmpl(chan) + (ast_channel_insmpl(chan) - ast_channel_outsmpl(chan)) + cur->samples);
  4730. } else {
  4731. ast_channel_outsmpl_set(chan, ast_channel_outsmpl(chan) + cur->samples);
  4732. }
  4733. #else
  4734. int jump = calc_monitor_jump((ast_channel_insmpl(chan) - ast_channel_outsmpl(chan)),
  4735. ast_format_get_sample_rate(f->subclass.format),
  4736. ast_format_get_sample_rate(ast_channel_monitor(chan)->write_stream->fmt->format));
  4737. if (jump - MONITOR_DELAY >= 0) {
  4738. if (ast_seekstream(ast_channel_monitor(chan)->write_stream, jump - cur->samples, SEEK_FORCECUR) == -1) {
  4739. ast_log(LOG_WARNING, "Failed to perform seek in monitoring write stream, synchronization between the files may be broken\n");
  4740. }
  4741. ast_channel_outsmpl_set(chan, ast_channel_outsmpl(chan) + ast_channel_insmpl(chan) - ast_channel_outsmpl(chan));
  4742. } else {
  4743. ast_channel_outsmpl_set(chan, ast_channel_outsmpl(chan) + cur->samples);
  4744. }
  4745. #endif
  4746. if (ast_channel_monitor(chan)->state == AST_MONITOR_RUNNING) {
  4747. if (ast_writestream(ast_channel_monitor(chan)->write_stream, cur) < 0)
  4748. ast_log(LOG_WARNING, "Failed to write data to channel monitor write stream\n");
  4749. }
  4750. }
  4751. }
  4752. /* the translator on chan->writetrans may have returned multiple frames
  4753. from the single frame we passed in; if so, feed each one of them to the
  4754. channel, freeing each one after it has been written */
  4755. CHECK_BLOCKING(chan);
  4756. if ((f != fr) && AST_LIST_NEXT(f, frame_list)) {
  4757. struct ast_frame *cur, *next = NULL;
  4758. unsigned int skip = 0;
  4759. cur = f;
  4760. while (cur) {
  4761. next = AST_LIST_NEXT(cur, frame_list);
  4762. AST_LIST_NEXT(cur, frame_list) = NULL;
  4763. if (!skip) {
  4764. if ((res = ast_channel_tech(chan)->write(chan, cur)) < 0) {
  4765. ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
  4766. skip = 1;
  4767. } else if (next) {
  4768. /* don't do this for the last frame in the list,
  4769. as the code outside the loop will do it once
  4770. */
  4771. ast_channel_fout_set(chan, FRAMECOUNT_INC(ast_channel_fout(chan)));
  4772. }
  4773. }
  4774. ast_frfree(cur);
  4775. cur = next;
  4776. }
  4777. /* reset f so the code below doesn't attempt to free it */
  4778. f = NULL;
  4779. } else {
  4780. res = ast_channel_tech(chan)->write(chan, f);
  4781. }
  4782. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
  4783. break;
  4784. case AST_FRAME_NULL:
  4785. case AST_FRAME_IAX:
  4786. /* Ignore these */
  4787. res = 0;
  4788. break;
  4789. default:
  4790. /* At this point, fr is the incoming frame and f is NULL. Channels do
  4791. * not expect to get NULL as a frame pointer and will segfault. Hence,
  4792. * we output the original frame passed in. */
  4793. CHECK_BLOCKING(chan);
  4794. res = ast_channel_tech(chan)->write(chan, fr);
  4795. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
  4796. break;
  4797. }
  4798. if (f && f != fr)
  4799. ast_frfree(f);
  4800. /* Consider a write failure to force a soft hangup */
  4801. if (res < 0) {
  4802. ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
  4803. } else {
  4804. ast_channel_fout_set(chan, FRAMECOUNT_INC(ast_channel_fout(chan)));
  4805. }
  4806. done:
  4807. if (ast_channel_audiohooks(chan) && ast_audiohook_write_list_empty(ast_channel_audiohooks(chan))) {
  4808. /* The list gets recreated if audiohooks are added again later */
  4809. ast_audiohook_detach_list(ast_channel_audiohooks(chan));
  4810. ast_channel_audiohooks_set(chan, NULL);
  4811. }
  4812. ast_channel_unlock(chan);
  4813. return res;
  4814. }
  4815. int ast_set_read_format_path(struct ast_channel *chan, struct ast_format *raw_format, struct ast_format *core_format)
  4816. {
  4817. struct ast_trans_pvt *trans_old;
  4818. struct ast_trans_pvt *trans_new;
  4819. if (ast_format_cmp(ast_channel_rawreadformat(chan), raw_format) == AST_FORMAT_CMP_EQUAL
  4820. && ast_format_cmp(ast_channel_readformat(chan), core_format) == AST_FORMAT_CMP_EQUAL) {
  4821. /* Nothing to setup */
  4822. return 0;
  4823. }
  4824. ast_debug(1, "Channel %s setting read format path: %s -> %s\n",
  4825. ast_channel_name(chan),
  4826. ast_format_get_name(raw_format),
  4827. ast_format_get_name(core_format));
  4828. /* Setup new translation path. */
  4829. if (ast_format_cmp(raw_format, core_format) != AST_FORMAT_CMP_EQUAL) {
  4830. trans_new = ast_translator_build_path(core_format, raw_format);
  4831. if (!trans_new) {
  4832. return -1;
  4833. }
  4834. } else {
  4835. /* No translation needed. */
  4836. trans_new = NULL;
  4837. }
  4838. trans_old = ast_channel_readtrans(chan);
  4839. if (trans_old) {
  4840. ast_translator_free_path(trans_old);
  4841. }
  4842. ast_channel_readtrans_set(chan, trans_new);
  4843. ast_channel_set_rawreadformat(chan, raw_format);
  4844. ast_channel_set_readformat(chan, core_format);
  4845. return 0;
  4846. }
  4847. int ast_set_write_format_path(struct ast_channel *chan, struct ast_format *core_format, struct ast_format *raw_format)
  4848. {
  4849. struct ast_trans_pvt *trans_old;
  4850. struct ast_trans_pvt *trans_new;
  4851. if (ast_format_cmp(ast_channel_rawwriteformat(chan), raw_format) == AST_FORMAT_CMP_EQUAL
  4852. && ast_format_cmp(ast_channel_writeformat(chan), core_format) == AST_FORMAT_CMP_EQUAL) {
  4853. /* Nothing to setup */
  4854. return 0;
  4855. }
  4856. ast_debug(1, "Channel %s setting write format path: %s -> %s\n",
  4857. ast_channel_name(chan),
  4858. ast_format_get_name(core_format),
  4859. ast_format_get_name(raw_format));
  4860. /* Setup new translation path. */
  4861. if (ast_format_cmp(raw_format, core_format) != AST_FORMAT_CMP_EQUAL) {
  4862. trans_new = ast_translator_build_path(raw_format, core_format);
  4863. if (!trans_new) {
  4864. return -1;
  4865. }
  4866. } else {
  4867. /* No translation needed. */
  4868. trans_new = NULL;
  4869. }
  4870. trans_old = ast_channel_writetrans(chan);
  4871. if (trans_old) {
  4872. ast_translator_free_path(trans_old);
  4873. }
  4874. ast_channel_writetrans_set(chan, trans_new);
  4875. ast_channel_set_rawwriteformat(chan, raw_format);
  4876. ast_channel_set_writeformat(chan, core_format);
  4877. return 0;
  4878. }
  4879. struct set_format_access {
  4880. const char *direction;
  4881. struct ast_trans_pvt *(*get_trans)(const struct ast_channel *chan);
  4882. void (*set_trans)(struct ast_channel *chan, struct ast_trans_pvt *value);
  4883. struct ast_format *(*get_format)(struct ast_channel *chan);
  4884. void (*set_format)(struct ast_channel *chan, struct ast_format *format);
  4885. struct ast_format *(*get_rawformat)(struct ast_channel *chan);
  4886. void (*set_rawformat)(struct ast_channel *chan, struct ast_format *format);
  4887. int setoption;
  4888. };
  4889. static const struct set_format_access set_format_access_read = {
  4890. .direction = "read",
  4891. .get_trans = ast_channel_readtrans,
  4892. .set_trans = ast_channel_readtrans_set,
  4893. .get_format = ast_channel_readformat,
  4894. .set_format = ast_channel_set_readformat,
  4895. .get_rawformat = ast_channel_rawreadformat,
  4896. .set_rawformat = ast_channel_set_rawreadformat,
  4897. .setoption = AST_OPTION_FORMAT_READ,
  4898. };
  4899. static const struct set_format_access set_format_access_write = {
  4900. .direction = "write",
  4901. .get_trans = ast_channel_writetrans,
  4902. .set_trans = ast_channel_writetrans_set,
  4903. .get_format = ast_channel_writeformat,
  4904. .set_format = ast_channel_set_writeformat,
  4905. .get_rawformat = ast_channel_rawwriteformat,
  4906. .set_rawformat = ast_channel_set_rawwriteformat,
  4907. .setoption = AST_OPTION_FORMAT_WRITE,
  4908. };
  4909. static int set_format(struct ast_channel *chan, struct ast_format_cap *cap_set, const int direction)
  4910. {
  4911. struct ast_trans_pvt *trans_pvt;
  4912. struct ast_format_cap *cap_native;
  4913. const struct set_format_access *access;
  4914. struct ast_format *rawformat;
  4915. struct ast_format *format;
  4916. RAII_VAR(struct ast_format *, best_set_fmt, NULL, ao2_cleanup);
  4917. RAII_VAR(struct ast_format *, best_native_fmt, NULL, ao2_cleanup);
  4918. int res;
  4919. if (!direction) {
  4920. /* reading */
  4921. access = &set_format_access_read;
  4922. } else {
  4923. /* writing */
  4924. access = &set_format_access_write;
  4925. }
  4926. best_set_fmt = ast_format_cap_get_best_by_type(cap_set, AST_MEDIA_TYPE_AUDIO);
  4927. if (!best_set_fmt) {
  4928. /*
  4929. * Not setting any audio formats?
  4930. * Assume a call without any sounds (video, text)
  4931. */
  4932. return 0;
  4933. }
  4934. /* See if the underlying channel driver is capable of performing transcoding for us */
  4935. res = ast_channel_setoption(chan, access->setoption,
  4936. &best_set_fmt, sizeof(best_set_fmt), 0);
  4937. if (!res) {
  4938. ast_debug(1, "Channel driver natively set channel %s to %s format %s\n",
  4939. ast_channel_name(chan), access->direction, ast_format_get_name(best_set_fmt));
  4940. ast_channel_lock(chan);
  4941. cap_native = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  4942. if (!cap_native
  4943. || ast_format_cap_append(cap_native, best_set_fmt, 0)) {
  4944. ast_channel_unlock(chan);
  4945. ao2_cleanup(cap_native);
  4946. return -1;
  4947. }
  4948. ast_channel_nativeformats_set(chan, cap_native);
  4949. ao2_cleanup(cap_native);
  4950. access->set_format(chan, best_set_fmt);
  4951. access->set_rawformat(chan, best_set_fmt);
  4952. trans_pvt = access->get_trans(chan);
  4953. if (trans_pvt) {
  4954. ast_translator_free_path(trans_pvt);
  4955. access->set_trans(chan, NULL);
  4956. }
  4957. ast_channel_unlock(chan);
  4958. /* If there is a generator on the channel, it needs to know about this
  4959. * change if it is the write format. */
  4960. if (direction && ast_channel_generatordata(chan)) {
  4961. generator_write_format_change(chan);
  4962. }
  4963. return 0;
  4964. }
  4965. ast_channel_lock(chan);
  4966. format = access->get_format(chan);
  4967. rawformat = access->get_rawformat(chan);
  4968. ast_assert(format != NULL);
  4969. ast_assert(rawformat != NULL);
  4970. cap_native = ast_channel_nativeformats(chan);
  4971. if (ast_format_cap_empty(cap_native)) {
  4972. ast_channel_unlock(chan);
  4973. ast_log(LOG_ERROR, "Unable to set format because channel %s supports no formats\n",
  4974. ast_channel_name(chan));
  4975. return -1;
  4976. }
  4977. /* Find a translation path from the native format to one of the desired formats */
  4978. if (!direction) {
  4979. /* reading */
  4980. res = ast_translator_best_choice(cap_set, cap_native, &best_set_fmt, &best_native_fmt);
  4981. } else {
  4982. /* writing */
  4983. res = ast_translator_best_choice(cap_native, cap_set, &best_native_fmt, &best_set_fmt);
  4984. }
  4985. if (res < 0) {
  4986. struct ast_str *codec_native = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  4987. struct ast_str *codec_set = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  4988. ast_format_cap_get_names(cap_native, &codec_native);
  4989. ast_channel_unlock(chan);
  4990. ast_format_cap_get_names(cap_set, &codec_set);
  4991. ast_log(LOG_WARNING, "Unable to find a codec translation path: %s -> %s\n",
  4992. ast_str_buffer(direction ? codec_set : codec_native),
  4993. ast_str_buffer(direction ? codec_native : codec_set));
  4994. return -1;
  4995. }
  4996. /* Now we have a good choice for both. */
  4997. if ((ast_format_cmp(rawformat, best_native_fmt) != AST_FORMAT_CMP_NOT_EQUAL) &&
  4998. (ast_format_cmp(format, best_set_fmt) != AST_FORMAT_CMP_NOT_EQUAL) &&
  4999. ((ast_format_cmp(rawformat, format) != AST_FORMAT_CMP_NOT_EQUAL) || access->get_trans(chan))) {
  5000. /* the channel is already in these formats, so nothing to do */
  5001. ast_channel_unlock(chan);
  5002. return 0;
  5003. }
  5004. /* Free any translation we have right now */
  5005. trans_pvt = access->get_trans(chan);
  5006. if (trans_pvt) {
  5007. ast_translator_free_path(trans_pvt);
  5008. access->set_trans(chan, NULL);
  5009. }
  5010. /* Build a translation path from the raw format to the desired format */
  5011. if (ast_format_cmp(best_set_fmt, best_native_fmt) != AST_FORMAT_CMP_NOT_EQUAL) {
  5012. /*
  5013. * If we were able to swap the native format to the format that
  5014. * has been requested, then there is no need to try to build
  5015. * a translation path.
  5016. */
  5017. res = 0;
  5018. } else {
  5019. if (!direction) {
  5020. /* reading */
  5021. trans_pvt = ast_translator_build_path(best_set_fmt, best_native_fmt);
  5022. } else {
  5023. /* writing */
  5024. trans_pvt = ast_translator_build_path(best_native_fmt, best_set_fmt);
  5025. }
  5026. access->set_trans(chan, trans_pvt);
  5027. res = trans_pvt ? 0 : -1;
  5028. }
  5029. if (!res) {
  5030. access->set_format(chan, best_set_fmt);
  5031. access->set_rawformat(chan, best_native_fmt);
  5032. ast_debug(1, "Channel %s setting %s format path: %s -> %s\n",
  5033. ast_channel_name(chan),
  5034. access->direction,
  5035. ast_format_get_name(direction ? best_set_fmt : best_native_fmt),
  5036. ast_format_get_name(direction ? best_native_fmt : best_set_fmt));
  5037. }
  5038. ast_channel_unlock(chan);
  5039. /* If there is a generator on the channel, it needs to know about this
  5040. * change if it is the write format. */
  5041. if (direction && ast_channel_generatordata(chan)) {
  5042. generator_write_format_change(chan);
  5043. }
  5044. return res;
  5045. }
  5046. int ast_set_read_format(struct ast_channel *chan, struct ast_format *format)
  5047. {
  5048. struct ast_format_cap *cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  5049. int res;
  5050. ast_assert(format != NULL);
  5051. if (!cap) {
  5052. return -1;
  5053. }
  5054. ast_format_cap_append(cap, format, 0);
  5055. res = set_format(chan, cap, 0);
  5056. ao2_cleanup(cap);
  5057. return res;
  5058. }
  5059. int ast_set_read_format_from_cap(struct ast_channel *chan, struct ast_format_cap *cap)
  5060. {
  5061. return set_format(chan, cap, 0);
  5062. }
  5063. int ast_set_write_format(struct ast_channel *chan, struct ast_format *format)
  5064. {
  5065. struct ast_format_cap *cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  5066. int res;
  5067. ast_assert(format != NULL);
  5068. if (!cap) {
  5069. return -1;
  5070. }
  5071. ast_format_cap_append(cap, format, 0);
  5072. res = set_format(chan, cap, 1);
  5073. ao2_cleanup(cap);
  5074. return res;
  5075. }
  5076. int ast_set_write_format_from_cap(struct ast_channel *chan, struct ast_format_cap *cap)
  5077. {
  5078. return set_format(chan, cap, 1);
  5079. }
  5080. const char *ast_channel_reason2str(int reason)
  5081. {
  5082. switch (reason) /* the following appear to be the only ones actually returned by request_and_dial */
  5083. {
  5084. case 0:
  5085. return "Call Failure (not BUSY, and not NO_ANSWER, maybe Circuit busy or down?)";
  5086. case AST_CONTROL_HANGUP:
  5087. return "Hangup";
  5088. case AST_CONTROL_RING:
  5089. return "Local Ring";
  5090. case AST_CONTROL_RINGING:
  5091. return "Remote end Ringing";
  5092. case AST_CONTROL_ANSWER:
  5093. return "Remote end has Answered";
  5094. case AST_CONTROL_BUSY:
  5095. return "Remote end is Busy";
  5096. case AST_CONTROL_CONGESTION:
  5097. return "Congestion (circuits busy)";
  5098. default:
  5099. return "Unknown Reason!!";
  5100. }
  5101. }
  5102. static void handle_cause(int cause, int *outstate)
  5103. {
  5104. if (outstate) {
  5105. /* compute error and return */
  5106. if (cause == AST_CAUSE_BUSY)
  5107. *outstate = AST_CONTROL_BUSY;
  5108. else if (cause == AST_CAUSE_CONGESTION)
  5109. *outstate = AST_CONTROL_CONGESTION;
  5110. else
  5111. *outstate = 0;
  5112. }
  5113. }
  5114. /*!
  5115. * \internal
  5116. * \brief Helper function to inherit info from parent channel.
  5117. *
  5118. * \param new_chan Channel inheriting information.
  5119. * \param parent Channel new_chan inherits information.
  5120. * \param orig Channel being replaced by the call forward channel.
  5121. *
  5122. * \return Nothing
  5123. */
  5124. static void call_forward_inherit(struct ast_channel *new_chan, struct ast_channel *parent, struct ast_channel *orig)
  5125. {
  5126. if (!ast_test_flag(ast_channel_flags(parent), AST_FLAG_ZOMBIE) && !ast_check_hangup(parent)) {
  5127. struct ast_party_redirecting redirecting;
  5128. /*
  5129. * The parent is not a ZOMBIE or hungup so update it with the
  5130. * original channel's redirecting information.
  5131. */
  5132. ast_party_redirecting_init(&redirecting);
  5133. ast_channel_lock(orig);
  5134. ast_party_redirecting_copy(&redirecting, ast_channel_redirecting(orig));
  5135. ast_channel_unlock(orig);
  5136. if (ast_channel_redirecting_sub(orig, parent, &redirecting, 0) &&
  5137. ast_channel_redirecting_macro(orig, parent, &redirecting, 1, 0)) {
  5138. ast_channel_update_redirecting(parent, &redirecting, NULL);
  5139. }
  5140. ast_party_redirecting_free(&redirecting);
  5141. }
  5142. /* Safely inherit variables and datastores from the parent channel. */
  5143. ast_channel_lock_both(parent, new_chan);
  5144. ast_channel_inherit_variables(parent, new_chan);
  5145. ast_channel_datastore_inherit(parent, new_chan);
  5146. ast_max_forwards_decrement(new_chan);
  5147. ast_channel_unlock(new_chan);
  5148. ast_channel_unlock(parent);
  5149. }
  5150. struct ast_channel *ast_call_forward(struct ast_channel *caller, struct ast_channel *orig, int *timeout, struct ast_format_cap *cap, struct outgoing_helper *oh, int *outstate)
  5151. {
  5152. char tmpchan[256];
  5153. char forwarder[AST_CHANNEL_NAME];
  5154. struct ast_channel *new_chan = NULL;
  5155. char *data, *type;
  5156. int cause = 0;
  5157. int res;
  5158. /* gather data and request the new forward channel */
  5159. ast_copy_string(tmpchan, ast_channel_call_forward(orig), sizeof(tmpchan));
  5160. ast_copy_string(forwarder, ast_channel_name(orig), sizeof(forwarder));
  5161. if ((data = strchr(tmpchan, '/'))) {
  5162. *data++ = '\0';
  5163. type = tmpchan;
  5164. } else {
  5165. const char *forward_context;
  5166. ast_channel_lock(orig);
  5167. forward_context = pbx_builtin_getvar_helper(orig, "FORWARD_CONTEXT");
  5168. snprintf(tmpchan, sizeof(tmpchan), "%s@%s", ast_channel_call_forward(orig), S_OR(forward_context, ast_channel_context(orig)));
  5169. ast_channel_unlock(orig);
  5170. data = tmpchan;
  5171. type = "Local";
  5172. }
  5173. if (!(new_chan = ast_request(type, cap, NULL, orig, data, &cause))) {
  5174. ast_log(LOG_NOTICE, "Unable to create channel for call forward to '%s/%s' (cause = %d)\n", type, data, cause);
  5175. handle_cause(cause, outstate);
  5176. ast_hangup(orig);
  5177. return NULL;
  5178. }
  5179. /* Copy/inherit important information into new channel */
  5180. if (oh) {
  5181. if (oh->vars) {
  5182. ast_channel_lock(new_chan);
  5183. ast_set_variables(new_chan, oh->vars);
  5184. ast_channel_unlock(new_chan);
  5185. }
  5186. if (oh->parent_channel) {
  5187. call_forward_inherit(new_chan, oh->parent_channel, orig);
  5188. }
  5189. if (!ast_strlen_zero(oh->account)) {
  5190. ast_channel_lock(new_chan);
  5191. ast_channel_stage_snapshot(new_chan);
  5192. ast_channel_accountcode_set(new_chan, oh->account);
  5193. ast_channel_peeraccount_set(new_chan, oh->account);
  5194. ast_channel_stage_snapshot_done(new_chan);
  5195. ast_channel_unlock(new_chan);
  5196. }
  5197. } else if (caller) { /* no outgoing helper so use caller if available */
  5198. call_forward_inherit(new_chan, caller, orig);
  5199. }
  5200. ast_channel_lock_both(orig, new_chan);
  5201. ast_channel_set_flag(new_chan, AST_FLAG_ORIGINATED);
  5202. pbx_builtin_setvar_helper(new_chan, "FORWARDERNAME", forwarder);
  5203. ast_party_connected_line_copy(ast_channel_connected(new_chan), ast_channel_connected(orig));
  5204. ast_party_redirecting_copy(ast_channel_redirecting(new_chan), ast_channel_redirecting(orig));
  5205. ast_channel_req_accountcodes(new_chan, orig, AST_CHANNEL_REQUESTOR_REPLACEMENT);
  5206. ast_channel_unlock(new_chan);
  5207. ast_channel_unlock(orig);
  5208. /* call new channel */
  5209. res = ast_call(new_chan, data, 0);
  5210. if (timeout) {
  5211. *timeout = res;
  5212. }
  5213. if (res) {
  5214. ast_log(LOG_NOTICE, "Unable to call forward to channel %s/%s\n", type, (char *)data);
  5215. ast_hangup(orig);
  5216. ast_hangup(new_chan);
  5217. return NULL;
  5218. }
  5219. ast_hangup(orig);
  5220. return new_chan;
  5221. }
  5222. struct ast_channel *__ast_request_and_dial(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *addr, int timeout, int *outstate, const char *cid_num, const char *cid_name, struct outgoing_helper *oh)
  5223. {
  5224. int dummy_outstate;
  5225. int cause = 0;
  5226. struct ast_channel *chan;
  5227. int res = 0;
  5228. int last_subclass = 0;
  5229. struct ast_party_connected_line connected;
  5230. if (outstate)
  5231. *outstate = 0;
  5232. else
  5233. outstate = &dummy_outstate; /* make outstate always a valid pointer */
  5234. chan = ast_request(type, cap, assignedids, requestor, addr, &cause);
  5235. if (!chan) {
  5236. ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, addr);
  5237. handle_cause(cause, outstate);
  5238. return NULL;
  5239. }
  5240. if (oh) {
  5241. if (oh->vars) {
  5242. ast_channel_lock(chan);
  5243. ast_set_variables(chan, oh->vars);
  5244. ast_channel_unlock(chan);
  5245. }
  5246. if (!ast_strlen_zero(oh->cid_num) && !ast_strlen_zero(oh->cid_name)) {
  5247. /*
  5248. * Use the oh values instead of the function parameters for the
  5249. * outgoing CallerID.
  5250. */
  5251. cid_num = oh->cid_num;
  5252. cid_name = oh->cid_name;
  5253. }
  5254. if (oh->parent_channel) {
  5255. /* Safely inherit variables and datastores from the parent channel. */
  5256. ast_channel_lock_both(oh->parent_channel, chan);
  5257. ast_channel_inherit_variables(oh->parent_channel, chan);
  5258. ast_channel_datastore_inherit(oh->parent_channel, chan);
  5259. ast_max_forwards_decrement(chan);
  5260. ast_channel_unlock(oh->parent_channel);
  5261. ast_channel_unlock(chan);
  5262. }
  5263. if (!ast_strlen_zero(oh->account)) {
  5264. ast_channel_lock(chan);
  5265. ast_channel_stage_snapshot(chan);
  5266. ast_channel_accountcode_set(chan, oh->account);
  5267. ast_channel_peeraccount_set(chan, oh->account);
  5268. ast_channel_stage_snapshot_done(chan);
  5269. ast_channel_unlock(chan);
  5270. }
  5271. }
  5272. /*
  5273. * I seems strange to set the CallerID on an outgoing call leg
  5274. * to whom we are calling, but this function's callers are doing
  5275. * various Originate methods. This call leg goes to the local
  5276. * user. Once the local user answers, the dialplan needs to be
  5277. * able to access the CallerID from the CALLERID function as if
  5278. * the local user had placed this call.
  5279. */
  5280. ast_set_callerid(chan, cid_num, cid_name, cid_num);
  5281. ast_channel_lock(chan);
  5282. ast_set_flag(ast_channel_flags(chan), AST_FLAG_ORIGINATED);
  5283. ast_channel_unlock(chan);
  5284. ast_party_connected_line_set_init(&connected, ast_channel_connected(chan));
  5285. if (cid_num) {
  5286. connected.id.number.valid = 1;
  5287. connected.id.number.str = (char *) cid_num;
  5288. connected.id.number.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
  5289. }
  5290. if (cid_name) {
  5291. connected.id.name.valid = 1;
  5292. connected.id.name.str = (char *) cid_name;
  5293. connected.id.name.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
  5294. }
  5295. ast_channel_set_connected_line(chan, &connected, NULL);
  5296. if (requestor) {
  5297. ast_channel_lock_both(chan, (struct ast_channel *) requestor);
  5298. ast_channel_req_accountcodes(chan, requestor, AST_CHANNEL_REQUESTOR_BRIDGE_PEER);
  5299. ast_channel_unlock(chan);
  5300. ast_channel_unlock((struct ast_channel *) requestor);
  5301. }
  5302. if (ast_call(chan, addr, 0)) { /* ast_call failed... */
  5303. ast_log(LOG_NOTICE, "Unable to call channel %s/%s\n", type, addr);
  5304. } else {
  5305. struct timeval start = ast_tvnow();
  5306. res = 1; /* mark success in case chan->_state is already AST_STATE_UP */
  5307. while (timeout && ast_channel_state(chan) != AST_STATE_UP) {
  5308. struct ast_frame *f;
  5309. int ms = ast_remaining_ms(start, timeout);
  5310. res = ast_waitfor(chan, ms);
  5311. if (res == 0) { /* timeout, treat it like ringing */
  5312. *outstate = AST_CONTROL_RINGING;
  5313. break;
  5314. }
  5315. if (res < 0) /* error or done */
  5316. break;
  5317. if (!ast_strlen_zero(ast_channel_call_forward(chan))) {
  5318. if (!(chan = ast_call_forward(NULL, chan, NULL, cap, oh, outstate))) {
  5319. return NULL;
  5320. }
  5321. continue;
  5322. }
  5323. f = ast_read(chan);
  5324. if (!f) {
  5325. *outstate = AST_CONTROL_HANGUP;
  5326. res = 0;
  5327. break;
  5328. }
  5329. if (f->frametype == AST_FRAME_CONTROL) {
  5330. switch (f->subclass.integer) {
  5331. case AST_CONTROL_RINGING: /* record but keep going */
  5332. *outstate = f->subclass.integer;
  5333. break;
  5334. case AST_CONTROL_BUSY:
  5335. *outstate = f->subclass.integer;
  5336. timeout = 0;
  5337. break;
  5338. case AST_CONTROL_INCOMPLETE:
  5339. *outstate = AST_CONTROL_CONGESTION;
  5340. timeout = 0;
  5341. break;
  5342. case AST_CONTROL_CONGESTION:
  5343. *outstate = f->subclass.integer;
  5344. timeout = 0;
  5345. break;
  5346. case AST_CONTROL_ANSWER:
  5347. *outstate = f->subclass.integer;
  5348. timeout = 0; /* trick to force exit from the while() */
  5349. break;
  5350. case AST_CONTROL_PVT_CAUSE_CODE:
  5351. ast_channel_hangupcause_hash_set(chan, f->data.ptr, f->datalen);
  5352. break;
  5353. case AST_CONTROL_PROGRESS:
  5354. if (oh && oh->connect_on_early_media) {
  5355. *outstate = f->subclass.integer;
  5356. timeout = 0; /* trick to force exit from the while() */
  5357. break;
  5358. }
  5359. /* Fallthrough */
  5360. /* Ignore these */
  5361. case AST_CONTROL_PROCEEDING:
  5362. case AST_CONTROL_HOLD:
  5363. case AST_CONTROL_UNHOLD:
  5364. case AST_CONTROL_VIDUPDATE:
  5365. case AST_CONTROL_SRCUPDATE:
  5366. case AST_CONTROL_SRCCHANGE:
  5367. case AST_CONTROL_CONNECTED_LINE:
  5368. case AST_CONTROL_REDIRECTING:
  5369. case AST_CONTROL_CC:
  5370. case -1: /* Ignore -- just stopping indications */
  5371. break;
  5372. default:
  5373. ast_log(LOG_NOTICE, "Don't know what to do with control frame %d\n", f->subclass.integer);
  5374. }
  5375. last_subclass = f->subclass.integer;
  5376. }
  5377. ast_frfree(f);
  5378. }
  5379. }
  5380. /* Final fixups */
  5381. if (oh) {
  5382. if (!ast_strlen_zero(oh->context))
  5383. ast_channel_context_set(chan, oh->context);
  5384. if (!ast_strlen_zero(oh->exten))
  5385. ast_channel_exten_set(chan, oh->exten);
  5386. if (oh->priority)
  5387. ast_channel_priority_set(chan, oh->priority);
  5388. }
  5389. if (ast_channel_state(chan) == AST_STATE_UP)
  5390. *outstate = AST_CONTROL_ANSWER;
  5391. if (res <= 0) {
  5392. ast_channel_lock(chan);
  5393. if (AST_CONTROL_RINGING == last_subclass) {
  5394. ast_channel_hangupcause_set(chan, AST_CAUSE_NO_ANSWER);
  5395. }
  5396. ast_channel_unlock(chan);
  5397. ast_hangup(chan);
  5398. chan = NULL;
  5399. }
  5400. return chan;
  5401. }
  5402. struct ast_channel *ast_request_and_dial(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *addr, int timeout, int *outstate, const char *cidnum, const char *cidname)
  5403. {
  5404. return __ast_request_and_dial(type, cap, assignedids, requestor, addr, timeout, outstate, cidnum, cidname, NULL);
  5405. }
  5406. static int set_security_requirements(const struct ast_channel *requestor, struct ast_channel *out)
  5407. {
  5408. int ops[2][2] = {
  5409. {AST_OPTION_SECURE_SIGNALING, 0},
  5410. {AST_OPTION_SECURE_MEDIA, 0},
  5411. };
  5412. int i;
  5413. struct ast_channel *r = (struct ast_channel *) requestor; /* UGLY */
  5414. struct ast_datastore *ds;
  5415. if (!requestor || !out) {
  5416. return 0;
  5417. }
  5418. ast_channel_lock(r);
  5419. if ((ds = ast_channel_datastore_find(r, &secure_call_info, NULL))) {
  5420. struct ast_secure_call_store *encrypt = ds->data;
  5421. ops[0][1] = encrypt->signaling;
  5422. ops[1][1] = encrypt->media;
  5423. } else {
  5424. ast_channel_unlock(r);
  5425. return 0;
  5426. }
  5427. ast_channel_unlock(r);
  5428. for (i = 0; i < 2; i++) {
  5429. if (ops[i][1]) {
  5430. if (ast_channel_setoption(out, ops[i][0], &ops[i][1], sizeof(ops[i][1]), 0)) {
  5431. /* We require a security feature, but the channel won't provide it */
  5432. return -1;
  5433. }
  5434. } else {
  5435. /* We don't care if we can't clear the option on a channel that doesn't support it */
  5436. ast_channel_setoption(out, ops[i][0], &ops[i][1], sizeof(ops[i][1]), 0);
  5437. }
  5438. }
  5439. return 0;
  5440. }
  5441. struct ast_channel *ast_request(const char *type, struct ast_format_cap *request_cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *addr, int *cause)
  5442. {
  5443. struct chanlist *chan;
  5444. struct ast_channel *c;
  5445. int res;
  5446. int foo;
  5447. if (!cause)
  5448. cause = &foo;
  5449. *cause = AST_CAUSE_NOTDEFINED;
  5450. if (AST_RWLIST_RDLOCK(&backends)) {
  5451. ast_log(LOG_WARNING, "Unable to lock technology backend list\n");
  5452. return NULL;
  5453. }
  5454. AST_RWLIST_TRAVERSE(&backends, chan, list) {
  5455. struct ast_format_cap *tmp_cap;
  5456. RAII_VAR(struct ast_format *, tmp_fmt, NULL, ao2_cleanup);
  5457. RAII_VAR(struct ast_format *, best_audio_fmt, NULL, ao2_cleanup);
  5458. struct ast_format_cap *joint_cap;
  5459. if (strcasecmp(type, chan->tech->type))
  5460. continue;
  5461. /* find the best audio format to use */
  5462. tmp_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  5463. if (tmp_cap) {
  5464. ast_format_cap_append_from_cap(tmp_cap, request_cap, AST_MEDIA_TYPE_AUDIO);
  5465. /* We have audio - is it possible to connect the various calls to each other?
  5466. (Avoid this check for calls without audio, like text+video calls)
  5467. */
  5468. res = ast_translator_best_choice(tmp_cap, chan->tech->capabilities, &tmp_fmt, &best_audio_fmt);
  5469. ao2_ref(tmp_cap, -1);
  5470. if (res < 0) {
  5471. struct ast_str *tech_codecs = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  5472. struct ast_str *request_codecs = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  5473. ast_log(LOG_WARNING, "No translator path exists for channel type %s (native %s) to %s\n", type,
  5474. ast_format_cap_get_names(chan->tech->capabilities, &tech_codecs),
  5475. ast_format_cap_get_names(request_cap, &request_codecs));
  5476. *cause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
  5477. AST_RWLIST_UNLOCK(&backends);
  5478. return NULL;
  5479. }
  5480. }
  5481. AST_RWLIST_UNLOCK(&backends);
  5482. if (!chan->tech->requester)
  5483. return NULL;
  5484. /* XXX Only the audio format calculated as being the best for translation
  5485. * purposes is used for the request. This is because we don't have the ability
  5486. * to signal to the initiator which one of their codecs that was offered is
  5487. * the one that was selected, particularly in a chain of Local channels.
  5488. */
  5489. joint_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  5490. if (!joint_cap) {
  5491. return NULL;
  5492. }
  5493. ast_format_cap_append_from_cap(joint_cap, request_cap, AST_MEDIA_TYPE_UNKNOWN);
  5494. ast_format_cap_remove_by_type(joint_cap, AST_MEDIA_TYPE_AUDIO);
  5495. ast_format_cap_append(joint_cap, best_audio_fmt, 0);
  5496. if (!(c = chan->tech->requester(type, joint_cap, assignedids, requestor, addr, cause))) {
  5497. ao2_ref(joint_cap, -1);
  5498. return NULL;
  5499. }
  5500. if (requestor) {
  5501. struct ast_callid *callid;
  5502. ast_channel_lock_both(c, (struct ast_channel *) requestor);
  5503. /* Set the newly created channel's callid to the same as the requestor. */
  5504. callid = ast_channel_callid(requestor);
  5505. if (callid) {
  5506. ast_channel_callid_set(c, callid);
  5507. callid = ast_callid_unref(callid);
  5508. }
  5509. ast_channel_unlock(c);
  5510. ast_channel_unlock((struct ast_channel *) requestor);
  5511. }
  5512. ao2_ref(joint_cap, -1);
  5513. if (set_security_requirements(requestor, c)) {
  5514. ast_log(LOG_WARNING, "Setting security requirements failed\n");
  5515. ast_hangup(c);
  5516. *cause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
  5517. return NULL;
  5518. }
  5519. /* no need to generate a Newchannel event here; it is done in the channel_alloc call */
  5520. return c;
  5521. }
  5522. ast_log(LOG_WARNING, "No channel type registered for '%s'\n", type);
  5523. *cause = AST_CAUSE_NOSUCHDRIVER;
  5524. AST_RWLIST_UNLOCK(&backends);
  5525. return NULL;
  5526. }
  5527. /*!
  5528. * \internal
  5529. * \brief Setup new channel accountcodes from the requestor channel after ast_request().
  5530. * \since 13.0.0
  5531. *
  5532. * \param chan New channel to get accountcodes setup.
  5533. * \param requestor Requesting channel to get accountcodes from.
  5534. * \param relationship What the new channel was created for.
  5535. * \param precious TRUE if pre-existing accountcodes on chan will not be overwritten.
  5536. *
  5537. * \pre The chan and requestor channels are already locked.
  5538. *
  5539. * \return Nothing
  5540. */
  5541. static void channel_req_accountcodes(struct ast_channel *chan, const struct ast_channel *requestor, enum ast_channel_requestor_relationship relationship, int precious)
  5542. {
  5543. /*
  5544. * The primary reason for the existence of this function is
  5545. * so local channels can propagate accountcodes to the ;2
  5546. * channel before ast_call().
  5547. *
  5548. * The secondary reason is to propagate the CHANNEL(peeraccount)
  5549. * value set before Dial, FollowMe, and Queue while maintaining
  5550. * the historic straight across accountcode propagation as a
  5551. * fallback.
  5552. */
  5553. switch (relationship) {
  5554. case AST_CHANNEL_REQUESTOR_BRIDGE_PEER:
  5555. /* Crossover the requestor's accountcode and peeraccount */
  5556. if (!precious || ast_strlen_zero(ast_channel_accountcode(chan))) {
  5557. /*
  5558. * The newly created channel does not have an accountcode
  5559. * or we don't care.
  5560. */
  5561. if (!ast_strlen_zero(ast_channel_peeraccount(requestor))) {
  5562. /*
  5563. * Set it to the requestor's peeraccount. This allows the
  5564. * dialplan to indicate the accountcode to use when dialing
  5565. * by setting CHANNEL(peeraccount).
  5566. */
  5567. ast_channel_accountcode_set(chan, ast_channel_peeraccount(requestor));
  5568. } else if (!precious
  5569. && !ast_strlen_zero(ast_channel_accountcode(requestor))) {
  5570. /*
  5571. * Fallback to the historic propagation and set it to the
  5572. * requestor's accountcode.
  5573. */
  5574. ast_channel_accountcode_set(chan, ast_channel_accountcode(requestor));
  5575. }
  5576. }
  5577. if (!ast_strlen_zero(ast_channel_accountcode(requestor))) {
  5578. ast_channel_peeraccount_set(chan, ast_channel_accountcode(requestor));
  5579. }
  5580. break;
  5581. case AST_CHANNEL_REQUESTOR_REPLACEMENT:
  5582. /* Pass the requestor's accountcode and peeraccount straight. */
  5583. if (!precious || ast_strlen_zero(ast_channel_accountcode(chan))) {
  5584. /*
  5585. * The newly created channel does not have an accountcode
  5586. * or we don't care.
  5587. */
  5588. if (!ast_strlen_zero(ast_channel_accountcode(requestor))) {
  5589. ast_channel_accountcode_set(chan, ast_channel_accountcode(requestor));
  5590. }
  5591. }
  5592. if (!ast_strlen_zero(ast_channel_peeraccount(requestor))) {
  5593. ast_channel_peeraccount_set(chan, ast_channel_peeraccount(requestor));
  5594. }
  5595. break;
  5596. }
  5597. }
  5598. void ast_channel_req_accountcodes(struct ast_channel *chan, const struct ast_channel *requestor, enum ast_channel_requestor_relationship relationship)
  5599. {
  5600. channel_req_accountcodes(chan, requestor, relationship, 0);
  5601. }
  5602. void ast_channel_req_accountcodes_precious(struct ast_channel *chan, const struct ast_channel *requestor, enum ast_channel_requestor_relationship relationship)
  5603. {
  5604. channel_req_accountcodes(chan, requestor, relationship, 1);
  5605. }
  5606. int ast_pre_call(struct ast_channel *chan, const char *sub_args)
  5607. {
  5608. int (*pre_call)(struct ast_channel *chan, const char *sub_args);
  5609. ast_channel_lock(chan);
  5610. pre_call = ast_channel_tech(chan)->pre_call;
  5611. if (pre_call) {
  5612. int res;
  5613. res = pre_call(chan, sub_args);
  5614. ast_channel_unlock(chan);
  5615. return res;
  5616. }
  5617. ast_channel_unlock(chan);
  5618. return ast_app_exec_sub(NULL, chan, sub_args, 0);
  5619. }
  5620. int ast_call(struct ast_channel *chan, const char *addr, int timeout)
  5621. {
  5622. /* Place an outgoing call, but don't wait any longer than timeout ms before returning.
  5623. If the remote end does not answer within the timeout, then do NOT hang up, but
  5624. return anyway. */
  5625. int res = -1;
  5626. /* Stop if we're a zombie or need a soft hangup */
  5627. ast_channel_lock(chan);
  5628. if (!ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) && !ast_check_hangup(chan)) {
  5629. if (ast_channel_tech(chan)->call)
  5630. res = ast_channel_tech(chan)->call(chan, addr, timeout);
  5631. ast_set_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING);
  5632. }
  5633. ast_channel_unlock(chan);
  5634. return res;
  5635. }
  5636. /*!
  5637. \brief Transfer a call to dest, if the channel supports transfer
  5638. Called by:
  5639. \arg app_transfer
  5640. \arg the manager interface
  5641. */
  5642. int ast_transfer(struct ast_channel *chan, char *dest)
  5643. {
  5644. int res = -1;
  5645. /* Stop if we're a zombie or need a soft hangup */
  5646. ast_channel_lock(chan);
  5647. if (!ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) && !ast_check_hangup(chan)) {
  5648. if (ast_channel_tech(chan)->transfer) {
  5649. res = ast_channel_tech(chan)->transfer(chan, dest);
  5650. if (!res)
  5651. res = 1;
  5652. } else
  5653. res = 0;
  5654. }
  5655. ast_channel_unlock(chan);
  5656. if (res <= 0) {
  5657. return res;
  5658. }
  5659. for (;;) {
  5660. struct ast_frame *fr;
  5661. res = ast_waitfor(chan, -1);
  5662. if (res < 0 || !(fr = ast_read(chan))) {
  5663. res = -1;
  5664. break;
  5665. }
  5666. if (fr->frametype == AST_FRAME_CONTROL && fr->subclass.integer == AST_CONTROL_TRANSFER) {
  5667. enum ast_control_transfer *message = fr->data.ptr;
  5668. if (*message == AST_TRANSFER_SUCCESS) {
  5669. res = 1;
  5670. } else {
  5671. res = -1;
  5672. }
  5673. ast_frfree(fr);
  5674. break;
  5675. }
  5676. ast_frfree(fr);
  5677. }
  5678. return res;
  5679. }
  5680. int ast_readstring(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders)
  5681. {
  5682. return ast_readstring_full(c, s, len, timeout, ftimeout, enders, -1, -1);
  5683. }
  5684. int ast_readstring_full(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders, int audiofd, int ctrlfd)
  5685. {
  5686. int pos = 0; /* index in the buffer where we accumulate digits */
  5687. int to = ftimeout;
  5688. struct ast_silence_generator *silgen = NULL;
  5689. /* Stop if we're a zombie or need a soft hangup */
  5690. if (ast_test_flag(ast_channel_flags(c), AST_FLAG_ZOMBIE) || ast_check_hangup(c))
  5691. return -1;
  5692. if (!len)
  5693. return -1;
  5694. for (;;) {
  5695. int d;
  5696. if (ast_channel_stream(c)) {
  5697. d = ast_waitstream_full(c, AST_DIGIT_ANY, audiofd, ctrlfd);
  5698. ast_stopstream(c);
  5699. if (!silgen && ast_opt_transmit_silence)
  5700. silgen = ast_channel_start_silence_generator(c);
  5701. usleep(1000);
  5702. if (!d)
  5703. d = ast_waitfordigit_full(c, to, audiofd, ctrlfd);
  5704. } else {
  5705. if (!silgen && ast_opt_transmit_silence)
  5706. silgen = ast_channel_start_silence_generator(c);
  5707. d = ast_waitfordigit_full(c, to, audiofd, ctrlfd);
  5708. }
  5709. if (d < 0) {
  5710. ast_channel_stop_silence_generator(c, silgen);
  5711. return AST_GETDATA_FAILED;
  5712. }
  5713. if (d == 0) {
  5714. s[pos] = '\0';
  5715. ast_channel_stop_silence_generator(c, silgen);
  5716. return AST_GETDATA_TIMEOUT;
  5717. }
  5718. if (d == 1) {
  5719. s[pos] = '\0';
  5720. ast_channel_stop_silence_generator(c, silgen);
  5721. return AST_GETDATA_INTERRUPTED;
  5722. }
  5723. if (strchr(enders, d) && (pos == 0)) {
  5724. s[pos] = '\0';
  5725. ast_channel_stop_silence_generator(c, silgen);
  5726. return AST_GETDATA_EMPTY_END_TERMINATED;
  5727. }
  5728. if (!strchr(enders, d)) {
  5729. s[pos++] = d;
  5730. }
  5731. if (strchr(enders, d) || (pos >= len)) {
  5732. s[pos] = '\0';
  5733. ast_channel_stop_silence_generator(c, silgen);
  5734. return AST_GETDATA_COMPLETE;
  5735. }
  5736. to = timeout;
  5737. }
  5738. /* Never reached */
  5739. return 0;
  5740. }
  5741. int ast_channel_supports_html(struct ast_channel *chan)
  5742. {
  5743. return (ast_channel_tech(chan)->send_html) ? 1 : 0;
  5744. }
  5745. int ast_channel_sendhtml(struct ast_channel *chan, int subclass, const char *data, int datalen)
  5746. {
  5747. if (ast_channel_tech(chan)->send_html)
  5748. return ast_channel_tech(chan)->send_html(chan, subclass, data, datalen);
  5749. return -1;
  5750. }
  5751. int ast_channel_sendurl(struct ast_channel *chan, const char *url)
  5752. {
  5753. return ast_channel_sendhtml(chan, AST_HTML_URL, url, strlen(url) + 1);
  5754. }
  5755. /*! \brief Set up translation from one channel to another */
  5756. static int ast_channel_make_compatible_helper(struct ast_channel *from, struct ast_channel *to)
  5757. {
  5758. struct ast_format_cap *src_cap;
  5759. struct ast_format_cap *dst_cap;
  5760. RAII_VAR(struct ast_format *, best_src_fmt, NULL, ao2_cleanup);
  5761. RAII_VAR(struct ast_format *, best_dst_fmt, NULL, ao2_cleanup);
  5762. int no_path;
  5763. /*
  5764. * We cannot short circuit this code because it is possible to ask
  5765. * to make compatible two channels that are "compatible" because
  5766. * they already have translation paths setup but together make for
  5767. * a sub-optimal path. e.g., The From channel has g722 -> ulaw
  5768. * and the To channel has ulaw -> g722. They are "compatible" but
  5769. * together the translations are unnecessary and the audio loses
  5770. * fidelity in the process.
  5771. */
  5772. ast_channel_lock_both(from, to);
  5773. src_cap = ast_channel_nativeformats(from); /* shallow copy, do not destroy */
  5774. dst_cap = ast_channel_nativeformats(to); /* shallow copy, do not destroy */
  5775. /* If there's no audio in this call, don't bother with trying to find a translation path */
  5776. if (!ast_format_cap_has_type(src_cap, AST_MEDIA_TYPE_AUDIO)
  5777. || !ast_format_cap_has_type(dst_cap, AST_MEDIA_TYPE_AUDIO)) {
  5778. ast_channel_unlock(to);
  5779. ast_channel_unlock(from);
  5780. return 0;
  5781. }
  5782. no_path = ast_translator_best_choice(dst_cap, src_cap, &best_dst_fmt, &best_src_fmt);
  5783. ast_channel_unlock(to);
  5784. ast_channel_unlock(from);
  5785. if (no_path) {
  5786. ast_log(LOG_WARNING, "No path to translate from %s to %s\n",
  5787. ast_channel_name(from), ast_channel_name(to));
  5788. return -1;
  5789. }
  5790. /* if the best path is not 'pass through', then
  5791. * transcoding is needed; if desired, force transcode path
  5792. * to use SLINEAR between channels, but only if there is
  5793. * no direct conversion available. If generic PLC is
  5794. * desired, then transcoding via SLINEAR is a requirement
  5795. * even if the formats are the same.
  5796. */
  5797. if (ast_opt_generic_plc_on_equal_codecs
  5798. || (ast_format_cmp(best_dst_fmt, best_src_fmt) == AST_FORMAT_CMP_NOT_EQUAL
  5799. && (ast_opt_generic_plc || ast_opt_transcode_via_slin))) {
  5800. int use_slin = (ast_format_cache_is_slinear(best_src_fmt)
  5801. || ast_format_cache_is_slinear(best_dst_fmt))
  5802. ? 1 : ast_opt_generic_plc_on_equal_codecs;
  5803. if (use_slin || ast_translate_path_steps(best_dst_fmt, best_src_fmt) != 1) {
  5804. int best_sample_rate = (ast_format_get_sample_rate(best_src_fmt) > ast_format_get_sample_rate(best_dst_fmt)) ?
  5805. ast_format_get_sample_rate(best_src_fmt) : ast_format_get_sample_rate(best_dst_fmt);
  5806. /* pick the best signed linear format based upon what preserves the sample rate the best. */
  5807. ao2_replace(best_src_fmt, ast_format_cache_get_slin_by_rate(best_sample_rate));
  5808. }
  5809. }
  5810. if (ast_set_read_format(from, best_src_fmt)) {
  5811. ast_log(LOG_WARNING, "Unable to set read format on channel %s to %s\n",
  5812. ast_channel_name(from), ast_format_get_name(best_src_fmt));
  5813. return -1;
  5814. }
  5815. if (ast_set_write_format(to, best_src_fmt)) {
  5816. ast_log(LOG_WARNING, "Unable to set write format on channel %s to %s\n",
  5817. ast_channel_name(to), ast_format_get_name(best_src_fmt));
  5818. return -1;
  5819. }
  5820. return 0;
  5821. }
  5822. int ast_channel_make_compatible(struct ast_channel *chan, struct ast_channel *peer)
  5823. {
  5824. /*
  5825. * Set up translation from the peer to the chan first in case we
  5826. * need to hear any in-band tones and the other direction fails.
  5827. */
  5828. if (ast_channel_make_compatible_helper(peer, chan)) {
  5829. return -1;
  5830. }
  5831. /* Set up translation from the chan to the peer */
  5832. if (ast_channel_make_compatible_helper(chan, peer)) {
  5833. return -1;
  5834. }
  5835. return 0;
  5836. }
  5837. /*! \brief this function simply changes the name of the channel and issues a manager_event
  5838. * with out unlinking and linking the channel from the ao2_container. This should
  5839. * only be used when the channel has already been unlinked from the ao2_container.
  5840. */
  5841. static void __ast_change_name_nolink(struct ast_channel *chan, const char *newname)
  5842. {
  5843. /*** DOCUMENTATION
  5844. <managerEventInstance>
  5845. <synopsis>Raised when the name of a channel is changed.</synopsis>
  5846. </managerEventInstance>
  5847. ***/
  5848. ast_manager_event(chan, EVENT_FLAG_CALL, "Rename",
  5849. "Channel: %s\r\n"
  5850. "Newname: %s\r\n"
  5851. "Uniqueid: %s\r\n",
  5852. ast_channel_name(chan), newname, ast_channel_uniqueid(chan));
  5853. ast_channel_name_set(chan, newname);
  5854. }
  5855. void ast_change_name(struct ast_channel *chan, const char *newname)
  5856. {
  5857. /* We must re-link, as the hash value will change here. */
  5858. ao2_lock(channels);
  5859. ast_channel_lock(chan);
  5860. ao2_unlink(channels, chan);
  5861. __ast_change_name_nolink(chan, newname);
  5862. ao2_link(channels, chan);
  5863. ast_channel_unlock(chan);
  5864. ao2_unlock(channels);
  5865. }
  5866. void ast_channel_inherit_variables(const struct ast_channel *parent, struct ast_channel *child)
  5867. {
  5868. struct ast_var_t *current;
  5869. struct ast_var_t *newvar;
  5870. const char *varname;
  5871. int vartype;
  5872. AST_LIST_TRAVERSE(ast_channel_varshead((struct ast_channel *) parent), current, entries) {
  5873. varname = ast_var_full_name(current);
  5874. if (!varname) {
  5875. continue;
  5876. }
  5877. vartype = 0;
  5878. if (varname[0] == '_') {
  5879. vartype = 1;
  5880. if (varname[1] == '_') {
  5881. vartype = 2;
  5882. }
  5883. }
  5884. switch (vartype) {
  5885. case 1:
  5886. newvar = ast_var_assign(&varname[1], ast_var_value(current));
  5887. break;
  5888. case 2:
  5889. newvar = ast_var_assign(varname, ast_var_value(current));
  5890. break;
  5891. default:
  5892. continue;
  5893. }
  5894. if (newvar) {
  5895. ast_debug(1, "Inheriting variable %s from %s to %s.\n",
  5896. ast_var_full_name(newvar), ast_channel_name(parent),
  5897. ast_channel_name(child));
  5898. AST_LIST_INSERT_TAIL(ast_channel_varshead(child), newvar, entries);
  5899. ast_channel_publish_varset(child, ast_var_full_name(newvar),
  5900. ast_var_value(newvar));
  5901. }
  5902. }
  5903. }
  5904. /*!
  5905. \brief Clone channel variables from 'clone' channel into 'original' channel
  5906. All variables except those related to app_groupcount are cloned.
  5907. Variables are actually _removed_ from 'clone' channel, presumably
  5908. because it will subsequently be destroyed.
  5909. \note Assumes locks will be in place on both channels when called.
  5910. */
  5911. static void clone_variables(struct ast_channel *original, struct ast_channel *clonechan)
  5912. {
  5913. struct ast_var_t *current, *newvar;
  5914. /* Append variables from clone channel into original channel */
  5915. /* XXX Is this always correct? We have to in order to keep MACROS working XXX */
  5916. AST_LIST_APPEND_LIST(ast_channel_varshead(original), ast_channel_varshead(clonechan), entries);
  5917. /* then, dup the varshead list into the clone */
  5918. AST_LIST_TRAVERSE(ast_channel_varshead(original), current, entries) {
  5919. newvar = ast_var_assign(current->name, current->value);
  5920. if (newvar)
  5921. AST_LIST_INSERT_TAIL(ast_channel_varshead(clonechan), newvar, entries);
  5922. }
  5923. }
  5924. void ast_channel_name_to_dial_string(char *channel_name)
  5925. {
  5926. char *dash;
  5927. /* Truncate after the dash */
  5928. dash = strrchr(channel_name, '-');
  5929. if (dash) {
  5930. *dash = '\0';
  5931. }
  5932. }
  5933. /*!
  5934. * \brief Masquerade a channel
  5935. *
  5936. * \note Assumes _NO_ channels and _NO_ channel pvt's are locked. If a channel is locked while calling
  5937. * this function, it invalidates our channel container locking order. All channels
  5938. * must be unlocked before it is permissible to lock the channels' ao2 container.
  5939. */
  5940. static void channel_do_masquerade(struct ast_channel *original, struct ast_channel *clonechan)
  5941. {
  5942. int x;
  5943. int origstate;
  5944. unsigned int orig_disablestatecache;
  5945. unsigned int clone_disablestatecache;
  5946. int visible_indication;
  5947. int clone_hold_state;
  5948. int moh_is_playing;
  5949. struct ast_frame *current;
  5950. const struct ast_channel_tech *t;
  5951. void *t_pvt;
  5952. union {
  5953. struct ast_hangup_handler_list handlers;
  5954. struct ast_party_dialed dialed;
  5955. struct ast_party_caller caller;
  5956. struct ast_party_connected_line connected;
  5957. struct ast_party_redirecting redirecting;
  5958. } exchange;
  5959. struct ast_channel *bridged;
  5960. struct ast_format *rformat;
  5961. struct ast_format *wformat;
  5962. struct ast_format *tmp_format;
  5963. struct ast_format_cap *tmp_cap;
  5964. char tmp_name[AST_CHANNEL_NAME];
  5965. char clone_sending_dtmf_digit;
  5966. struct timeval clone_sending_dtmf_tv;
  5967. /* XXX This operation is a bit odd. We're essentially putting the guts of
  5968. * the clone channel into the original channel. Start by killing off the
  5969. * original channel's backend. While the features are nice, which is the
  5970. * reason we're keeping it, it's still awesomely weird. XXX */
  5971. /* Indicate to each channel that a masquerade is about to begin. */
  5972. x = 1;
  5973. ast_indicate_data(original, AST_CONTROL_MASQUERADE_NOTIFY, &x, sizeof(x));
  5974. ast_indicate_data(clonechan, AST_CONTROL_MASQUERADE_NOTIFY, &x, sizeof(x));
  5975. /*
  5976. * The container lock is necessary for proper locking order
  5977. * because the channels must be unlinked to change their
  5978. * names.
  5979. *
  5980. * The original and clonechan locks must be held while the
  5981. * channel contents are shuffled around for the masquerade.
  5982. *
  5983. * The masq and masqr pointers need to be left alone until the masquerade
  5984. * has restabilized the channels to hold off ast_hangup() and until
  5985. * AST_FLAG_ZOMBIE can be set on the clonechan.
  5986. */
  5987. ao2_lock(channels);
  5988. /* Bump the refs to ensure that they won't dissapear on us. */
  5989. ast_channel_ref(original);
  5990. ast_channel_ref(clonechan);
  5991. /* unlink from channels container as name (which is the hash value) will change */
  5992. ao2_unlink(channels, original);
  5993. ao2_unlink(channels, clonechan);
  5994. moh_is_playing = ast_test_flag(ast_channel_flags(original), AST_FLAG_MOH);
  5995. if (moh_is_playing) {
  5996. /* Stop MOH on the old original channel. */
  5997. ast_moh_stop(original);
  5998. }
  5999. /*
  6000. * Stop any visible indication on the original channel so we can
  6001. * transfer it to the clonechan taking the original's place.
  6002. */
  6003. ast_channel_lock(original);
  6004. visible_indication = ast_channel_visible_indication(original);
  6005. ast_channel_unlock(original);
  6006. ast_indicate(original, -1);
  6007. /* Start the masquerade channel contents rearangement. */
  6008. ast_channel_lock_both(original, clonechan);
  6009. ast_debug(1, "Actually Masquerading %s(%u) into the structure of %s(%u)\n",
  6010. ast_channel_name(clonechan), ast_channel_state(clonechan),
  6011. ast_channel_name(original), ast_channel_state(original));
  6012. /*
  6013. * Remember the original read/write formats. We turn off any
  6014. * translation on either one
  6015. */
  6016. rformat = ao2_bump(ast_channel_readformat(original));
  6017. wformat = ao2_bump(ast_channel_writeformat(original));
  6018. free_translation(clonechan);
  6019. free_translation(original);
  6020. clone_hold_state = ast_channel_hold_state(clonechan);
  6021. /* Save the current DTMF digit being sent if any. */
  6022. clone_sending_dtmf_digit = ast_channel_sending_dtmf_digit(clonechan);
  6023. clone_sending_dtmf_tv = ast_channel_sending_dtmf_tv(clonechan);
  6024. /* Swap uniqueid's of the channels. This needs to happen before channel renames,
  6025. * so rename events get the proper id's.
  6026. */
  6027. ast_channel_internal_swap_uniqueid_and_linkedid(clonechan, original);
  6028. /* Make sure the Stasis topic on the channel is updated appropriately */
  6029. ast_channel_internal_swap_topics(clonechan, original);
  6030. /* Swap endpoint forward and endpoint cache forward details of the channels,
  6031. * so channel created with endpoint exchanges its state with other channel
  6032. * for proper endpoint cleanup.
  6033. */
  6034. ast_channel_internal_swap_endpoint_forward_and_endpoint_cache_forward(clonechan, original);
  6035. /* Swap channel names. This uses ast_channel_name_set directly, so we
  6036. * don't get any spurious rename events.
  6037. */
  6038. ast_copy_string(tmp_name, ast_channel_name(clonechan), sizeof(tmp_name));
  6039. ast_channel_name_set(clonechan, ast_channel_name(original));
  6040. ast_channel_name_set(original, tmp_name);
  6041. /* Swap the technologies */
  6042. t = ast_channel_tech(original);
  6043. ast_channel_tech_set(original, ast_channel_tech(clonechan));
  6044. ast_channel_tech_set(clonechan, t);
  6045. t_pvt = ast_channel_tech_pvt(original);
  6046. ast_channel_tech_pvt_set(original, ast_channel_tech_pvt(clonechan));
  6047. ast_channel_tech_pvt_set(clonechan, t_pvt);
  6048. /* Swap the alertpipes */
  6049. ast_channel_internal_alertpipe_swap(original, clonechan);
  6050. /*
  6051. * Swap the readq's. The end result should be this:
  6052. *
  6053. * 1) All frames should be on the new (original) channel.
  6054. * 2) Any frames that were already on the new channel before this
  6055. * masquerade need to be at the end of the readq, after all of the
  6056. * frames on the old (clone) channel.
  6057. * 3) The alertpipe needs to get poked for every frame that was already
  6058. * on the new channel, since we are now using the alert pipe from the
  6059. * old (clone) channel.
  6060. */
  6061. {
  6062. AST_LIST_HEAD_NOLOCK(, ast_frame) tmp_readq;
  6063. AST_LIST_HEAD_INIT_NOLOCK(&tmp_readq);
  6064. AST_LIST_APPEND_LIST(&tmp_readq, ast_channel_readq(original), frame_list);
  6065. AST_LIST_APPEND_LIST(ast_channel_readq(original), ast_channel_readq(clonechan), frame_list);
  6066. while ((current = AST_LIST_REMOVE_HEAD(&tmp_readq, frame_list))) {
  6067. AST_LIST_INSERT_TAIL(ast_channel_readq(original), current, frame_list);
  6068. if (ast_channel_alert_write(original)) {
  6069. ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
  6070. }
  6071. }
  6072. }
  6073. /* Swap the raw formats */
  6074. tmp_format = ao2_bump(ast_channel_rawreadformat(original));
  6075. ast_channel_set_rawreadformat(original, ast_channel_rawreadformat(clonechan));
  6076. ast_channel_set_rawreadformat(clonechan, tmp_format);
  6077. ao2_cleanup(tmp_format);
  6078. tmp_format = ao2_bump(ast_channel_rawwriteformat(original));
  6079. ast_channel_set_rawwriteformat(original, ast_channel_rawwriteformat(clonechan));
  6080. ast_channel_set_rawwriteformat(clonechan, tmp_format);
  6081. ao2_cleanup(tmp_format);
  6082. ast_channel_softhangup_internal_flag_set(clonechan, AST_SOFTHANGUP_DEV);
  6083. /* And of course, so does our current state. Note we need not
  6084. call ast_setstate since the event manager doesn't really consider
  6085. these separate. We do this early so that the clone has the proper
  6086. state of the original channel. */
  6087. origstate = ast_channel_state(original);
  6088. ast_channel_state_set(original, ast_channel_state(clonechan));
  6089. ast_channel_state_set(clonechan, origstate);
  6090. /* And the swap the cachable state too. Otherwise we'd start caching
  6091. * Local channels and ignoring real ones. */
  6092. orig_disablestatecache = ast_test_flag(ast_channel_flags(original), AST_FLAG_DISABLE_DEVSTATE_CACHE);
  6093. clone_disablestatecache = ast_test_flag(ast_channel_flags(clonechan), AST_FLAG_DISABLE_DEVSTATE_CACHE);
  6094. if (orig_disablestatecache != clone_disablestatecache) {
  6095. if (orig_disablestatecache) {
  6096. ast_clear_flag(ast_channel_flags(original), AST_FLAG_DISABLE_DEVSTATE_CACHE);
  6097. ast_set_flag(ast_channel_flags(clonechan), AST_FLAG_DISABLE_DEVSTATE_CACHE);
  6098. } else {
  6099. ast_set_flag(ast_channel_flags(original), AST_FLAG_DISABLE_DEVSTATE_CACHE);
  6100. ast_clear_flag(ast_channel_flags(clonechan), AST_FLAG_DISABLE_DEVSTATE_CACHE);
  6101. }
  6102. }
  6103. /* Update the type. */
  6104. t_pvt = ast_channel_monitor(original);
  6105. ast_channel_monitor_set(original, ast_channel_monitor(clonechan));
  6106. ast_channel_monitor_set(clonechan, t_pvt);
  6107. /* Keep the same language. */
  6108. ast_channel_language_set(original, ast_channel_language(clonechan));
  6109. /* Keep the same parkinglot. */
  6110. ast_channel_parkinglot_set(original, ast_channel_parkinglot(clonechan));
  6111. /* Copy the FD's other than the generator fd */
  6112. for (x = 0; x < AST_MAX_FDS; x++) {
  6113. if (x != AST_GENERATOR_FD)
  6114. ast_channel_set_fd(original, x, ast_channel_fd(clonechan, x));
  6115. }
  6116. ast_app_group_update(clonechan, original);
  6117. /* Swap hangup handlers. */
  6118. exchange.handlers = *ast_channel_hangup_handlers(original);
  6119. *ast_channel_hangup_handlers(original) = *ast_channel_hangup_handlers(clonechan);
  6120. *ast_channel_hangup_handlers(clonechan) = exchange.handlers;
  6121. /* Call fixup handlers for the clone chan */
  6122. if (AST_LIST_FIRST(ast_channel_datastores(clonechan))) {
  6123. struct ast_datastore *ds;
  6124. /* We use a safe traversal here because some fixup routines actually
  6125. * remove the datastore from the list and free them.
  6126. */
  6127. AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_datastores(clonechan), ds, entry) {
  6128. if (ds->info->chan_fixup) {
  6129. ds->info->chan_fixup(ds->data, clonechan, original);
  6130. }
  6131. }
  6132. AST_LIST_TRAVERSE_SAFE_END;
  6133. }
  6134. /* Call breakdown handlers for the original chan */
  6135. if (AST_LIST_FIRST(ast_channel_datastores(original))) {
  6136. struct ast_datastore *ds;
  6137. /* We use a safe traversal here because some breakdown routines may
  6138. * remove the datastore from the list and free them.
  6139. */
  6140. AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_datastores(original), ds, entry) {
  6141. if (ds->info->chan_breakdown) {
  6142. ds->info->chan_breakdown(ds->data, clonechan, original);
  6143. }
  6144. }
  6145. AST_LIST_TRAVERSE_SAFE_END;
  6146. }
  6147. /* Move data stores over */
  6148. if (AST_LIST_FIRST(ast_channel_datastores(clonechan))) {
  6149. AST_LIST_APPEND_LIST(ast_channel_datastores(original), ast_channel_datastores(clonechan), entry);
  6150. }
  6151. /* Move framehooks over */
  6152. ast_framehook_list_fixup(clonechan, original);
  6153. /* Move audiohooks over */
  6154. ast_audiohook_move_all(clonechan, original);
  6155. ast_autochan_new_channel(clonechan, original);
  6156. clone_variables(original, clonechan);
  6157. /* Presense of ADSI capable CPE follows clone */
  6158. ast_channel_adsicpe_set(original, ast_channel_adsicpe(clonechan));
  6159. /* Bridge remains the same */
  6160. /* CDR fields remain the same */
  6161. /* XXX What about blocking, softhangup, blocker, and lock and blockproc? XXX */
  6162. /* Application and data remain the same */
  6163. /* Clone exception becomes real one, as with fdno */
  6164. ast_set_flag(ast_channel_flags(original), ast_test_flag(ast_channel_flags(clonechan), AST_FLAG_EXCEPTION | AST_FLAG_OUTGOING));
  6165. ast_channel_fdno_set(original, ast_channel_fdno(clonechan));
  6166. /* Schedule context remains the same */
  6167. /* Stream stuff stays the same */
  6168. /* Keep the original state. The fixup code will need to work with it most likely */
  6169. /*
  6170. * Just swap the whole structures, nevermind the allocations,
  6171. * they'll work themselves out.
  6172. */
  6173. exchange.dialed = *ast_channel_dialed(original);
  6174. ast_channel_dialed_set(original, ast_channel_dialed(clonechan));
  6175. ast_channel_dialed_set(clonechan, &exchange.dialed);
  6176. /* Reset any earlier private caller id representations */
  6177. ast_party_id_reset(&ast_channel_caller(original)->priv);
  6178. ast_party_id_reset(&ast_channel_caller(clonechan)->priv);
  6179. exchange.caller = *ast_channel_caller(original);
  6180. ast_channel_caller_set(original, ast_channel_caller(clonechan));
  6181. ast_channel_caller_set(clonechan, &exchange.caller);
  6182. /* Reset any earlier private connected id representations */
  6183. ast_party_id_reset(&ast_channel_connected(original)->priv);
  6184. ast_party_id_reset(&ast_channel_connected(clonechan)->priv);
  6185. exchange.connected = *ast_channel_connected(original);
  6186. ast_channel_connected_set(original, ast_channel_connected(clonechan));
  6187. ast_channel_connected_set(clonechan, &exchange.connected);
  6188. /* Reset any earlier private redirecting orig, from or to representations */
  6189. ast_party_id_reset(&ast_channel_redirecting(original)->priv_orig);
  6190. ast_party_id_reset(&ast_channel_redirecting(clonechan)->priv_orig);
  6191. ast_party_id_reset(&ast_channel_redirecting(original)->priv_from);
  6192. ast_party_id_reset(&ast_channel_redirecting(clonechan)->priv_from);
  6193. ast_party_id_reset(&ast_channel_redirecting(original)->priv_to);
  6194. ast_party_id_reset(&ast_channel_redirecting(clonechan)->priv_to);
  6195. exchange.redirecting = *ast_channel_redirecting(original);
  6196. ast_channel_redirecting_set(original, ast_channel_redirecting(clonechan));
  6197. ast_channel_redirecting_set(clonechan, &exchange.redirecting);
  6198. ast_channel_publish_snapshot(original);
  6199. /* Restore original timing file descriptor */
  6200. ast_channel_set_fd(original, AST_TIMING_FD, ast_channel_timingfd(original));
  6201. /* Our native formats are different now */
  6202. tmp_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  6203. if (tmp_cap) {
  6204. ast_format_cap_append_from_cap(tmp_cap, ast_channel_nativeformats(clonechan), AST_MEDIA_TYPE_UNKNOWN);
  6205. ast_channel_nativeformats_set(original, tmp_cap);
  6206. ao2_ref(tmp_cap, -1);
  6207. }
  6208. /* Context, extension, priority, app data, jump table, remain the same */
  6209. /* pvt switches. pbx stays the same, as does next */
  6210. /* Set the write format */
  6211. ast_set_write_format(original, wformat);
  6212. /* Set the read format */
  6213. ast_set_read_format(original, rformat);
  6214. /* Copy the music class */
  6215. ast_channel_musicclass_set(original, ast_channel_musicclass(clonechan));
  6216. /* copy over accuntcode and set peeraccount across the bridge */
  6217. ast_channel_accountcode_set(original, S_OR(ast_channel_accountcode(clonechan), ""));
  6218. ast_debug(1, "Putting channel %s in %s/%s formats\n", ast_channel_name(original),
  6219. ast_format_get_name(wformat), ast_format_get_name(rformat));
  6220. /* Fixup the original clonechan's physical side */
  6221. if (ast_channel_tech(original)->fixup && ast_channel_tech(original)->fixup(clonechan, original)) {
  6222. ast_log(LOG_WARNING, "Channel type '%s' could not fixup channel %s, strange things may happen. (clonechan)\n",
  6223. ast_channel_tech(original)->type, ast_channel_name(original));
  6224. }
  6225. /* Fixup the original original's physical side */
  6226. if (ast_channel_tech(clonechan)->fixup && ast_channel_tech(clonechan)->fixup(original, clonechan)) {
  6227. ast_log(LOG_WARNING, "Channel type '%s' could not fixup channel %s, strange things may happen. (original)\n",
  6228. ast_channel_tech(clonechan)->type, ast_channel_name(clonechan));
  6229. }
  6230. /*
  6231. * Now, at this point, the "clone" channel is totally F'd up.
  6232. * We mark it as a zombie so nothing tries to touch it.
  6233. *
  6234. * This must be done before we unlock clonechan to prevent
  6235. * setting up another masquerade on the clonechan.
  6236. */
  6237. ast_set_flag(ast_channel_flags(clonechan), AST_FLAG_ZOMBIE);
  6238. ast_queue_frame(clonechan, &ast_null_frame);
  6239. ast_channel_unlock(original);
  6240. ast_channel_unlock(clonechan);
  6241. /*
  6242. * Indicate to each channel that a masquerade is complete.
  6243. *
  6244. * We can still do this to clonechan even though it is a
  6245. * zombie because ast_indicate_data() will explicitly pass
  6246. * this control and ast_hangup() is held off until the
  6247. * ast_channel_masq() and ast_channel_masqr() pointers are
  6248. * cleared.
  6249. */
  6250. x = 0;
  6251. ast_indicate_data(original, AST_CONTROL_MASQUERADE_NOTIFY, &x, sizeof(x));
  6252. ast_indicate_data(clonechan, AST_CONTROL_MASQUERADE_NOTIFY, &x, sizeof(x));
  6253. ast_bridge_notify_masquerade(original);
  6254. if (clone_hold_state == AST_CONTROL_HOLD) {
  6255. ast_debug(1, "Channel %s simulating UNHOLD for masquerade.\n",
  6256. ast_channel_name(original));
  6257. ast_indicate(original, AST_CONTROL_UNHOLD);
  6258. }
  6259. if (clone_sending_dtmf_digit) {
  6260. /*
  6261. * The clonechan was sending a DTMF digit that was not completed
  6262. * before the masquerade.
  6263. */
  6264. ast_channel_end_dtmf(original, clone_sending_dtmf_digit, clone_sending_dtmf_tv,
  6265. "masquerade");
  6266. }
  6267. /*
  6268. * If an indication is currently playing, maintain it on the
  6269. * channel that is taking the place of original.
  6270. *
  6271. * This is needed because the masquerade is swapping out the
  6272. * internals of the channel, and the new channel private data
  6273. * needs to be made aware of the current visible indication
  6274. * (RINGING, CONGESTION, etc.)
  6275. */
  6276. if (visible_indication) {
  6277. if (visible_indication == AST_CONTROL_HOLD) {
  6278. const char *latest_musicclass;
  6279. int len;
  6280. ast_channel_lock(original);
  6281. latest_musicclass = ast_strdupa(ast_channel_latest_musicclass(original));
  6282. ast_channel_unlock(original);
  6283. if (ast_strlen_zero(latest_musicclass)) {
  6284. latest_musicclass = NULL;
  6285. len = 0;
  6286. } else {
  6287. len = strlen(latest_musicclass) + 1;
  6288. }
  6289. ast_indicate_data(original, visible_indication, latest_musicclass, len);
  6290. } else {
  6291. ast_indicate(original, visible_indication);
  6292. }
  6293. }
  6294. /*
  6295. * If MOH was playing on the original channel then it needs to be
  6296. * maintained on the channel that is replacing it.
  6297. */
  6298. if (moh_is_playing) {
  6299. /* Start MOH on the new original channel. */
  6300. ast_moh_start(original, NULL, NULL);
  6301. }
  6302. ast_channel_lock(original);
  6303. /* Signal any blocker */
  6304. if (ast_test_flag(ast_channel_flags(original), AST_FLAG_BLOCKING)) {
  6305. pthread_kill(ast_channel_blocker(original), SIGURG);
  6306. }
  6307. ast_debug(1, "Done Masquerading %s (%u)\n", ast_channel_name(original), ast_channel_state(original));
  6308. ast_channel_unlock(original);
  6309. if ((bridged = ast_channel_bridge_peer(original))) {
  6310. ast_indicate(bridged, AST_CONTROL_SRCCHANGE);
  6311. ast_channel_unref(bridged);
  6312. }
  6313. ast_indicate(original, AST_CONTROL_SRCCHANGE);
  6314. /* Now that the operation is complete, we can clear the masq
  6315. * and masqr fields of both channels.
  6316. */
  6317. ast_channel_lock_both(original, clonechan);
  6318. ast_channel_masq_set(original, NULL);
  6319. ast_channel_masqr_set(clonechan, NULL);
  6320. ast_channel_unlock(original);
  6321. ast_channel_unlock(clonechan);
  6322. ao2_link(channels, clonechan);
  6323. ao2_link(channels, original);
  6324. ao2_unlock(channels);
  6325. /* Release our held safety references. */
  6326. ast_channel_unref(original);
  6327. ast_channel_unref(clonechan);
  6328. ao2_cleanup(rformat);
  6329. ao2_cleanup(wformat);
  6330. }
  6331. void ast_set_callerid(struct ast_channel *chan, const char *cid_num, const char *cid_name, const char *cid_ani)
  6332. {
  6333. ast_channel_lock(chan);
  6334. if (cid_num) {
  6335. ast_channel_caller(chan)->id.number.valid = 1;
  6336. ast_free(ast_channel_caller(chan)->id.number.str);
  6337. ast_channel_caller(chan)->id.number.str = ast_strdup(cid_num);
  6338. }
  6339. if (cid_name) {
  6340. ast_channel_caller(chan)->id.name.valid = 1;
  6341. ast_free(ast_channel_caller(chan)->id.name.str);
  6342. ast_channel_caller(chan)->id.name.str = ast_strdup(cid_name);
  6343. }
  6344. if (cid_ani) {
  6345. ast_channel_caller(chan)->ani.number.valid = 1;
  6346. ast_free(ast_channel_caller(chan)->ani.number.str);
  6347. ast_channel_caller(chan)->ani.number.str = ast_strdup(cid_ani);
  6348. }
  6349. ast_channel_publish_snapshot(chan);
  6350. ast_channel_unlock(chan);
  6351. }
  6352. void ast_channel_set_caller(struct ast_channel *chan, const struct ast_party_caller *caller, const struct ast_set_party_caller *update)
  6353. {
  6354. if (ast_channel_caller(chan) == caller) {
  6355. /* Don't set to self */
  6356. return;
  6357. }
  6358. ast_channel_lock(chan);
  6359. ast_party_caller_set(ast_channel_caller(chan), caller, update);
  6360. ast_channel_unlock(chan);
  6361. }
  6362. void ast_channel_set_caller_event(struct ast_channel *chan, const struct ast_party_caller *caller, const struct ast_set_party_caller *update)
  6363. {
  6364. if (ast_channel_caller(chan) == caller) {
  6365. /* Don't set to self */
  6366. return;
  6367. }
  6368. ast_channel_lock(chan);
  6369. ast_party_caller_set(ast_channel_caller(chan), caller, update);
  6370. ast_channel_publish_snapshot(chan);
  6371. ast_channel_unlock(chan);
  6372. }
  6373. int ast_setstate(struct ast_channel *chan, enum ast_channel_state state)
  6374. {
  6375. int oldstate = ast_channel_state(chan);
  6376. char name[AST_CHANNEL_NAME], *dashptr;
  6377. if (oldstate == state)
  6378. return 0;
  6379. ast_copy_string(name, ast_channel_name(chan), sizeof(name));
  6380. if ((dashptr = strrchr(name, '-'))) {
  6381. *dashptr = '\0';
  6382. }
  6383. ast_channel_state_set(chan, state);
  6384. ast_publish_channel_state(chan);
  6385. /* We have to pass AST_DEVICE_UNKNOWN here because it is entirely possible that the channel driver
  6386. * for this channel is using the callback method for device state. If we pass in an actual state here
  6387. * we override what they are saying the state is and things go amuck. */
  6388. ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE) ? AST_DEVSTATE_NOT_CACHABLE : AST_DEVSTATE_CACHABLE), name);
  6389. return 0;
  6390. }
  6391. /*! \brief Bridge two channels together (early) */
  6392. int ast_channel_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
  6393. {
  6394. /* Make sure we can early bridge, if not error out */
  6395. if (!ast_channel_tech(c0)->early_bridge || (c1 && (!ast_channel_tech(c1)->early_bridge || ast_channel_tech(c0)->early_bridge != ast_channel_tech(c1)->early_bridge)))
  6396. return -1;
  6397. return ast_channel_tech(c0)->early_bridge(c0, c1);
  6398. }
  6399. /*! \brief Sets an option on a channel */
  6400. int ast_channel_setoption(struct ast_channel *chan, int option, void *data, int datalen, int block)
  6401. {
  6402. int res;
  6403. ast_channel_lock(chan);
  6404. if (!ast_channel_tech(chan)->setoption) {
  6405. errno = ENOSYS;
  6406. ast_channel_unlock(chan);
  6407. return -1;
  6408. }
  6409. if (block)
  6410. ast_log(LOG_ERROR, "XXX Blocking not implemented yet XXX\n");
  6411. res = ast_channel_tech(chan)->setoption(chan, option, data, datalen);
  6412. ast_channel_unlock(chan);
  6413. return res;
  6414. }
  6415. int ast_channel_queryoption(struct ast_channel *chan, int option, void *data, int *datalen, int block)
  6416. {
  6417. int res;
  6418. ast_channel_lock(chan);
  6419. if (!ast_channel_tech(chan)->queryoption) {
  6420. errno = ENOSYS;
  6421. ast_channel_unlock(chan);
  6422. return -1;
  6423. }
  6424. if (block)
  6425. ast_log(LOG_ERROR, "XXX Blocking not implemented yet XXX\n");
  6426. res = ast_channel_tech(chan)->queryoption(chan, option, data, datalen);
  6427. ast_channel_unlock(chan);
  6428. return res;
  6429. }
  6430. struct tonepair_def {
  6431. int freq1;
  6432. int freq2;
  6433. int duration;
  6434. int vol;
  6435. };
  6436. struct tonepair_state {
  6437. int fac1;
  6438. int fac2;
  6439. int v1_1;
  6440. int v2_1;
  6441. int v3_1;
  6442. int v1_2;
  6443. int v2_2;
  6444. int v3_2;
  6445. struct ast_format *origwfmt;
  6446. int pos;
  6447. int duration;
  6448. int modulate;
  6449. struct ast_frame f;
  6450. unsigned char offset[AST_FRIENDLY_OFFSET];
  6451. short data[4000];
  6452. };
  6453. static void tonepair_release(struct ast_channel *chan, void *params)
  6454. {
  6455. struct tonepair_state *ts = params;
  6456. if (chan) {
  6457. ast_set_write_format(chan, ts->origwfmt);
  6458. }
  6459. ao2_cleanup(ts->origwfmt);
  6460. ast_free(ts);
  6461. }
  6462. static void *tonepair_alloc(struct ast_channel *chan, void *params)
  6463. {
  6464. struct tonepair_state *ts;
  6465. struct tonepair_def *td = params;
  6466. if (!(ts = ast_calloc(1, sizeof(*ts)))) {
  6467. return NULL;
  6468. }
  6469. ts->origwfmt = ao2_bump(ast_channel_writeformat(chan));
  6470. if (ast_set_write_format(chan, ast_format_slin)) {
  6471. ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", ast_channel_name(chan));
  6472. tonepair_release(NULL, ts);
  6473. ts = NULL;
  6474. } else {
  6475. ts->fac1 = 2.0 * cos(2.0 * M_PI * (td->freq1 / 8000.0)) * 32768.0;
  6476. ts->v1_1 = 0;
  6477. ts->v2_1 = sin(-4.0 * M_PI * (td->freq1 / 8000.0)) * td->vol;
  6478. ts->v3_1 = sin(-2.0 * M_PI * (td->freq1 / 8000.0)) * td->vol;
  6479. ts->v2_1 = 0;
  6480. ts->fac2 = 2.0 * cos(2.0 * M_PI * (td->freq2 / 8000.0)) * 32768.0;
  6481. ts->v2_2 = sin(-4.0 * M_PI * (td->freq2 / 8000.0)) * td->vol;
  6482. ts->v3_2 = sin(-2.0 * M_PI * (td->freq2 / 8000.0)) * td->vol;
  6483. ts->duration = td->duration;
  6484. ts->modulate = 0;
  6485. }
  6486. /* Let interrupts interrupt :) */
  6487. ast_set_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT);
  6488. return ts;
  6489. }
  6490. static int tonepair_generator(struct ast_channel *chan, void *data, int len, int samples)
  6491. {
  6492. struct tonepair_state *ts = data;
  6493. int x;
  6494. /* we need to prepare a frame with 16 * timelen samples as we're
  6495. * generating SLIN audio
  6496. */
  6497. len = samples * 2;
  6498. if (len > sizeof(ts->data) / 2 - 1) {
  6499. ast_log(LOG_WARNING, "Can't generate that much data!\n");
  6500. return -1;
  6501. }
  6502. memset(&ts->f, 0, sizeof(ts->f));
  6503. for (x=0;x<len/2;x++) {
  6504. ts->v1_1 = ts->v2_1;
  6505. ts->v2_1 = ts->v3_1;
  6506. ts->v3_1 = (ts->fac1 * ts->v2_1 >> 15) - ts->v1_1;
  6507. ts->v1_2 = ts->v2_2;
  6508. ts->v2_2 = ts->v3_2;
  6509. ts->v3_2 = (ts->fac2 * ts->v2_2 >> 15) - ts->v1_2;
  6510. if (ts->modulate) {
  6511. int p;
  6512. p = ts->v3_2 - 32768;
  6513. if (p < 0) p = -p;
  6514. p = ((p * 9) / 10) + 1;
  6515. ts->data[x] = (ts->v3_1 * p) >> 15;
  6516. } else
  6517. ts->data[x] = ts->v3_1 + ts->v3_2;
  6518. }
  6519. ts->f.frametype = AST_FRAME_VOICE;
  6520. ts->f.subclass.format = ast_format_slin;
  6521. ts->f.datalen = len;
  6522. ts->f.samples = samples;
  6523. ts->f.offset = AST_FRIENDLY_OFFSET;
  6524. ts->f.data.ptr = ts->data;
  6525. ast_write(chan, &ts->f);
  6526. ts->pos += x;
  6527. if (ts->duration > 0) {
  6528. if (ts->pos >= ts->duration * 8)
  6529. return -1;
  6530. }
  6531. return 0;
  6532. }
  6533. static struct ast_generator tonepair = {
  6534. .alloc = tonepair_alloc,
  6535. .release = tonepair_release,
  6536. .generate = tonepair_generator,
  6537. };
  6538. int ast_tonepair_start(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
  6539. {
  6540. struct tonepair_def d = { 0, };
  6541. d.freq1 = freq1;
  6542. d.freq2 = freq2;
  6543. d.duration = duration;
  6544. d.vol = (vol < 1) ? 8192 : vol; /* force invalid to 8192 */
  6545. if (ast_activate_generator(chan, &tonepair, &d))
  6546. return -1;
  6547. return 0;
  6548. }
  6549. void ast_tonepair_stop(struct ast_channel *chan)
  6550. {
  6551. ast_deactivate_generator(chan);
  6552. }
  6553. int ast_tonepair(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
  6554. {
  6555. int res;
  6556. if ((res = ast_tonepair_start(chan, freq1, freq2, duration, vol)))
  6557. return res;
  6558. /* Give us some wiggle room */
  6559. while (ast_channel_generatordata(chan) && ast_waitfor(chan, 100) >= 0) {
  6560. struct ast_frame *f = ast_read(chan);
  6561. if (f)
  6562. ast_frfree(f);
  6563. else
  6564. return -1;
  6565. }
  6566. return 0;
  6567. }
  6568. ast_group_t ast_get_group(const char *s)
  6569. {
  6570. char *piece;
  6571. char *c;
  6572. int start=0, finish=0, x;
  6573. ast_group_t group = 0;
  6574. if (ast_strlen_zero(s))
  6575. return 0;
  6576. c = ast_strdupa(s);
  6577. while ((piece = strsep(&c, ","))) {
  6578. if (sscanf(piece, "%30d-%30d", &start, &finish) == 2) {
  6579. /* Range */
  6580. } else if (sscanf(piece, "%30d", &start)) {
  6581. /* Just one */
  6582. finish = start;
  6583. } else {
  6584. ast_log(LOG_ERROR, "Syntax error parsing group configuration '%s' at '%s'. Ignoring.\n", s, piece);
  6585. continue;
  6586. }
  6587. for (x = start; x <= finish; x++) {
  6588. if ((x > 63) || (x < 0)) {
  6589. ast_log(LOG_WARNING, "Ignoring invalid group %d (maximum group is 63)\n", x);
  6590. } else
  6591. group |= ((ast_group_t) 1 << x);
  6592. }
  6593. }
  6594. return group;
  6595. }
  6596. /*! \brief Named group member structure */
  6597. struct namedgroup_member {
  6598. /*! Pre-built hash of group member name. */
  6599. unsigned int hash;
  6600. /*! Group member name. (End allocation of name string.) */
  6601. char name[1];
  6602. };
  6603. /*! \brief Comparison function used for named group container */
  6604. static int namedgroup_cmp_cb(void *obj, void *arg, int flags)
  6605. {
  6606. const struct namedgroup_member *an = obj;
  6607. const struct namedgroup_member *bn = arg;
  6608. return strcmp(an->name, bn->name) ? 0 : CMP_MATCH | CMP_STOP;
  6609. }
  6610. /*! \brief Hashing function used for named group container */
  6611. static int namedgroup_hash_cb(const void *obj, const int flags)
  6612. {
  6613. const struct namedgroup_member *member = obj;
  6614. return member->hash;
  6615. }
  6616. struct ast_namedgroups *ast_get_namedgroups(const char *s)
  6617. {
  6618. struct ao2_container *namedgroups;
  6619. char *piece;
  6620. char *c;
  6621. if (!s) {
  6622. return NULL;
  6623. }
  6624. /*! \brief Remove leading and trailing whitespace */
  6625. c = ast_trim_blanks(ast_strdupa(ast_skip_blanks(s)));
  6626. if (ast_strlen_zero(c)) {
  6627. return NULL;
  6628. }
  6629. namedgroups = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, 19,
  6630. namedgroup_hash_cb, NULL, namedgroup_cmp_cb);
  6631. if (!namedgroups) {
  6632. return NULL;
  6633. }
  6634. while ((piece = strsep(&c, ","))) {
  6635. struct namedgroup_member *member;
  6636. size_t len;
  6637. /* remove leading/trailing whitespace */
  6638. piece = ast_strip(piece);
  6639. len = strlen(piece);
  6640. if (!len) {
  6641. continue;
  6642. }
  6643. member = ao2_alloc_options(sizeof(*member) + len, NULL, AO2_ALLOC_OPT_LOCK_NOLOCK);
  6644. if (!member) {
  6645. ao2_ref(namedgroups, -1);
  6646. return NULL;
  6647. }
  6648. strcpy(member->name, piece);/* Safe */
  6649. member->hash = ast_str_hash(member->name);
  6650. /* every group name may exist only once, delete duplicates */
  6651. ao2_find(namedgroups, member, OBJ_POINTER | OBJ_UNLINK | OBJ_NODATA);
  6652. ao2_link(namedgroups, member);
  6653. ao2_ref(member, -1);
  6654. }
  6655. if (!ao2_container_count(namedgroups)) {
  6656. /* There were no group names specified. */
  6657. ao2_ref(namedgroups, -1);
  6658. namedgroups = NULL;
  6659. }
  6660. return (struct ast_namedgroups *) namedgroups;
  6661. }
  6662. struct ast_namedgroups *ast_unref_namedgroups(struct ast_namedgroups *groups)
  6663. {
  6664. ao2_cleanup(groups);
  6665. return NULL;
  6666. }
  6667. struct ast_namedgroups *ast_ref_namedgroups(struct ast_namedgroups *groups)
  6668. {
  6669. if (groups) {
  6670. ao2_ref(groups, 1);
  6671. }
  6672. return groups;
  6673. }
  6674. static int (*ast_moh_start_ptr)(struct ast_channel *, const char *, const char *) = NULL;
  6675. static void (*ast_moh_stop_ptr)(struct ast_channel *) = NULL;
  6676. static void (*ast_moh_cleanup_ptr)(struct ast_channel *) = NULL;
  6677. void ast_install_music_functions(int (*start_ptr)(struct ast_channel *, const char *, const char *),
  6678. void (*stop_ptr)(struct ast_channel *),
  6679. void (*cleanup_ptr)(struct ast_channel *))
  6680. {
  6681. ast_moh_start_ptr = start_ptr;
  6682. ast_moh_stop_ptr = stop_ptr;
  6683. ast_moh_cleanup_ptr = cleanup_ptr;
  6684. }
  6685. void ast_uninstall_music_functions(void)
  6686. {
  6687. ast_moh_start_ptr = NULL;
  6688. ast_moh_stop_ptr = NULL;
  6689. ast_moh_cleanup_ptr = NULL;
  6690. }
  6691. int ast_moh_start(struct ast_channel *chan, const char *mclass, const char *interpclass)
  6692. {
  6693. if (ast_moh_start_ptr)
  6694. return ast_moh_start_ptr(chan, mclass, interpclass);
  6695. ast_verb(3, "Music class %s requested but no musiconhold loaded.\n", mclass ? mclass : (interpclass ? interpclass : "default"));
  6696. return -1;
  6697. }
  6698. void ast_moh_stop(struct ast_channel *chan)
  6699. {
  6700. if (ast_moh_stop_ptr)
  6701. ast_moh_stop_ptr(chan);
  6702. }
  6703. void ast_moh_cleanup(struct ast_channel *chan)
  6704. {
  6705. if (ast_moh_cleanup_ptr)
  6706. ast_moh_cleanup_ptr(chan);
  6707. }
  6708. static int ast_channel_hash_cb(const void *obj, const int flags)
  6709. {
  6710. const char *name = (flags & OBJ_KEY) ? obj : ast_channel_name((struct ast_channel *) obj);
  6711. /* If the name isn't set, return 0 so that the ao2_find() search will
  6712. * start in the first bucket. */
  6713. if (ast_strlen_zero(name)) {
  6714. return 0;
  6715. }
  6716. return ast_str_case_hash(name);
  6717. }
  6718. int ast_plc_reload(void)
  6719. {
  6720. struct ast_variable *var;
  6721. struct ast_flags config_flags = { 0 };
  6722. struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
  6723. if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID)
  6724. return 0;
  6725. for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
  6726. if (!strcasecmp(var->name, "genericplc")) {
  6727. ast_set2_flag(&ast_options, ast_true(var->value), AST_OPT_FLAG_GENERIC_PLC);
  6728. } else if (!strcasecmp(var->name, "genericplc_on_equal_codecs")) {
  6729. ast_set2_flag(&ast_options, ast_true(var->value), AST_OPT_FLAG_GENERIC_PLC_ON_EQUAL_CODECS);
  6730. }
  6731. }
  6732. /*
  6733. * Force on_equal_codecs to false if generic_plc is false.
  6734. */
  6735. if (!ast_opt_generic_plc) {
  6736. ast_set2_flag(&ast_options, 0, AST_OPT_FLAG_GENERIC_PLC_ON_EQUAL_CODECS);
  6737. }
  6738. ast_config_destroy(cfg);
  6739. return 0;
  6740. }
  6741. /*!
  6742. * \internal
  6743. * \brief Implements the channels provider.
  6744. */
  6745. static int data_channels_provider_handler(const struct ast_data_search *search,
  6746. struct ast_data *root)
  6747. {
  6748. struct ast_channel *c;
  6749. struct ast_channel_iterator *iter = NULL;
  6750. struct ast_data *data_channel;
  6751. for (iter = ast_channel_iterator_all_new();
  6752. iter && (c = ast_channel_iterator_next(iter)); ast_channel_unref(c)) {
  6753. ast_channel_lock(c);
  6754. data_channel = ast_data_add_node(root, "channel");
  6755. if (!data_channel) {
  6756. ast_channel_unlock(c);
  6757. continue;
  6758. }
  6759. if (ast_channel_data_add_structure(data_channel, c, 1) < 0) {
  6760. ast_log(LOG_ERROR, "Unable to add channel structure for channel: %s\n", ast_channel_name(c));
  6761. }
  6762. ast_channel_unlock(c);
  6763. if (!ast_data_search_match(search, data_channel)) {
  6764. ast_data_remove_node(root, data_channel);
  6765. }
  6766. }
  6767. if (iter) {
  6768. ast_channel_iterator_destroy(iter);
  6769. }
  6770. return 0;
  6771. }
  6772. /*!
  6773. * \internal
  6774. * \brief Implements the channeltypes provider.
  6775. */
  6776. static int data_channeltypes_provider_handler(const struct ast_data_search *search,
  6777. struct ast_data *data_root)
  6778. {
  6779. struct chanlist *cl;
  6780. struct ast_data *data_type;
  6781. AST_RWLIST_RDLOCK(&backends);
  6782. AST_RWLIST_TRAVERSE(&backends, cl, list) {
  6783. data_type = ast_data_add_node(data_root, "type");
  6784. if (!data_type) {
  6785. continue;
  6786. }
  6787. ast_data_add_str(data_type, "name", cl->tech->type);
  6788. ast_data_add_str(data_type, "description", cl->tech->description);
  6789. ast_data_add_bool(data_type, "devicestate", cl->tech->devicestate ? 1 : 0);
  6790. ast_data_add_bool(data_type, "indications", cl->tech->indicate ? 1 : 0);
  6791. ast_data_add_bool(data_type, "transfer", cl->tech->transfer ? 1 : 0);
  6792. ast_data_add_bool(data_type, "send_digit_begin", cl->tech->send_digit_begin ? 1 : 0);
  6793. ast_data_add_bool(data_type, "send_digit_end", cl->tech->send_digit_end ? 1 : 0);
  6794. ast_data_add_bool(data_type, "call", cl->tech->call ? 1 : 0);
  6795. ast_data_add_bool(data_type, "hangup", cl->tech->hangup ? 1 : 0);
  6796. ast_data_add_bool(data_type, "answer", cl->tech->answer ? 1 : 0);
  6797. ast_data_add_bool(data_type, "read", cl->tech->read ? 1 : 0);
  6798. ast_data_add_bool(data_type, "write", cl->tech->write ? 1 : 0);
  6799. ast_data_add_bool(data_type, "send_text", cl->tech->send_text ? 1 : 0);
  6800. ast_data_add_bool(data_type, "send_image", cl->tech->send_image ? 1 : 0);
  6801. ast_data_add_bool(data_type, "send_html", cl->tech->send_html ? 1 : 0);
  6802. ast_data_add_bool(data_type, "exception", cl->tech->exception ? 1 : 0);
  6803. ast_data_add_bool(data_type, "early_bridge", cl->tech->early_bridge ? 1 : 0);
  6804. ast_data_add_bool(data_type, "fixup", cl->tech->fixup ? 1 : 0);
  6805. ast_data_add_bool(data_type, "setoption", cl->tech->setoption ? 1 : 0);
  6806. ast_data_add_bool(data_type, "queryoption", cl->tech->queryoption ? 1 : 0);
  6807. ast_data_add_bool(data_type, "write_video", cl->tech->write_video ? 1 : 0);
  6808. ast_data_add_bool(data_type, "write_text", cl->tech->write_text ? 1 : 0);
  6809. ast_data_add_bool(data_type, "func_channel_read", cl->tech->func_channel_read ? 1 : 0);
  6810. ast_data_add_bool(data_type, "func_channel_write", cl->tech->func_channel_write ? 1 : 0);
  6811. ast_data_add_bool(data_type, "get_pvt_uniqueid", cl->tech->get_pvt_uniqueid ? 1 : 0);
  6812. ast_data_add_bool(data_type, "cc_callback", cl->tech->cc_callback ? 1 : 0);
  6813. ast_data_add_codecs(data_type, "capabilities", cl->tech->capabilities);
  6814. if (!ast_data_search_match(search, data_type)) {
  6815. ast_data_remove_node(data_root, data_type);
  6816. }
  6817. }
  6818. AST_RWLIST_UNLOCK(&backends);
  6819. return 0;
  6820. }
  6821. /*!
  6822. * \internal
  6823. * \brief /asterisk/core/channels provider.
  6824. */
  6825. static const struct ast_data_handler channels_provider = {
  6826. .version = AST_DATA_HANDLER_VERSION,
  6827. .get = data_channels_provider_handler
  6828. };
  6829. /*!
  6830. * \internal
  6831. * \brief /asterisk/core/channeltypes provider.
  6832. */
  6833. static const struct ast_data_handler channeltypes_provider = {
  6834. .version = AST_DATA_HANDLER_VERSION,
  6835. .get = data_channeltypes_provider_handler
  6836. };
  6837. static const struct ast_data_entry channel_providers[] = {
  6838. AST_DATA_ENTRY("/asterisk/core/channels", &channels_provider),
  6839. AST_DATA_ENTRY("/asterisk/core/channeltypes", &channeltypes_provider),
  6840. };
  6841. /*!
  6842. * \internal
  6843. * \brief Print channel object key (name).
  6844. * \since 12.0.0
  6845. *
  6846. * \param v_obj A pointer to the object we want the key printed.
  6847. * \param where User data needed by prnt to determine where to put output.
  6848. * \param prnt Print output callback function to use.
  6849. *
  6850. * \return Nothing
  6851. */
  6852. static void prnt_channel_key(void *v_obj, void *where, ao2_prnt_fn *prnt)
  6853. {
  6854. struct ast_channel *chan = v_obj;
  6855. if (!chan) {
  6856. return;
  6857. }
  6858. prnt(where, "%s", ast_channel_name(chan));
  6859. }
  6860. /*!
  6861. * \brief List of channel variables to append to all channel-related events.
  6862. */
  6863. struct manager_channel_variable {
  6864. AST_LIST_ENTRY(manager_channel_variable) entry;
  6865. unsigned int isfunc:1;
  6866. char name[];
  6867. };
  6868. static AST_RWLIST_HEAD_STATIC(channelvars, manager_channel_variable);
  6869. static void free_channelvars(void)
  6870. {
  6871. struct manager_channel_variable *var;
  6872. AST_RWLIST_WRLOCK(&channelvars);
  6873. while ((var = AST_RWLIST_REMOVE_HEAD(&channelvars, entry))) {
  6874. ast_free(var);
  6875. }
  6876. AST_RWLIST_UNLOCK(&channelvars);
  6877. }
  6878. int ast_channel_has_manager_vars(void)
  6879. {
  6880. int vars_present;
  6881. AST_RWLIST_RDLOCK(&channelvars);
  6882. vars_present = !AST_LIST_EMPTY(&channelvars);
  6883. AST_RWLIST_UNLOCK(&channelvars);
  6884. return vars_present;
  6885. }
  6886. void ast_channel_set_manager_vars(size_t varc, char **vars)
  6887. {
  6888. size_t i;
  6889. free_channelvars();
  6890. AST_RWLIST_WRLOCK(&channelvars);
  6891. for (i = 0; i < varc; ++i) {
  6892. const char *var = vars[i];
  6893. struct manager_channel_variable *mcv;
  6894. if (!(mcv = ast_calloc(1, sizeof(*mcv) + strlen(var) + 1))) {
  6895. break;
  6896. }
  6897. strcpy(mcv->name, var); /* SAFE */
  6898. if (strchr(var, '(')) {
  6899. mcv->isfunc = 1;
  6900. }
  6901. AST_RWLIST_INSERT_TAIL(&channelvars, mcv, entry);
  6902. }
  6903. AST_RWLIST_UNLOCK(&channelvars);
  6904. }
  6905. /*!
  6906. * \brief Destructor for lists of variables.
  6907. * \param obj AO2 object.
  6908. */
  6909. static void varshead_dtor(void *obj)
  6910. {
  6911. struct varshead *head = obj;
  6912. struct ast_var_t *var;
  6913. while ((var = AST_RWLIST_REMOVE_HEAD(head, entries))) {
  6914. ast_var_delete(var);
  6915. }
  6916. }
  6917. struct varshead *ast_channel_get_vars(struct ast_channel *chan)
  6918. {
  6919. RAII_VAR(struct varshead *, ret, NULL, ao2_cleanup);
  6920. struct ast_var_t *cv;
  6921. ret = ao2_alloc(sizeof(*ret), varshead_dtor);
  6922. if (!ret) {
  6923. return NULL;
  6924. }
  6925. AST_LIST_TRAVERSE(ast_channel_varshead(chan), cv, entries) {
  6926. struct ast_var_t *var = ast_var_assign(ast_var_name(cv), ast_var_value(cv));
  6927. if (!var) {
  6928. return NULL;
  6929. }
  6930. AST_LIST_INSERT_TAIL(ret, var, entries);
  6931. }
  6932. ao2_ref(ret, +1);
  6933. return ret;
  6934. }
  6935. struct varshead *ast_channel_get_manager_vars(struct ast_channel *chan)
  6936. {
  6937. RAII_VAR(struct varshead *, ret, NULL, ao2_cleanup);
  6938. RAII_VAR(struct ast_str *, tmp, NULL, ast_free);
  6939. struct manager_channel_variable *mcv;
  6940. SCOPED_LOCK(lock, &channelvars, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
  6941. if (AST_LIST_EMPTY(&channelvars)) {
  6942. return NULL;
  6943. }
  6944. ret = ao2_alloc(sizeof(*ret), varshead_dtor);
  6945. tmp = ast_str_create(16);
  6946. if (!ret || !tmp) {
  6947. return NULL;
  6948. }
  6949. AST_LIST_TRAVERSE(&channelvars, mcv, entry) {
  6950. const char *val = NULL;
  6951. struct ast_var_t *var;
  6952. if (mcv->isfunc) {
  6953. if (ast_func_read2(chan, mcv->name, &tmp, 0) == 0) {
  6954. val = ast_str_buffer(tmp);
  6955. } else {
  6956. ast_log(LOG_ERROR,
  6957. "Error invoking function %s\n", mcv->name);
  6958. }
  6959. } else {
  6960. val = pbx_builtin_getvar_helper(chan, mcv->name);
  6961. }
  6962. var = ast_var_assign(mcv->name, val ? val : "");
  6963. if (!var) {
  6964. return NULL;
  6965. }
  6966. AST_RWLIST_INSERT_TAIL(ret, var, entries);
  6967. }
  6968. ao2_ref(ret, +1);
  6969. return ret;
  6970. }
  6971. static void channels_shutdown(void)
  6972. {
  6973. free_channelvars();
  6974. ast_data_unregister(NULL);
  6975. ast_cli_unregister_multiple(cli_channel, ARRAY_LEN(cli_channel));
  6976. if (channels) {
  6977. ao2_container_unregister("channels");
  6978. ao2_ref(channels, -1);
  6979. channels = NULL;
  6980. }
  6981. ast_channel_unregister(&surrogate_tech);
  6982. }
  6983. int ast_channels_init(void)
  6984. {
  6985. channels = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NUM_CHANNEL_BUCKETS,
  6986. ast_channel_hash_cb, NULL, ast_channel_cmp_cb);
  6987. if (!channels) {
  6988. return -1;
  6989. }
  6990. ao2_container_register("channels", channels, prnt_channel_key);
  6991. ast_channel_register(&surrogate_tech);
  6992. ast_stasis_channels_init();
  6993. ast_cli_register_multiple(cli_channel, ARRAY_LEN(cli_channel));
  6994. ast_data_register_multiple_core(channel_providers, ARRAY_LEN(channel_providers));
  6995. ast_plc_reload();
  6996. ast_register_cleanup(channels_shutdown);
  6997. return 0;
  6998. }
  6999. /*! \brief Print call group and pickup group ---*/
  7000. char *ast_print_group(char *buf, int buflen, ast_group_t group)
  7001. {
  7002. unsigned int i;
  7003. int first = 1;
  7004. char num[3];
  7005. buf[0] = '\0';
  7006. if (!group) /* Return empty string if no group */
  7007. return buf;
  7008. for (i = 0; i <= 63; i++) { /* Max group is 63 */
  7009. if (group & ((ast_group_t) 1 << i)) {
  7010. if (!first) {
  7011. strncat(buf, ", ", buflen - strlen(buf) - 1);
  7012. } else {
  7013. first = 0;
  7014. }
  7015. snprintf(num, sizeof(num), "%u", i);
  7016. strncat(buf, num, buflen - strlen(buf) - 1);
  7017. }
  7018. }
  7019. return buf;
  7020. }
  7021. char *ast_print_namedgroups(struct ast_str **buf, struct ast_namedgroups *group)
  7022. {
  7023. struct ao2_container *grp = (struct ao2_container *) group;
  7024. struct namedgroup_member *ng;
  7025. int first = 1;
  7026. struct ao2_iterator it;
  7027. if (!grp) {
  7028. return ast_str_buffer(*buf);
  7029. }
  7030. for (it = ao2_iterator_init(grp, 0); (ng = ao2_iterator_next(&it)); ao2_ref(ng, -1)) {
  7031. if (!first) {
  7032. ast_str_append(buf, 0, ", ");
  7033. } else {
  7034. first = 0;
  7035. }
  7036. ast_str_append(buf, 0, "%s", ng->name);
  7037. }
  7038. ao2_iterator_destroy(&it);
  7039. return ast_str_buffer(*buf);
  7040. }
  7041. static int namedgroup_match(void *obj, void *arg, int flags)
  7042. {
  7043. void *match;
  7044. match = ao2_find(arg, obj, OBJ_POINTER);
  7045. ao2_cleanup(match);
  7046. return match ? CMP_MATCH | CMP_STOP : 0;
  7047. }
  7048. int ast_namedgroups_intersect(struct ast_namedgroups *a, struct ast_namedgroups *b)
  7049. {
  7050. void *match;
  7051. struct ao2_container *group_a = (struct ao2_container *) a;
  7052. struct ao2_container *group_b = (struct ao2_container *) b;
  7053. if (!a || !b) {
  7054. return 0;
  7055. }
  7056. /*
  7057. * Do groups a and b intersect? Since a and b are hash tables,
  7058. * the average time complexity is:
  7059. * O(a.count <= b.count ? a.count : b.count)
  7060. */
  7061. if (ao2_container_count(group_b) < ao2_container_count(group_a)) {
  7062. /* Traverse over the smaller group. */
  7063. SWAP(group_a, group_b);
  7064. }
  7065. match = ao2_callback(group_a, 0, namedgroup_match, group_b);
  7066. ao2_cleanup(match);
  7067. return match != NULL;
  7068. }
  7069. void ast_set_variables(struct ast_channel *chan, struct ast_variable *vars)
  7070. {
  7071. struct ast_variable *cur;
  7072. for (cur = vars; cur; cur = cur->next) {
  7073. pbx_builtin_setvar_helper(chan, cur->name, cur->value);
  7074. }
  7075. }
  7076. static void *silence_generator_alloc(struct ast_channel *chan, void *data)
  7077. {
  7078. /* just store the data pointer in the channel structure */
  7079. return data;
  7080. }
  7081. static void silence_generator_release(struct ast_channel *chan, void *data)
  7082. {
  7083. /* nothing to do */
  7084. }
  7085. static int silence_generator_generate(struct ast_channel *chan, void *data, int len, int samples)
  7086. {
  7087. short buf[samples];
  7088. struct ast_frame frame = {
  7089. .frametype = AST_FRAME_VOICE,
  7090. .data.ptr = buf,
  7091. .samples = samples,
  7092. .datalen = sizeof(buf),
  7093. };
  7094. frame.subclass.format = ast_format_slin;
  7095. memset(buf, 0, sizeof(buf));
  7096. if (ast_write(chan, &frame))
  7097. return -1;
  7098. return 0;
  7099. }
  7100. static struct ast_generator silence_generator = {
  7101. .alloc = silence_generator_alloc,
  7102. .release = silence_generator_release,
  7103. .generate = silence_generator_generate,
  7104. };
  7105. struct ast_silence_generator {
  7106. struct ast_format *old_write_format;
  7107. };
  7108. struct ast_silence_generator *ast_channel_start_silence_generator(struct ast_channel *chan)
  7109. {
  7110. struct ast_silence_generator *state;
  7111. if (!(state = ast_calloc(1, sizeof(*state)))) {
  7112. return NULL;
  7113. }
  7114. state->old_write_format = ao2_bump(ast_channel_writeformat(chan));
  7115. if (ast_set_write_format(chan, ast_format_slin) < 0) {
  7116. ast_log(LOG_ERROR, "Could not set write format to SLINEAR\n");
  7117. ast_free(state);
  7118. return NULL;
  7119. }
  7120. ast_activate_generator(chan, &silence_generator, state);
  7121. ast_debug(1, "Started silence generator on '%s'\n", ast_channel_name(chan));
  7122. return state;
  7123. }
  7124. static int deactivate_silence_generator(struct ast_channel *chan)
  7125. {
  7126. ast_channel_lock(chan);
  7127. if (!ast_channel_generatordata(chan)) {
  7128. ast_debug(1, "Trying to stop silence generator when there is no generator on '%s'\n",
  7129. ast_channel_name(chan));
  7130. ast_channel_unlock(chan);
  7131. return 0;
  7132. }
  7133. if (ast_channel_generator(chan) != &silence_generator) {
  7134. ast_debug(1, "Trying to stop silence generator when it is not the current generator on '%s'\n",
  7135. ast_channel_name(chan));
  7136. ast_channel_unlock(chan);
  7137. return 0;
  7138. }
  7139. deactivate_generator_nolock(chan);
  7140. ast_channel_unlock(chan);
  7141. return 1;
  7142. }
  7143. void ast_channel_stop_silence_generator(struct ast_channel *chan, struct ast_silence_generator *state)
  7144. {
  7145. if (!state) {
  7146. return;
  7147. }
  7148. if (deactivate_silence_generator(chan)) {
  7149. ast_debug(1, "Stopped silence generator on '%s'\n", ast_channel_name(chan));
  7150. if (ast_set_write_format(chan, state->old_write_format) < 0) {
  7151. ast_log(LOG_ERROR, "Could not return write format to its original state\n");
  7152. }
  7153. }
  7154. ao2_cleanup(state->old_write_format);
  7155. ast_free(state);
  7156. }
  7157. /*! \ brief Convert channel reloadreason (ENUM) to text string for manager event */
  7158. const char *channelreloadreason2txt(enum channelreloadreason reason)
  7159. {
  7160. switch (reason) {
  7161. case CHANNEL_MODULE_LOAD:
  7162. return "LOAD (Channel module load)";
  7163. case CHANNEL_MODULE_RELOAD:
  7164. return "RELOAD (Channel module reload)";
  7165. case CHANNEL_CLI_RELOAD:
  7166. return "CLIRELOAD (Channel module reload by CLI command)";
  7167. default:
  7168. return "MANAGERRELOAD (Channel module reload by manager)";
  7169. }
  7170. };
  7171. /*
  7172. * Wrappers for various ast_say_*() functions that call the full version
  7173. * of the same functions.
  7174. * The proper place would be say.c, but that file is optional and one
  7175. * must be able to build asterisk even without it (using a loadable 'say'
  7176. * implementation that only supplies the 'full' version of the functions.
  7177. */
  7178. int ast_say_number(struct ast_channel *chan, int num,
  7179. const char *ints, const char *language, const char *options)
  7180. {
  7181. return ast_say_number_full(chan, num, ints, language, options, -1, -1);
  7182. }
  7183. int ast_say_enumeration(struct ast_channel *chan, int num,
  7184. const char *ints, const char *language, const char *options)
  7185. {
  7186. return ast_say_enumeration_full(chan, num, ints, language, options, -1, -1);
  7187. }
  7188. int ast_say_digits(struct ast_channel *chan, int num,
  7189. const char *ints, const char *lang)
  7190. {
  7191. return ast_say_digits_full(chan, num, ints, lang, -1, -1);
  7192. }
  7193. int ast_say_digit_str(struct ast_channel *chan, const char *str,
  7194. const char *ints, const char *lang)
  7195. {
  7196. return ast_say_digit_str_full(chan, str, ints, lang, -1, -1);
  7197. }
  7198. int ast_say_character_str(struct ast_channel *chan, const char *str,
  7199. const char *ints, const char *lang, enum ast_say_case_sensitivity sensitivity)
  7200. {
  7201. return ast_say_character_str_full(chan, str, ints, lang, sensitivity, -1, -1);
  7202. }
  7203. int ast_say_phonetic_str(struct ast_channel *chan, const char *str,
  7204. const char *ints, const char *lang)
  7205. {
  7206. return ast_say_phonetic_str_full(chan, str, ints, lang, -1, -1);
  7207. }
  7208. int ast_say_digits_full(struct ast_channel *chan, int num,
  7209. const char *ints, const char *lang, int audiofd, int ctrlfd)
  7210. {
  7211. char buf[256];
  7212. snprintf(buf, sizeof(buf), "%d", num);
  7213. return ast_say_digit_str_full(chan, buf, ints, lang, audiofd, ctrlfd);
  7214. }
  7215. void ast_connected_line_copy_from_caller(struct ast_party_connected_line *dest, const struct ast_party_caller *src)
  7216. {
  7217. ast_party_id_copy(&dest->id, &src->id);
  7218. ast_party_id_copy(&dest->ani, &src->ani);
  7219. dest->ani2 = src->ani2;
  7220. }
  7221. void ast_connected_line_copy_to_caller(struct ast_party_caller *dest, const struct ast_party_connected_line *src)
  7222. {
  7223. ast_party_id_copy(&dest->id, &src->id);
  7224. ast_party_id_copy(&dest->ani, &src->ani);
  7225. dest->ani2 = src->ani2;
  7226. }
  7227. void ast_channel_set_connected_line(struct ast_channel *chan, const struct ast_party_connected_line *connected, const struct ast_set_party_connected_line *update)
  7228. {
  7229. if (ast_channel_connected(chan) == connected) {
  7230. /* Don't set to self */
  7231. return;
  7232. }
  7233. ast_channel_lock(chan);
  7234. ast_party_connected_line_set(ast_channel_connected(chan), connected, update);
  7235. ast_channel_publish_snapshot(chan);
  7236. ast_channel_unlock(chan);
  7237. }
  7238. /*! \note Should follow struct ast_party_name */
  7239. struct ast_party_name_ies {
  7240. /*! \brief Subscriber name ie */
  7241. int str;
  7242. /*! \brief Character set ie. */
  7243. int char_set;
  7244. /*! \brief presentation-indicator ie */
  7245. int presentation;
  7246. /*! \brief valid/present ie */
  7247. int valid;
  7248. };
  7249. /*!
  7250. * \internal
  7251. * \since 1.8
  7252. * \brief Build a party name information data frame component.
  7253. *
  7254. * \param data Buffer to fill with the frame data
  7255. * \param datalen Size of the buffer to fill
  7256. * \param name Party name information
  7257. * \param label Name of particular party name
  7258. * \param ies Data frame ie values for the party name components
  7259. *
  7260. * \retval -1 if error
  7261. * \retval Amount of data buffer used
  7262. */
  7263. static int party_name_build_data(unsigned char *data, size_t datalen, const struct ast_party_name *name, const char *label, const struct ast_party_name_ies *ies)
  7264. {
  7265. size_t length;
  7266. size_t pos = 0;
  7267. /*
  7268. * The size of integer values must be fixed in case the frame is
  7269. * shipped to another machine.
  7270. */
  7271. if (name->str) {
  7272. length = strlen(name->str);
  7273. if (datalen < pos + (sizeof(data[0]) * 2) + length) {
  7274. ast_log(LOG_WARNING, "No space left for %s name\n", label);
  7275. return -1;
  7276. }
  7277. data[pos++] = ies->str;
  7278. data[pos++] = length;
  7279. memcpy(data + pos, name->str, length);
  7280. pos += length;
  7281. }
  7282. if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
  7283. ast_log(LOG_WARNING, "No space left for %s name char set\n", label);
  7284. return -1;
  7285. }
  7286. data[pos++] = ies->char_set;
  7287. data[pos++] = 1;
  7288. data[pos++] = name->char_set;
  7289. if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
  7290. ast_log(LOG_WARNING, "No space left for %s name presentation\n", label);
  7291. return -1;
  7292. }
  7293. data[pos++] = ies->presentation;
  7294. data[pos++] = 1;
  7295. data[pos++] = name->presentation;
  7296. if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
  7297. ast_log(LOG_WARNING, "No space left for %s name valid\n", label);
  7298. return -1;
  7299. }
  7300. data[pos++] = ies->valid;
  7301. data[pos++] = 1;
  7302. data[pos++] = name->valid;
  7303. return pos;
  7304. }
  7305. /*! \note Should follow struct ast_party_number */
  7306. struct ast_party_number_ies {
  7307. /*! \brief Subscriber phone number ie */
  7308. int str;
  7309. /*! \brief Type-Of-Number and Numbering-Plan ie */
  7310. int plan;
  7311. /*! \brief presentation-indicator ie */
  7312. int presentation;
  7313. /*! \brief valid/present ie */
  7314. int valid;
  7315. };
  7316. /*!
  7317. * \internal
  7318. * \since 1.8
  7319. * \brief Build a party number information data frame component.
  7320. *
  7321. * \param data Buffer to fill with the frame data
  7322. * \param datalen Size of the buffer to fill
  7323. * \param number Party number information
  7324. * \param label Name of particular party number
  7325. * \param ies Data frame ie values for the party number components
  7326. *
  7327. * \retval -1 if error
  7328. * \retval Amount of data buffer used
  7329. */
  7330. static int party_number_build_data(unsigned char *data, size_t datalen, const struct ast_party_number *number, const char *label, const struct ast_party_number_ies *ies)
  7331. {
  7332. size_t length;
  7333. size_t pos = 0;
  7334. /*
  7335. * The size of integer values must be fixed in case the frame is
  7336. * shipped to another machine.
  7337. */
  7338. if (number->str) {
  7339. length = strlen(number->str);
  7340. if (datalen < pos + (sizeof(data[0]) * 2) + length) {
  7341. ast_log(LOG_WARNING, "No space left for %s number\n", label);
  7342. return -1;
  7343. }
  7344. data[pos++] = ies->str;
  7345. data[pos++] = length;
  7346. memcpy(data + pos, number->str, length);
  7347. pos += length;
  7348. }
  7349. if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
  7350. ast_log(LOG_WARNING, "No space left for %s numbering plan\n", label);
  7351. return -1;
  7352. }
  7353. data[pos++] = ies->plan;
  7354. data[pos++] = 1;
  7355. data[pos++] = number->plan;
  7356. if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
  7357. ast_log(LOG_WARNING, "No space left for %s number presentation\n", label);
  7358. return -1;
  7359. }
  7360. data[pos++] = ies->presentation;
  7361. data[pos++] = 1;
  7362. data[pos++] = number->presentation;
  7363. if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
  7364. ast_log(LOG_WARNING, "No space left for %s number valid\n", label);
  7365. return -1;
  7366. }
  7367. data[pos++] = ies->valid;
  7368. data[pos++] = 1;
  7369. data[pos++] = number->valid;
  7370. return pos;
  7371. }
  7372. /*! \note Should follow struct ast_party_subaddress */
  7373. struct ast_party_subaddress_ies {
  7374. /*! \brief subaddress ie. */
  7375. int str;
  7376. /*! \brief subaddress type ie */
  7377. int type;
  7378. /*! \brief odd/even indicator ie */
  7379. int odd_even_indicator;
  7380. /*! \brief valid/present ie */
  7381. int valid;
  7382. };
  7383. /*!
  7384. * \internal
  7385. * \since 1.8
  7386. * \brief Build a party subaddress information data frame component.
  7387. *
  7388. * \param data Buffer to fill with the frame data
  7389. * \param datalen Size of the buffer to fill
  7390. * \param subaddress Party subaddress information
  7391. * \param label Name of particular party subaddress
  7392. * \param ies Data frame ie values for the party subaddress components
  7393. *
  7394. * \retval -1 if error
  7395. * \retval Amount of data buffer used
  7396. */
  7397. static int party_subaddress_build_data(unsigned char *data, size_t datalen, const struct ast_party_subaddress *subaddress, const char *label, const struct ast_party_subaddress_ies *ies)
  7398. {
  7399. size_t length;
  7400. size_t pos = 0;
  7401. /*
  7402. * The size of integer values must be fixed in case the frame is
  7403. * shipped to another machine.
  7404. */
  7405. if (subaddress->str) {
  7406. length = strlen(subaddress->str);
  7407. if (datalen < pos + (sizeof(data[0]) * 2) + length) {
  7408. ast_log(LOG_WARNING, "No space left for %s subaddress\n", label);
  7409. return -1;
  7410. }
  7411. data[pos++] = ies->str;
  7412. data[pos++] = length;
  7413. memcpy(data + pos, subaddress->str, length);
  7414. pos += length;
  7415. }
  7416. if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
  7417. ast_log(LOG_WARNING, "No space left for %s type of subaddress\n", label);
  7418. return -1;
  7419. }
  7420. data[pos++] = ies->type;
  7421. data[pos++] = 1;
  7422. data[pos++] = subaddress->type;
  7423. if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
  7424. ast_log(LOG_WARNING,
  7425. "No space left for %s subaddress odd-even indicator\n", label);
  7426. return -1;
  7427. }
  7428. data[pos++] = ies->odd_even_indicator;
  7429. data[pos++] = 1;
  7430. data[pos++] = subaddress->odd_even_indicator;
  7431. if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
  7432. ast_log(LOG_WARNING, "No space left for %s subaddress valid\n", label);
  7433. return -1;
  7434. }
  7435. data[pos++] = ies->valid;
  7436. data[pos++] = 1;
  7437. data[pos++] = subaddress->valid;
  7438. return pos;
  7439. }
  7440. /*! \note Should follow struct ast_party_id */
  7441. struct ast_party_id_ies {
  7442. /*! \brief Subscriber name ies */
  7443. struct ast_party_name_ies name;
  7444. /*! \brief Subscriber phone number ies */
  7445. struct ast_party_number_ies number;
  7446. /*! \brief Subscriber subaddress ies. */
  7447. struct ast_party_subaddress_ies subaddress;
  7448. /*! \brief User party id tag ie. */
  7449. int tag;
  7450. /*!
  7451. * \brief Combined name and number presentation ie.
  7452. * \note Not sent if value is zero.
  7453. */
  7454. int combined_presentation;
  7455. };
  7456. /*!
  7457. * \internal
  7458. * \since 1.8
  7459. * \brief Build a party id information data frame component.
  7460. *
  7461. * \param data Buffer to fill with the frame data
  7462. * \param datalen Size of the buffer to fill
  7463. * \param id Party id information
  7464. * \param label Name of particular party id
  7465. * \param ies Data frame ie values for the party id components
  7466. * \param update What id information to build. NULL if all.
  7467. *
  7468. * \retval -1 if error
  7469. * \retval Amount of data buffer used
  7470. */
  7471. static int party_id_build_data(unsigned char *data, size_t datalen,
  7472. const struct ast_party_id *id, const char *label, const struct ast_party_id_ies *ies,
  7473. const struct ast_set_party_id *update)
  7474. {
  7475. size_t length;
  7476. size_t pos = 0;
  7477. int res;
  7478. /*
  7479. * The size of integer values must be fixed in case the frame is
  7480. * shipped to another machine.
  7481. */
  7482. if (!update || update->name) {
  7483. res = party_name_build_data(data + pos, datalen - pos, &id->name, label,
  7484. &ies->name);
  7485. if (res < 0) {
  7486. return -1;
  7487. }
  7488. pos += res;
  7489. }
  7490. if (!update || update->number) {
  7491. res = party_number_build_data(data + pos, datalen - pos, &id->number, label,
  7492. &ies->number);
  7493. if (res < 0) {
  7494. return -1;
  7495. }
  7496. pos += res;
  7497. }
  7498. if (!update || update->subaddress) {
  7499. res = party_subaddress_build_data(data + pos, datalen - pos, &id->subaddress,
  7500. label, &ies->subaddress);
  7501. if (res < 0) {
  7502. return -1;
  7503. }
  7504. pos += res;
  7505. }
  7506. /* *************** Party id user tag **************************** */
  7507. if (id->tag) {
  7508. length = strlen(id->tag);
  7509. if (datalen < pos + (sizeof(data[0]) * 2) + length) {
  7510. ast_log(LOG_WARNING, "No space left for %s tag\n", label);
  7511. return -1;
  7512. }
  7513. data[pos++] = ies->tag;
  7514. data[pos++] = length;
  7515. memcpy(data + pos, id->tag, length);
  7516. pos += length;
  7517. }
  7518. /* *************** Party id combined presentation *************** */
  7519. if (ies->combined_presentation && (!update || update->number)) {
  7520. int presentation;
  7521. if (!update || update->name) {
  7522. presentation = ast_party_id_presentation(id);
  7523. } else {
  7524. /*
  7525. * We must compromise because not all the information is available
  7526. * to determine a combined presentation value.
  7527. * We will only send the number presentation instead.
  7528. */
  7529. presentation = id->number.presentation;
  7530. }
  7531. if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
  7532. ast_log(LOG_WARNING, "No space left for %s combined presentation\n", label);
  7533. return -1;
  7534. }
  7535. data[pos++] = ies->combined_presentation;
  7536. data[pos++] = 1;
  7537. data[pos++] = presentation;
  7538. }
  7539. return pos;
  7540. }
  7541. /*!
  7542. * \brief Element identifiers for connected line indication frame data
  7543. * \note Only add to the end of this enum.
  7544. */
  7545. enum {
  7546. AST_CONNECTED_LINE_NUMBER,
  7547. AST_CONNECTED_LINE_NAME,
  7548. AST_CONNECTED_LINE_NUMBER_PLAN,
  7549. AST_CONNECTED_LINE_ID_PRESENTATION,/* Combined number and name presentation. */
  7550. AST_CONNECTED_LINE_SOURCE,
  7551. AST_CONNECTED_LINE_SUBADDRESS,
  7552. AST_CONNECTED_LINE_SUBADDRESS_TYPE,
  7553. AST_CONNECTED_LINE_SUBADDRESS_ODD_EVEN,
  7554. AST_CONNECTED_LINE_SUBADDRESS_VALID,
  7555. AST_CONNECTED_LINE_TAG,
  7556. AST_CONNECTED_LINE_VERSION,
  7557. /*
  7558. * No more party id combined number and name presentation values
  7559. * need to be created.
  7560. */
  7561. AST_CONNECTED_LINE_NAME_VALID,
  7562. AST_CONNECTED_LINE_NAME_CHAR_SET,
  7563. AST_CONNECTED_LINE_NAME_PRESENTATION,
  7564. AST_CONNECTED_LINE_NUMBER_VALID,
  7565. AST_CONNECTED_LINE_NUMBER_PRESENTATION,
  7566. AST_CONNECTED_LINE_PRIV_NUMBER,
  7567. AST_CONNECTED_LINE_PRIV_NUMBER_PLAN,
  7568. AST_CONNECTED_LINE_PRIV_NUMBER_VALID,
  7569. AST_CONNECTED_LINE_PRIV_NUMBER_PRESENTATION,
  7570. AST_CONNECTED_LINE_PRIV_NAME,
  7571. AST_CONNECTED_LINE_PRIV_NAME_VALID,
  7572. AST_CONNECTED_LINE_PRIV_NAME_CHAR_SET,
  7573. AST_CONNECTED_LINE_PRIV_NAME_PRESENTATION,
  7574. AST_CONNECTED_LINE_PRIV_SUBADDRESS,
  7575. AST_CONNECTED_LINE_PRIV_SUBADDRESS_TYPE,
  7576. AST_CONNECTED_LINE_PRIV_SUBADDRESS_ODD_EVEN,
  7577. AST_CONNECTED_LINE_PRIV_SUBADDRESS_VALID,
  7578. AST_CONNECTED_LINE_PRIV_TAG,
  7579. };
  7580. int ast_connected_line_build_data(unsigned char *data, size_t datalen, const struct ast_party_connected_line *connected, const struct ast_set_party_connected_line *update)
  7581. {
  7582. int32_t value;
  7583. size_t pos = 0;
  7584. int res;
  7585. static const struct ast_party_id_ies ies = {
  7586. .name.str = AST_CONNECTED_LINE_NAME,
  7587. .name.char_set = AST_CONNECTED_LINE_NAME_CHAR_SET,
  7588. .name.presentation = AST_CONNECTED_LINE_NAME_PRESENTATION,
  7589. .name.valid = AST_CONNECTED_LINE_NAME_VALID,
  7590. .number.str = AST_CONNECTED_LINE_NUMBER,
  7591. .number.plan = AST_CONNECTED_LINE_NUMBER_PLAN,
  7592. .number.presentation = AST_CONNECTED_LINE_NUMBER_PRESENTATION,
  7593. .number.valid = AST_CONNECTED_LINE_NUMBER_VALID,
  7594. .subaddress.str = AST_CONNECTED_LINE_SUBADDRESS,
  7595. .subaddress.type = AST_CONNECTED_LINE_SUBADDRESS_TYPE,
  7596. .subaddress.odd_even_indicator = AST_CONNECTED_LINE_SUBADDRESS_ODD_EVEN,
  7597. .subaddress.valid = AST_CONNECTED_LINE_SUBADDRESS_VALID,
  7598. .tag = AST_CONNECTED_LINE_TAG,
  7599. .combined_presentation = AST_CONNECTED_LINE_ID_PRESENTATION,
  7600. };
  7601. static const struct ast_party_id_ies priv_ies = {
  7602. .name.str = AST_CONNECTED_LINE_PRIV_NAME,
  7603. .name.char_set = AST_CONNECTED_LINE_PRIV_NAME_CHAR_SET,
  7604. .name.presentation = AST_CONNECTED_LINE_PRIV_NAME_PRESENTATION,
  7605. .name.valid = AST_CONNECTED_LINE_PRIV_NAME_VALID,
  7606. .number.str = AST_CONNECTED_LINE_PRIV_NUMBER,
  7607. .number.plan = AST_CONNECTED_LINE_PRIV_NUMBER_PLAN,
  7608. .number.presentation = AST_CONNECTED_LINE_PRIV_NUMBER_PRESENTATION,
  7609. .number.valid = AST_CONNECTED_LINE_PRIV_NUMBER_VALID,
  7610. .subaddress.str = AST_CONNECTED_LINE_PRIV_SUBADDRESS,
  7611. .subaddress.type = AST_CONNECTED_LINE_PRIV_SUBADDRESS_TYPE,
  7612. .subaddress.odd_even_indicator = AST_CONNECTED_LINE_PRIV_SUBADDRESS_ODD_EVEN,
  7613. .subaddress.valid = AST_CONNECTED_LINE_PRIV_SUBADDRESS_VALID,
  7614. .tag = AST_CONNECTED_LINE_PRIV_TAG,
  7615. .combined_presentation = 0,/* Not sent. */
  7616. };
  7617. /*
  7618. * The size of integer values must be fixed in case the frame is
  7619. * shipped to another machine.
  7620. */
  7621. /* Connected line frame version */
  7622. if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
  7623. ast_log(LOG_WARNING, "No space left for connected line frame version\n");
  7624. return -1;
  7625. }
  7626. data[pos++] = AST_CONNECTED_LINE_VERSION;
  7627. data[pos++] = 1;
  7628. data[pos++] = 2;/* Version 1 did not have a version ie */
  7629. res = party_id_build_data(data + pos, datalen - pos, &connected->id,
  7630. "connected line", &ies, update ? &update->id : NULL);
  7631. if (res < 0) {
  7632. return -1;
  7633. }
  7634. pos += res;
  7635. res = party_id_build_data(data + pos, datalen - pos, &connected->priv,
  7636. "connected line priv", &priv_ies, update ? &update->priv : NULL);
  7637. if (res < 0) {
  7638. return -1;
  7639. }
  7640. pos += res;
  7641. /* Connected line source */
  7642. if (datalen < pos + (sizeof(data[0]) * 2) + sizeof(value)) {
  7643. ast_log(LOG_WARNING, "No space left for connected line source\n");
  7644. return -1;
  7645. }
  7646. data[pos++] = AST_CONNECTED_LINE_SOURCE;
  7647. data[pos++] = sizeof(value);
  7648. value = htonl(connected->source);
  7649. memcpy(data + pos, &value, sizeof(value));
  7650. pos += sizeof(value);
  7651. return pos;
  7652. }
  7653. int ast_connected_line_parse_data(const unsigned char *data, size_t datalen, struct ast_party_connected_line *connected)
  7654. {
  7655. size_t pos;
  7656. unsigned char ie_len;
  7657. unsigned char ie_id;
  7658. int32_t value;
  7659. int frame_version = 1;
  7660. int combined_presentation = 0;
  7661. int got_combined_presentation = 0;/* TRUE if got a combined name and number presentation value. */
  7662. for (pos = 0; pos < datalen; pos += ie_len) {
  7663. if (datalen < pos + sizeof(ie_id) + sizeof(ie_len)) {
  7664. ast_log(LOG_WARNING, "Invalid connected line update\n");
  7665. return -1;
  7666. }
  7667. ie_id = data[pos++];
  7668. ie_len = data[pos++];
  7669. if (datalen < pos + ie_len) {
  7670. ast_log(LOG_WARNING, "Invalid connected line update\n");
  7671. return -1;
  7672. }
  7673. switch (ie_id) {
  7674. /* Connected line party frame version */
  7675. case AST_CONNECTED_LINE_VERSION:
  7676. if (ie_len != 1) {
  7677. ast_log(LOG_WARNING, "Invalid connected line frame version (%u)\n",
  7678. (unsigned) ie_len);
  7679. break;
  7680. }
  7681. frame_version = data[pos];
  7682. break;
  7683. /* Connected line party id name */
  7684. case AST_CONNECTED_LINE_NAME:
  7685. ast_free(connected->id.name.str);
  7686. connected->id.name.str = ast_malloc(ie_len + 1);
  7687. if (connected->id.name.str) {
  7688. memcpy(connected->id.name.str, data + pos, ie_len);
  7689. connected->id.name.str[ie_len] = 0;
  7690. }
  7691. break;
  7692. case AST_CONNECTED_LINE_NAME_CHAR_SET:
  7693. if (ie_len != 1) {
  7694. ast_log(LOG_WARNING, "Invalid connected line name char set (%u)\n",
  7695. (unsigned) ie_len);
  7696. break;
  7697. }
  7698. connected->id.name.char_set = data[pos];
  7699. break;
  7700. case AST_CONNECTED_LINE_NAME_PRESENTATION:
  7701. if (ie_len != 1) {
  7702. ast_log(LOG_WARNING, "Invalid connected line name presentation (%u)\n",
  7703. (unsigned) ie_len);
  7704. break;
  7705. }
  7706. connected->id.name.presentation = data[pos];
  7707. break;
  7708. case AST_CONNECTED_LINE_NAME_VALID:
  7709. if (ie_len != 1) {
  7710. ast_log(LOG_WARNING, "Invalid connected line name valid (%u)\n",
  7711. (unsigned) ie_len);
  7712. break;
  7713. }
  7714. connected->id.name.valid = data[pos];
  7715. break;
  7716. /* Connected line party id number */
  7717. case AST_CONNECTED_LINE_NUMBER:
  7718. ast_free(connected->id.number.str);
  7719. connected->id.number.str = ast_malloc(ie_len + 1);
  7720. if (connected->id.number.str) {
  7721. memcpy(connected->id.number.str, data + pos, ie_len);
  7722. connected->id.number.str[ie_len] = 0;
  7723. }
  7724. break;
  7725. case AST_CONNECTED_LINE_NUMBER_PLAN:
  7726. if (ie_len != 1) {
  7727. ast_log(LOG_WARNING, "Invalid connected line numbering plan (%u)\n",
  7728. (unsigned) ie_len);
  7729. break;
  7730. }
  7731. connected->id.number.plan = data[pos];
  7732. break;
  7733. case AST_CONNECTED_LINE_NUMBER_PRESENTATION:
  7734. if (ie_len != 1) {
  7735. ast_log(LOG_WARNING, "Invalid connected line number presentation (%u)\n",
  7736. (unsigned) ie_len);
  7737. break;
  7738. }
  7739. connected->id.number.presentation = data[pos];
  7740. break;
  7741. case AST_CONNECTED_LINE_NUMBER_VALID:
  7742. if (ie_len != 1) {
  7743. ast_log(LOG_WARNING, "Invalid connected line number valid (%u)\n",
  7744. (unsigned) ie_len);
  7745. break;
  7746. }
  7747. connected->id.number.valid = data[pos];
  7748. break;
  7749. /* Connected line party id subaddress */
  7750. case AST_CONNECTED_LINE_SUBADDRESS:
  7751. ast_free(connected->id.subaddress.str);
  7752. connected->id.subaddress.str = ast_malloc(ie_len + 1);
  7753. if (connected->id.subaddress.str) {
  7754. memcpy(connected->id.subaddress.str, data + pos, ie_len);
  7755. connected->id.subaddress.str[ie_len] = 0;
  7756. }
  7757. break;
  7758. case AST_CONNECTED_LINE_SUBADDRESS_TYPE:
  7759. if (ie_len != 1) {
  7760. ast_log(LOG_WARNING, "Invalid connected line type of subaddress (%u)\n",
  7761. (unsigned) ie_len);
  7762. break;
  7763. }
  7764. connected->id.subaddress.type = data[pos];
  7765. break;
  7766. case AST_CONNECTED_LINE_SUBADDRESS_ODD_EVEN:
  7767. if (ie_len != 1) {
  7768. ast_log(LOG_WARNING,
  7769. "Invalid connected line subaddress odd-even indicator (%u)\n",
  7770. (unsigned) ie_len);
  7771. break;
  7772. }
  7773. connected->id.subaddress.odd_even_indicator = data[pos];
  7774. break;
  7775. case AST_CONNECTED_LINE_SUBADDRESS_VALID:
  7776. if (ie_len != 1) {
  7777. ast_log(LOG_WARNING, "Invalid connected line subaddress valid (%u)\n",
  7778. (unsigned) ie_len);
  7779. break;
  7780. }
  7781. connected->id.subaddress.valid = data[pos];
  7782. break;
  7783. /* Connected line party tag */
  7784. case AST_CONNECTED_LINE_TAG:
  7785. ast_free(connected->id.tag);
  7786. connected->id.tag = ast_malloc(ie_len + 1);
  7787. if (connected->id.tag) {
  7788. memcpy(connected->id.tag, data + pos, ie_len);
  7789. connected->id.tag[ie_len] = 0;
  7790. }
  7791. break;
  7792. /* Connected line party id combined presentation */
  7793. case AST_CONNECTED_LINE_ID_PRESENTATION:
  7794. if (ie_len != 1) {
  7795. ast_log(LOG_WARNING, "Invalid connected line combined presentation (%u)\n",
  7796. (unsigned) ie_len);
  7797. break;
  7798. }
  7799. combined_presentation = data[pos];
  7800. got_combined_presentation = 1;
  7801. break;
  7802. /* Private connected line party id name */
  7803. case AST_CONNECTED_LINE_PRIV_NAME:
  7804. ast_free(connected->priv.name.str);
  7805. connected->priv.name.str = ast_malloc(ie_len + 1);
  7806. if (connected->priv.name.str) {
  7807. memcpy(connected->priv.name.str, data + pos, ie_len);
  7808. connected->priv.name.str[ie_len] = 0;
  7809. }
  7810. break;
  7811. case AST_CONNECTED_LINE_PRIV_NAME_CHAR_SET:
  7812. if (ie_len != 1) {
  7813. ast_log(LOG_WARNING, "Invalid connected line private name char set (%u)\n",
  7814. (unsigned) ie_len);
  7815. break;
  7816. }
  7817. connected->priv.name.char_set = data[pos];
  7818. break;
  7819. case AST_CONNECTED_LINE_PRIV_NAME_PRESENTATION:
  7820. if (ie_len != 1) {
  7821. ast_log(LOG_WARNING, "Invalid connected line private name presentation (%u)\n",
  7822. (unsigned) ie_len);
  7823. break;
  7824. }
  7825. connected->priv.name.presentation = data[pos];
  7826. break;
  7827. case AST_CONNECTED_LINE_PRIV_NAME_VALID:
  7828. if (ie_len != 1) {
  7829. ast_log(LOG_WARNING, "Invalid connected line private name valid (%u)\n",
  7830. (unsigned) ie_len);
  7831. break;
  7832. }
  7833. connected->priv.name.valid = data[pos];
  7834. break;
  7835. /* Private connected line party id number */
  7836. case AST_CONNECTED_LINE_PRIV_NUMBER:
  7837. ast_free(connected->priv.number.str);
  7838. connected->priv.number.str = ast_malloc(ie_len + 1);
  7839. if (connected->priv.number.str) {
  7840. memcpy(connected->priv.number.str, data + pos, ie_len);
  7841. connected->priv.number.str[ie_len] = 0;
  7842. }
  7843. break;
  7844. case AST_CONNECTED_LINE_PRIV_NUMBER_PLAN:
  7845. if (ie_len != 1) {
  7846. ast_log(LOG_WARNING, "Invalid connected line private numbering plan (%u)\n",
  7847. (unsigned) ie_len);
  7848. break;
  7849. }
  7850. connected->priv.number.plan = data[pos];
  7851. break;
  7852. case AST_CONNECTED_LINE_PRIV_NUMBER_PRESENTATION:
  7853. if (ie_len != 1) {
  7854. ast_log(LOG_WARNING, "Invalid connected line private number presentation (%u)\n",
  7855. (unsigned) ie_len);
  7856. break;
  7857. }
  7858. connected->priv.number.presentation = data[pos];
  7859. break;
  7860. case AST_CONNECTED_LINE_PRIV_NUMBER_VALID:
  7861. if (ie_len != 1) {
  7862. ast_log(LOG_WARNING, "Invalid connected line private number valid (%u)\n",
  7863. (unsigned) ie_len);
  7864. break;
  7865. }
  7866. connected->priv.number.valid = data[pos];
  7867. break;
  7868. /* Private connected line party id subaddress */
  7869. case AST_CONNECTED_LINE_PRIV_SUBADDRESS:
  7870. ast_free(connected->priv.subaddress.str);
  7871. connected->priv.subaddress.str = ast_malloc(ie_len + 1);
  7872. if (connected->priv.subaddress.str) {
  7873. memcpy(connected->priv.subaddress.str, data + pos, ie_len);
  7874. connected->priv.subaddress.str[ie_len] = 0;
  7875. }
  7876. break;
  7877. case AST_CONNECTED_LINE_PRIV_SUBADDRESS_TYPE:
  7878. if (ie_len != 1) {
  7879. ast_log(LOG_WARNING, "Invalid connected line private type of subaddress (%u)\n",
  7880. (unsigned) ie_len);
  7881. break;
  7882. }
  7883. connected->priv.subaddress.type = data[pos];
  7884. break;
  7885. case AST_CONNECTED_LINE_PRIV_SUBADDRESS_ODD_EVEN:
  7886. if (ie_len != 1) {
  7887. ast_log(LOG_WARNING,
  7888. "Invalid connected line private subaddress odd-even indicator (%u)\n",
  7889. (unsigned) ie_len);
  7890. break;
  7891. }
  7892. connected->priv.subaddress.odd_even_indicator = data[pos];
  7893. break;
  7894. case AST_CONNECTED_LINE_PRIV_SUBADDRESS_VALID:
  7895. if (ie_len != 1) {
  7896. ast_log(LOG_WARNING, "Invalid connected line private subaddress valid (%u)\n",
  7897. (unsigned) ie_len);
  7898. break;
  7899. }
  7900. connected->priv.subaddress.valid = data[pos];
  7901. break;
  7902. /* Private connected line party tag */
  7903. case AST_CONNECTED_LINE_PRIV_TAG:
  7904. ast_free(connected->priv.tag);
  7905. connected->priv.tag = ast_malloc(ie_len + 1);
  7906. if (connected->priv.tag) {
  7907. memcpy(connected->priv.tag, data + pos, ie_len);
  7908. connected->priv.tag[ie_len] = 0;
  7909. }
  7910. break;
  7911. /* Connected line party source */
  7912. case AST_CONNECTED_LINE_SOURCE:
  7913. if (ie_len != sizeof(value)) {
  7914. ast_log(LOG_WARNING, "Invalid connected line source (%u)\n",
  7915. (unsigned) ie_len);
  7916. break;
  7917. }
  7918. memcpy(&value, data + pos, sizeof(value));
  7919. connected->source = ntohl(value);
  7920. break;
  7921. /* Connected line party unknown element */
  7922. default:
  7923. ast_debug(1, "Unknown connected line element: %u (%u)\n",
  7924. (unsigned) ie_id, (unsigned) ie_len);
  7925. break;
  7926. }
  7927. }
  7928. switch (frame_version) {
  7929. case 1:
  7930. /*
  7931. * The other end is an earlier version that we need to adjust
  7932. * for compatibility.
  7933. */
  7934. connected->id.name.valid = 1;
  7935. connected->id.name.char_set = AST_PARTY_CHAR_SET_ISO8859_1;
  7936. connected->id.number.valid = 1;
  7937. if (got_combined_presentation) {
  7938. connected->id.name.presentation = combined_presentation;
  7939. connected->id.number.presentation = combined_presentation;
  7940. }
  7941. break;
  7942. case 2:
  7943. /* The other end is at the same level as we are. */
  7944. break;
  7945. default:
  7946. /*
  7947. * The other end is newer than we are.
  7948. * We need to assume that they are compatible with us.
  7949. */
  7950. ast_debug(1, "Connected line frame has newer version: %u\n",
  7951. (unsigned) frame_version);
  7952. break;
  7953. }
  7954. return 0;
  7955. }
  7956. void ast_channel_update_connected_line(struct ast_channel *chan, const struct ast_party_connected_line *connected, const struct ast_set_party_connected_line *update)
  7957. {
  7958. unsigned char data[1024]; /* This should be large enough */
  7959. size_t datalen;
  7960. datalen = ast_connected_line_build_data(data, sizeof(data), connected, update);
  7961. if (datalen == (size_t) -1) {
  7962. return;
  7963. }
  7964. ast_indicate_data(chan, AST_CONTROL_CONNECTED_LINE, data, datalen);
  7965. }
  7966. void ast_channel_queue_connected_line_update(struct ast_channel *chan, const struct ast_party_connected_line *connected, const struct ast_set_party_connected_line *update)
  7967. {
  7968. unsigned char data[1024]; /* This should be large enough */
  7969. size_t datalen;
  7970. datalen = ast_connected_line_build_data(data, sizeof(data), connected, update);
  7971. if (datalen == (size_t) -1) {
  7972. return;
  7973. }
  7974. ast_queue_control_data(chan, AST_CONTROL_CONNECTED_LINE, data, datalen);
  7975. }
  7976. void ast_channel_set_redirecting(struct ast_channel *chan, const struct ast_party_redirecting *redirecting, const struct ast_set_party_redirecting *update)
  7977. {
  7978. if (ast_channel_redirecting(chan) == redirecting) {
  7979. /* Don't set to self */
  7980. return;
  7981. }
  7982. ast_channel_lock(chan);
  7983. ast_party_redirecting_set(ast_channel_redirecting(chan), redirecting, update);
  7984. ast_channel_unlock(chan);
  7985. }
  7986. /*!
  7987. * \brief Element identifiers for redirecting indication frame data
  7988. * \note Only add to the end of this enum.
  7989. */
  7990. enum {
  7991. AST_REDIRECTING_FROM_NUMBER,
  7992. AST_REDIRECTING_FROM_NAME,
  7993. AST_REDIRECTING_FROM_NUMBER_PLAN,
  7994. AST_REDIRECTING_FROM_ID_PRESENTATION,/* Combined number and name presentation. */
  7995. AST_REDIRECTING_TO_NUMBER,
  7996. AST_REDIRECTING_TO_NAME,
  7997. AST_REDIRECTING_TO_NUMBER_PLAN,
  7998. AST_REDIRECTING_TO_ID_PRESENTATION,/* Combined number and name presentation. */
  7999. AST_REDIRECTING_REASON_CODE,
  8000. AST_REDIRECTING_COUNT,
  8001. AST_REDIRECTING_FROM_SUBADDRESS,
  8002. AST_REDIRECTING_FROM_SUBADDRESS_TYPE,
  8003. AST_REDIRECTING_FROM_SUBADDRESS_ODD_EVEN,
  8004. AST_REDIRECTING_FROM_SUBADDRESS_VALID,
  8005. AST_REDIRECTING_TO_SUBADDRESS,
  8006. AST_REDIRECTING_TO_SUBADDRESS_TYPE,
  8007. AST_REDIRECTING_TO_SUBADDRESS_ODD_EVEN,
  8008. AST_REDIRECTING_TO_SUBADDRESS_VALID,
  8009. AST_REDIRECTING_FROM_TAG,
  8010. AST_REDIRECTING_TO_TAG,
  8011. AST_REDIRECTING_VERSION,
  8012. /*
  8013. * No more party id combined number and name presentation values
  8014. * need to be created.
  8015. */
  8016. AST_REDIRECTING_FROM_NAME_VALID,
  8017. AST_REDIRECTING_FROM_NAME_CHAR_SET,
  8018. AST_REDIRECTING_FROM_NAME_PRESENTATION,
  8019. AST_REDIRECTING_FROM_NUMBER_VALID,
  8020. AST_REDIRECTING_FROM_NUMBER_PRESENTATION,
  8021. AST_REDIRECTING_TO_NAME_VALID,
  8022. AST_REDIRECTING_TO_NAME_CHAR_SET,
  8023. AST_REDIRECTING_TO_NAME_PRESENTATION,
  8024. AST_REDIRECTING_TO_NUMBER_VALID,
  8025. AST_REDIRECTING_TO_NUMBER_PRESENTATION,
  8026. AST_REDIRECTING_ORIG_NUMBER,
  8027. AST_REDIRECTING_ORIG_NUMBER_VALID,
  8028. AST_REDIRECTING_ORIG_NUMBER_PLAN,
  8029. AST_REDIRECTING_ORIG_NUMBER_PRESENTATION,
  8030. AST_REDIRECTING_ORIG_NAME,
  8031. AST_REDIRECTING_ORIG_NAME_VALID,
  8032. AST_REDIRECTING_ORIG_NAME_CHAR_SET,
  8033. AST_REDIRECTING_ORIG_NAME_PRESENTATION,
  8034. AST_REDIRECTING_ORIG_SUBADDRESS,
  8035. AST_REDIRECTING_ORIG_SUBADDRESS_TYPE,
  8036. AST_REDIRECTING_ORIG_SUBADDRESS_ODD_EVEN,
  8037. AST_REDIRECTING_ORIG_SUBADDRESS_VALID,
  8038. AST_REDIRECTING_ORIG_TAG,
  8039. AST_REDIRECTING_ORIG_REASON_CODE,
  8040. AST_REDIRECTING_PRIV_TO_NUMBER,
  8041. AST_REDIRECTING_PRIV_TO_NUMBER_PLAN,
  8042. AST_REDIRECTING_PRIV_TO_NUMBER_VALID,
  8043. AST_REDIRECTING_PRIV_TO_NUMBER_PRESENTATION,
  8044. AST_REDIRECTING_PRIV_TO_NAME,
  8045. AST_REDIRECTING_PRIV_TO_NAME_VALID,
  8046. AST_REDIRECTING_PRIV_TO_NAME_CHAR_SET,
  8047. AST_REDIRECTING_PRIV_TO_NAME_PRESENTATION,
  8048. AST_REDIRECTING_PRIV_TO_SUBADDRESS,
  8049. AST_REDIRECTING_PRIV_TO_SUBADDRESS_TYPE,
  8050. AST_REDIRECTING_PRIV_TO_SUBADDRESS_ODD_EVEN,
  8051. AST_REDIRECTING_PRIV_TO_SUBADDRESS_VALID,
  8052. AST_REDIRECTING_PRIV_TO_TAG,
  8053. AST_REDIRECTING_PRIV_FROM_NUMBER,
  8054. AST_REDIRECTING_PRIV_FROM_NUMBER_PLAN,
  8055. AST_REDIRECTING_PRIV_FROM_NUMBER_VALID,
  8056. AST_REDIRECTING_PRIV_FROM_NUMBER_PRESENTATION,
  8057. AST_REDIRECTING_PRIV_FROM_NAME,
  8058. AST_REDIRECTING_PRIV_FROM_NAME_VALID,
  8059. AST_REDIRECTING_PRIV_FROM_NAME_CHAR_SET,
  8060. AST_REDIRECTING_PRIV_FROM_NAME_PRESENTATION,
  8061. AST_REDIRECTING_PRIV_FROM_SUBADDRESS,
  8062. AST_REDIRECTING_PRIV_FROM_SUBADDRESS_TYPE,
  8063. AST_REDIRECTING_PRIV_FROM_SUBADDRESS_ODD_EVEN,
  8064. AST_REDIRECTING_PRIV_FROM_SUBADDRESS_VALID,
  8065. AST_REDIRECTING_PRIV_FROM_TAG,
  8066. AST_REDIRECTING_PRIV_ORIG_NUMBER,
  8067. AST_REDIRECTING_PRIV_ORIG_NUMBER_VALID,
  8068. AST_REDIRECTING_PRIV_ORIG_NUMBER_PLAN,
  8069. AST_REDIRECTING_PRIV_ORIG_NUMBER_PRESENTATION,
  8070. AST_REDIRECTING_PRIV_ORIG_NAME,
  8071. AST_REDIRECTING_PRIV_ORIG_NAME_VALID,
  8072. AST_REDIRECTING_PRIV_ORIG_NAME_CHAR_SET,
  8073. AST_REDIRECTING_PRIV_ORIG_NAME_PRESENTATION,
  8074. AST_REDIRECTING_PRIV_ORIG_SUBADDRESS,
  8075. AST_REDIRECTING_PRIV_ORIG_SUBADDRESS_TYPE,
  8076. AST_REDIRECTING_PRIV_ORIG_SUBADDRESS_ODD_EVEN,
  8077. AST_REDIRECTING_PRIV_ORIG_SUBADDRESS_VALID,
  8078. AST_REDIRECTING_PRIV_ORIG_TAG,
  8079. AST_REDIRECTING_REASON_STR,
  8080. AST_REDIRECTING_ORIG_REASON_STR,
  8081. };
  8082. struct ast_party_redirecting_reason_ies {
  8083. int code;
  8084. int str;
  8085. };
  8086. static int redirecting_reason_build_data(unsigned char *data, size_t datalen,
  8087. const struct ast_party_redirecting_reason *reason, const char *label,
  8088. const struct ast_party_redirecting_reason_ies *ies)
  8089. {
  8090. size_t length;
  8091. size_t pos = 0;
  8092. int32_t value;
  8093. if (datalen < pos + (sizeof(data[0]) * 2) + sizeof(value)) {
  8094. ast_log(LOG_WARNING, "No space left for %s code\n", label);
  8095. return -1;
  8096. }
  8097. data[pos++] = ies->code;
  8098. data[pos++] = sizeof(value);
  8099. value = htonl(reason->code);
  8100. memcpy(data + pos, &value, sizeof(value));
  8101. pos += sizeof(value);
  8102. if (reason->str) {
  8103. length = strlen(reason->str);
  8104. if (datalen < pos + (sizeof(data[0]) * 2) + length) {
  8105. ast_log(LOG_WARNING, "No space left for %s string\n", label);
  8106. return -1;
  8107. }
  8108. data[pos++] = ies->str;
  8109. data[pos++] = length;
  8110. memcpy(data + pos, reason->str, length);
  8111. pos += length;
  8112. }
  8113. return pos;
  8114. }
  8115. int ast_redirecting_build_data(unsigned char *data, size_t datalen, const struct ast_party_redirecting *redirecting, const struct ast_set_party_redirecting *update)
  8116. {
  8117. int32_t value;
  8118. size_t pos = 0;
  8119. int res;
  8120. static const struct ast_party_id_ies orig_ies = {
  8121. .name.str = AST_REDIRECTING_ORIG_NAME,
  8122. .name.char_set = AST_REDIRECTING_ORIG_NAME_CHAR_SET,
  8123. .name.presentation = AST_REDIRECTING_ORIG_NAME_PRESENTATION,
  8124. .name.valid = AST_REDIRECTING_ORIG_NAME_VALID,
  8125. .number.str = AST_REDIRECTING_ORIG_NUMBER,
  8126. .number.plan = AST_REDIRECTING_ORIG_NUMBER_PLAN,
  8127. .number.presentation = AST_REDIRECTING_ORIG_NUMBER_PRESENTATION,
  8128. .number.valid = AST_REDIRECTING_ORIG_NUMBER_VALID,
  8129. .subaddress.str = AST_REDIRECTING_ORIG_SUBADDRESS,
  8130. .subaddress.type = AST_REDIRECTING_ORIG_SUBADDRESS_TYPE,
  8131. .subaddress.odd_even_indicator = AST_REDIRECTING_ORIG_SUBADDRESS_ODD_EVEN,
  8132. .subaddress.valid = AST_REDIRECTING_ORIG_SUBADDRESS_VALID,
  8133. .tag = AST_REDIRECTING_ORIG_TAG,
  8134. .combined_presentation = 0,/* Not sent. */
  8135. };
  8136. static const struct ast_party_id_ies from_ies = {
  8137. .name.str = AST_REDIRECTING_FROM_NAME,
  8138. .name.char_set = AST_REDIRECTING_FROM_NAME_CHAR_SET,
  8139. .name.presentation = AST_REDIRECTING_FROM_NAME_PRESENTATION,
  8140. .name.valid = AST_REDIRECTING_FROM_NAME_VALID,
  8141. .number.str = AST_REDIRECTING_FROM_NUMBER,
  8142. .number.plan = AST_REDIRECTING_FROM_NUMBER_PLAN,
  8143. .number.presentation = AST_REDIRECTING_FROM_NUMBER_PRESENTATION,
  8144. .number.valid = AST_REDIRECTING_FROM_NUMBER_VALID,
  8145. .subaddress.str = AST_REDIRECTING_FROM_SUBADDRESS,
  8146. .subaddress.type = AST_REDIRECTING_FROM_SUBADDRESS_TYPE,
  8147. .subaddress.odd_even_indicator = AST_REDIRECTING_FROM_SUBADDRESS_ODD_EVEN,
  8148. .subaddress.valid = AST_REDIRECTING_FROM_SUBADDRESS_VALID,
  8149. .tag = AST_REDIRECTING_FROM_TAG,
  8150. .combined_presentation = AST_REDIRECTING_FROM_ID_PRESENTATION,
  8151. };
  8152. static const struct ast_party_id_ies to_ies = {
  8153. .name.str = AST_REDIRECTING_TO_NAME,
  8154. .name.char_set = AST_REDIRECTING_TO_NAME_CHAR_SET,
  8155. .name.presentation = AST_REDIRECTING_TO_NAME_PRESENTATION,
  8156. .name.valid = AST_REDIRECTING_TO_NAME_VALID,
  8157. .number.str = AST_REDIRECTING_TO_NUMBER,
  8158. .number.plan = AST_REDIRECTING_TO_NUMBER_PLAN,
  8159. .number.presentation = AST_REDIRECTING_TO_NUMBER_PRESENTATION,
  8160. .number.valid = AST_REDIRECTING_TO_NUMBER_VALID,
  8161. .subaddress.str = AST_REDIRECTING_TO_SUBADDRESS,
  8162. .subaddress.type = AST_REDIRECTING_TO_SUBADDRESS_TYPE,
  8163. .subaddress.odd_even_indicator = AST_REDIRECTING_TO_SUBADDRESS_ODD_EVEN,
  8164. .subaddress.valid = AST_REDIRECTING_TO_SUBADDRESS_VALID,
  8165. .tag = AST_REDIRECTING_TO_TAG,
  8166. .combined_presentation = AST_REDIRECTING_TO_ID_PRESENTATION,
  8167. };
  8168. static const struct ast_party_id_ies priv_orig_ies = {
  8169. .name.str = AST_REDIRECTING_PRIV_ORIG_NAME,
  8170. .name.char_set = AST_REDIRECTING_PRIV_ORIG_NAME_CHAR_SET,
  8171. .name.presentation = AST_REDIRECTING_PRIV_ORIG_NAME_PRESENTATION,
  8172. .name.valid = AST_REDIRECTING_PRIV_ORIG_NAME_VALID,
  8173. .number.str = AST_REDIRECTING_PRIV_ORIG_NUMBER,
  8174. .number.plan = AST_REDIRECTING_PRIV_ORIG_NUMBER_PLAN,
  8175. .number.presentation = AST_REDIRECTING_PRIV_ORIG_NUMBER_PRESENTATION,
  8176. .number.valid = AST_REDIRECTING_PRIV_ORIG_NUMBER_VALID,
  8177. .subaddress.str = AST_REDIRECTING_PRIV_ORIG_SUBADDRESS,
  8178. .subaddress.type = AST_REDIRECTING_PRIV_ORIG_SUBADDRESS_TYPE,
  8179. .subaddress.odd_even_indicator = AST_REDIRECTING_PRIV_ORIG_SUBADDRESS_ODD_EVEN,
  8180. .subaddress.valid = AST_REDIRECTING_PRIV_ORIG_SUBADDRESS_VALID,
  8181. .tag = AST_REDIRECTING_PRIV_ORIG_TAG,
  8182. .combined_presentation = 0,/* Not sent. */
  8183. };
  8184. static const struct ast_party_id_ies priv_from_ies = {
  8185. .name.str = AST_REDIRECTING_PRIV_FROM_NAME,
  8186. .name.char_set = AST_REDIRECTING_PRIV_FROM_NAME_CHAR_SET,
  8187. .name.presentation = AST_REDIRECTING_PRIV_FROM_NAME_PRESENTATION,
  8188. .name.valid = AST_REDIRECTING_PRIV_FROM_NAME_VALID,
  8189. .number.str = AST_REDIRECTING_PRIV_FROM_NUMBER,
  8190. .number.plan = AST_REDIRECTING_PRIV_FROM_NUMBER_PLAN,
  8191. .number.presentation = AST_REDIRECTING_PRIV_FROM_NUMBER_PRESENTATION,
  8192. .number.valid = AST_REDIRECTING_PRIV_FROM_NUMBER_VALID,
  8193. .subaddress.str = AST_REDIRECTING_PRIV_FROM_SUBADDRESS,
  8194. .subaddress.type = AST_REDIRECTING_PRIV_FROM_SUBADDRESS_TYPE,
  8195. .subaddress.odd_even_indicator = AST_REDIRECTING_PRIV_FROM_SUBADDRESS_ODD_EVEN,
  8196. .subaddress.valid = AST_REDIRECTING_PRIV_FROM_SUBADDRESS_VALID,
  8197. .tag = AST_REDIRECTING_PRIV_FROM_TAG,
  8198. .combined_presentation = 0,/* Not sent. */
  8199. };
  8200. static const struct ast_party_id_ies priv_to_ies = {
  8201. .name.str = AST_REDIRECTING_PRIV_TO_NAME,
  8202. .name.char_set = AST_REDIRECTING_PRIV_TO_NAME_CHAR_SET,
  8203. .name.presentation = AST_REDIRECTING_PRIV_TO_NAME_PRESENTATION,
  8204. .name.valid = AST_REDIRECTING_PRIV_TO_NAME_VALID,
  8205. .number.str = AST_REDIRECTING_PRIV_TO_NUMBER,
  8206. .number.plan = AST_REDIRECTING_PRIV_TO_NUMBER_PLAN,
  8207. .number.presentation = AST_REDIRECTING_PRIV_TO_NUMBER_PRESENTATION,
  8208. .number.valid = AST_REDIRECTING_PRIV_TO_NUMBER_VALID,
  8209. .subaddress.str = AST_REDIRECTING_PRIV_TO_SUBADDRESS,
  8210. .subaddress.type = AST_REDIRECTING_PRIV_TO_SUBADDRESS_TYPE,
  8211. .subaddress.odd_even_indicator = AST_REDIRECTING_PRIV_TO_SUBADDRESS_ODD_EVEN,
  8212. .subaddress.valid = AST_REDIRECTING_PRIV_TO_SUBADDRESS_VALID,
  8213. .tag = AST_REDIRECTING_PRIV_TO_TAG,
  8214. .combined_presentation = 0,/* Not sent. */
  8215. };
  8216. static const struct ast_party_redirecting_reason_ies reason_ies = {
  8217. .code = AST_REDIRECTING_REASON_CODE,
  8218. .str = AST_REDIRECTING_REASON_STR,
  8219. };
  8220. static const struct ast_party_redirecting_reason_ies orig_reason_ies = {
  8221. .code = AST_REDIRECTING_ORIG_REASON_CODE,
  8222. .str = AST_REDIRECTING_ORIG_REASON_STR,
  8223. };
  8224. /* Redirecting frame version */
  8225. if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
  8226. ast_log(LOG_WARNING, "No space left for redirecting frame version\n");
  8227. return -1;
  8228. }
  8229. data[pos++] = AST_REDIRECTING_VERSION;
  8230. data[pos++] = 1;
  8231. data[pos++] = 2;/* Version 1 did not have a version ie */
  8232. res = party_id_build_data(data + pos, datalen - pos, &redirecting->orig,
  8233. "redirecting-orig", &orig_ies, update ? &update->orig : NULL);
  8234. if (res < 0) {
  8235. return -1;
  8236. }
  8237. pos += res;
  8238. res = party_id_build_data(data + pos, datalen - pos, &redirecting->from,
  8239. "redirecting-from", &from_ies, update ? &update->from : NULL);
  8240. if (res < 0) {
  8241. return -1;
  8242. }
  8243. pos += res;
  8244. res = party_id_build_data(data + pos, datalen - pos, &redirecting->to,
  8245. "redirecting-to", &to_ies, update ? &update->to : NULL);
  8246. if (res < 0) {
  8247. return -1;
  8248. }
  8249. pos += res;
  8250. res = party_id_build_data(data + pos, datalen - pos, &redirecting->priv_orig,
  8251. "redirecting-priv-orig", &priv_orig_ies, update ? &update->priv_orig : NULL);
  8252. if (res < 0) {
  8253. return -1;
  8254. }
  8255. pos += res;
  8256. res = party_id_build_data(data + pos, datalen - pos, &redirecting->priv_from,
  8257. "redirecting-priv-from", &priv_from_ies, update ? &update->priv_from : NULL);
  8258. if (res < 0) {
  8259. return -1;
  8260. }
  8261. pos += res;
  8262. res = party_id_build_data(data + pos, datalen - pos, &redirecting->priv_to,
  8263. "redirecting-priv-to", &priv_to_ies, update ? &update->priv_to : NULL);
  8264. if (res < 0) {
  8265. return -1;
  8266. }
  8267. pos += res;
  8268. /* Redirecting reason */
  8269. res = redirecting_reason_build_data(data + pos, datalen - pos, &redirecting->reason,
  8270. "redirecting-reason", &reason_ies);
  8271. if (res < 0) {
  8272. return -1;
  8273. }
  8274. pos += res;
  8275. /* Redirecting original reason */
  8276. res = redirecting_reason_build_data(data + pos, datalen - pos, &redirecting->orig_reason,
  8277. "redirecting-orig-reason", &orig_reason_ies);
  8278. if (res < 0) {
  8279. return -1;
  8280. }
  8281. pos += res;
  8282. /* Redirecting count */
  8283. if (datalen < pos + (sizeof(data[0]) * 2) + sizeof(value)) {
  8284. ast_log(LOG_WARNING, "No space left for redirecting count\n");
  8285. return -1;
  8286. }
  8287. data[pos++] = AST_REDIRECTING_COUNT;
  8288. data[pos++] = sizeof(value);
  8289. value = htonl(redirecting->count);
  8290. memcpy(data + pos, &value, sizeof(value));
  8291. pos += sizeof(value);
  8292. return pos;
  8293. }
  8294. int ast_redirecting_parse_data(const unsigned char *data, size_t datalen, struct ast_party_redirecting *redirecting)
  8295. {
  8296. size_t pos;
  8297. unsigned char ie_len;
  8298. unsigned char ie_id;
  8299. int32_t value;
  8300. int frame_version = 1;
  8301. int from_combined_presentation = 0;
  8302. int got_from_combined_presentation = 0;/* TRUE if got a combined name and number presentation value. */
  8303. int to_combined_presentation = 0;
  8304. int got_to_combined_presentation = 0;/* TRUE if got a combined name and number presentation value. */
  8305. for (pos = 0; pos < datalen; pos += ie_len) {
  8306. if (datalen < pos + sizeof(ie_id) + sizeof(ie_len)) {
  8307. ast_log(LOG_WARNING, "Invalid redirecting update\n");
  8308. return -1;
  8309. }
  8310. ie_id = data[pos++];
  8311. ie_len = data[pos++];
  8312. if (datalen < pos + ie_len) {
  8313. ast_log(LOG_WARNING, "Invalid redirecting update\n");
  8314. return -1;
  8315. }
  8316. switch (ie_id) {
  8317. /* Redirecting frame version */
  8318. case AST_REDIRECTING_VERSION:
  8319. if (ie_len != 1) {
  8320. ast_log(LOG_WARNING, "Invalid redirecting frame version (%u)\n",
  8321. (unsigned) ie_len);
  8322. break;
  8323. }
  8324. frame_version = data[pos];
  8325. break;
  8326. /* Redirecting-orig party id name */
  8327. case AST_REDIRECTING_ORIG_NAME:
  8328. ast_free(redirecting->orig.name.str);
  8329. redirecting->orig.name.str = ast_malloc(ie_len + 1);
  8330. if (redirecting->orig.name.str) {
  8331. memcpy(redirecting->orig.name.str, data + pos, ie_len);
  8332. redirecting->orig.name.str[ie_len] = 0;
  8333. }
  8334. break;
  8335. case AST_REDIRECTING_ORIG_NAME_CHAR_SET:
  8336. if (ie_len != 1) {
  8337. ast_log(LOG_WARNING, "Invalid redirecting-orig name char set (%u)\n",
  8338. (unsigned) ie_len);
  8339. break;
  8340. }
  8341. redirecting->orig.name.char_set = data[pos];
  8342. break;
  8343. case AST_REDIRECTING_ORIG_NAME_PRESENTATION:
  8344. if (ie_len != 1) {
  8345. ast_log(LOG_WARNING, "Invalid redirecting-orig name presentation (%u)\n",
  8346. (unsigned) ie_len);
  8347. break;
  8348. }
  8349. redirecting->orig.name.presentation = data[pos];
  8350. break;
  8351. case AST_REDIRECTING_ORIG_NAME_VALID:
  8352. if (ie_len != 1) {
  8353. ast_log(LOG_WARNING, "Invalid redirecting-orig name valid (%u)\n",
  8354. (unsigned) ie_len);
  8355. break;
  8356. }
  8357. redirecting->orig.name.valid = data[pos];
  8358. break;
  8359. /* Redirecting-orig party id number */
  8360. case AST_REDIRECTING_ORIG_NUMBER:
  8361. ast_free(redirecting->orig.number.str);
  8362. redirecting->orig.number.str = ast_malloc(ie_len + 1);
  8363. if (redirecting->orig.number.str) {
  8364. memcpy(redirecting->orig.number.str, data + pos, ie_len);
  8365. redirecting->orig.number.str[ie_len] = 0;
  8366. }
  8367. break;
  8368. case AST_REDIRECTING_ORIG_NUMBER_PLAN:
  8369. if (ie_len != 1) {
  8370. ast_log(LOG_WARNING, "Invalid redirecting-orig numbering plan (%u)\n",
  8371. (unsigned) ie_len);
  8372. break;
  8373. }
  8374. redirecting->orig.number.plan = data[pos];
  8375. break;
  8376. case AST_REDIRECTING_ORIG_NUMBER_PRESENTATION:
  8377. if (ie_len != 1) {
  8378. ast_log(LOG_WARNING, "Invalid redirecting-orig number presentation (%u)\n",
  8379. (unsigned) ie_len);
  8380. break;
  8381. }
  8382. redirecting->orig.number.presentation = data[pos];
  8383. break;
  8384. case AST_REDIRECTING_ORIG_NUMBER_VALID:
  8385. if (ie_len != 1) {
  8386. ast_log(LOG_WARNING, "Invalid redirecting-orig number valid (%u)\n",
  8387. (unsigned) ie_len);
  8388. break;
  8389. }
  8390. redirecting->orig.number.valid = data[pos];
  8391. break;
  8392. /* Redirecting-orig party id subaddress */
  8393. case AST_REDIRECTING_ORIG_SUBADDRESS:
  8394. ast_free(redirecting->orig.subaddress.str);
  8395. redirecting->orig.subaddress.str = ast_malloc(ie_len + 1);
  8396. if (redirecting->orig.subaddress.str) {
  8397. memcpy(redirecting->orig.subaddress.str, data + pos, ie_len);
  8398. redirecting->orig.subaddress.str[ie_len] = 0;
  8399. }
  8400. break;
  8401. case AST_REDIRECTING_ORIG_SUBADDRESS_TYPE:
  8402. if (ie_len != 1) {
  8403. ast_log(LOG_WARNING, "Invalid redirecting-orig type of subaddress (%u)\n",
  8404. (unsigned) ie_len);
  8405. break;
  8406. }
  8407. redirecting->orig.subaddress.type = data[pos];
  8408. break;
  8409. case AST_REDIRECTING_ORIG_SUBADDRESS_ODD_EVEN:
  8410. if (ie_len != 1) {
  8411. ast_log(LOG_WARNING,
  8412. "Invalid redirecting-orig subaddress odd-even indicator (%u)\n",
  8413. (unsigned) ie_len);
  8414. break;
  8415. }
  8416. redirecting->orig.subaddress.odd_even_indicator = data[pos];
  8417. break;
  8418. case AST_REDIRECTING_ORIG_SUBADDRESS_VALID:
  8419. if (ie_len != 1) {
  8420. ast_log(LOG_WARNING, "Invalid redirecting-orig subaddress valid (%u)\n",
  8421. (unsigned) ie_len);
  8422. break;
  8423. }
  8424. redirecting->orig.subaddress.valid = data[pos];
  8425. break;
  8426. /* Redirecting-orig party id tag */
  8427. case AST_REDIRECTING_ORIG_TAG:
  8428. ast_free(redirecting->orig.tag);
  8429. redirecting->orig.tag = ast_malloc(ie_len + 1);
  8430. if (redirecting->orig.tag) {
  8431. memcpy(redirecting->orig.tag, data + pos, ie_len);
  8432. redirecting->orig.tag[ie_len] = 0;
  8433. }
  8434. break;
  8435. /* Redirecting-from party id name */
  8436. case AST_REDIRECTING_FROM_NAME:
  8437. ast_free(redirecting->from.name.str);
  8438. redirecting->from.name.str = ast_malloc(ie_len + 1);
  8439. if (redirecting->from.name.str) {
  8440. memcpy(redirecting->from.name.str, data + pos, ie_len);
  8441. redirecting->from.name.str[ie_len] = 0;
  8442. }
  8443. break;
  8444. case AST_REDIRECTING_FROM_NAME_CHAR_SET:
  8445. if (ie_len != 1) {
  8446. ast_log(LOG_WARNING, "Invalid redirecting-from name char set (%u)\n",
  8447. (unsigned) ie_len);
  8448. break;
  8449. }
  8450. redirecting->from.name.char_set = data[pos];
  8451. break;
  8452. case AST_REDIRECTING_FROM_NAME_PRESENTATION:
  8453. if (ie_len != 1) {
  8454. ast_log(LOG_WARNING, "Invalid redirecting-from name presentation (%u)\n",
  8455. (unsigned) ie_len);
  8456. break;
  8457. }
  8458. redirecting->from.name.presentation = data[pos];
  8459. break;
  8460. case AST_REDIRECTING_FROM_NAME_VALID:
  8461. if (ie_len != 1) {
  8462. ast_log(LOG_WARNING, "Invalid redirecting-from name valid (%u)\n",
  8463. (unsigned) ie_len);
  8464. break;
  8465. }
  8466. redirecting->from.name.valid = data[pos];
  8467. break;
  8468. /* Redirecting-from party id number */
  8469. case AST_REDIRECTING_FROM_NUMBER:
  8470. ast_free(redirecting->from.number.str);
  8471. redirecting->from.number.str = ast_malloc(ie_len + 1);
  8472. if (redirecting->from.number.str) {
  8473. memcpy(redirecting->from.number.str, data + pos, ie_len);
  8474. redirecting->from.number.str[ie_len] = 0;
  8475. }
  8476. break;
  8477. case AST_REDIRECTING_FROM_NUMBER_PLAN:
  8478. if (ie_len != 1) {
  8479. ast_log(LOG_WARNING, "Invalid redirecting-from numbering plan (%u)\n",
  8480. (unsigned) ie_len);
  8481. break;
  8482. }
  8483. redirecting->from.number.plan = data[pos];
  8484. break;
  8485. case AST_REDIRECTING_FROM_NUMBER_PRESENTATION:
  8486. if (ie_len != 1) {
  8487. ast_log(LOG_WARNING, "Invalid redirecting-from number presentation (%u)\n",
  8488. (unsigned) ie_len);
  8489. break;
  8490. }
  8491. redirecting->from.number.presentation = data[pos];
  8492. break;
  8493. case AST_REDIRECTING_FROM_NUMBER_VALID:
  8494. if (ie_len != 1) {
  8495. ast_log(LOG_WARNING, "Invalid redirecting-from number valid (%u)\n",
  8496. (unsigned) ie_len);
  8497. break;
  8498. }
  8499. redirecting->from.number.valid = data[pos];
  8500. break;
  8501. /* Redirecting-from party id combined presentation */
  8502. case AST_REDIRECTING_FROM_ID_PRESENTATION:
  8503. if (ie_len != 1) {
  8504. ast_log(LOG_WARNING, "Invalid redirecting-from combined presentation (%u)\n",
  8505. (unsigned) ie_len);
  8506. break;
  8507. }
  8508. from_combined_presentation = data[pos];
  8509. got_from_combined_presentation = 1;
  8510. break;
  8511. /* Redirecting-from party id subaddress */
  8512. case AST_REDIRECTING_FROM_SUBADDRESS:
  8513. ast_free(redirecting->from.subaddress.str);
  8514. redirecting->from.subaddress.str = ast_malloc(ie_len + 1);
  8515. if (redirecting->from.subaddress.str) {
  8516. memcpy(redirecting->from.subaddress.str, data + pos, ie_len);
  8517. redirecting->from.subaddress.str[ie_len] = 0;
  8518. }
  8519. break;
  8520. case AST_REDIRECTING_FROM_SUBADDRESS_TYPE:
  8521. if (ie_len != 1) {
  8522. ast_log(LOG_WARNING, "Invalid redirecting-from type of subaddress (%u)\n",
  8523. (unsigned) ie_len);
  8524. break;
  8525. }
  8526. redirecting->from.subaddress.type = data[pos];
  8527. break;
  8528. case AST_REDIRECTING_FROM_SUBADDRESS_ODD_EVEN:
  8529. if (ie_len != 1) {
  8530. ast_log(LOG_WARNING,
  8531. "Invalid redirecting-from subaddress odd-even indicator (%u)\n",
  8532. (unsigned) ie_len);
  8533. break;
  8534. }
  8535. redirecting->from.subaddress.odd_even_indicator = data[pos];
  8536. break;
  8537. case AST_REDIRECTING_FROM_SUBADDRESS_VALID:
  8538. if (ie_len != 1) {
  8539. ast_log(LOG_WARNING, "Invalid redirecting-from subaddress valid (%u)\n",
  8540. (unsigned) ie_len);
  8541. break;
  8542. }
  8543. redirecting->from.subaddress.valid = data[pos];
  8544. break;
  8545. /* Redirecting-from party id tag */
  8546. case AST_REDIRECTING_FROM_TAG:
  8547. ast_free(redirecting->from.tag);
  8548. redirecting->from.tag = ast_malloc(ie_len + 1);
  8549. if (redirecting->from.tag) {
  8550. memcpy(redirecting->from.tag, data + pos, ie_len);
  8551. redirecting->from.tag[ie_len] = 0;
  8552. }
  8553. break;
  8554. /* Redirecting-to party id name */
  8555. case AST_REDIRECTING_TO_NAME:
  8556. ast_free(redirecting->to.name.str);
  8557. redirecting->to.name.str = ast_malloc(ie_len + 1);
  8558. if (redirecting->to.name.str) {
  8559. memcpy(redirecting->to.name.str, data + pos, ie_len);
  8560. redirecting->to.name.str[ie_len] = 0;
  8561. }
  8562. break;
  8563. case AST_REDIRECTING_TO_NAME_CHAR_SET:
  8564. if (ie_len != 1) {
  8565. ast_log(LOG_WARNING, "Invalid redirecting-to name char set (%u)\n",
  8566. (unsigned) ie_len);
  8567. break;
  8568. }
  8569. redirecting->to.name.char_set = data[pos];
  8570. break;
  8571. case AST_REDIRECTING_TO_NAME_PRESENTATION:
  8572. if (ie_len != 1) {
  8573. ast_log(LOG_WARNING, "Invalid redirecting-to name presentation (%u)\n",
  8574. (unsigned) ie_len);
  8575. break;
  8576. }
  8577. redirecting->to.name.presentation = data[pos];
  8578. break;
  8579. case AST_REDIRECTING_TO_NAME_VALID:
  8580. if (ie_len != 1) {
  8581. ast_log(LOG_WARNING, "Invalid redirecting-to name valid (%u)\n",
  8582. (unsigned) ie_len);
  8583. break;
  8584. }
  8585. redirecting->to.name.valid = data[pos];
  8586. break;
  8587. /* Redirecting-to party id number */
  8588. case AST_REDIRECTING_TO_NUMBER:
  8589. ast_free(redirecting->to.number.str);
  8590. redirecting->to.number.str = ast_malloc(ie_len + 1);
  8591. if (redirecting->to.number.str) {
  8592. memcpy(redirecting->to.number.str, data + pos, ie_len);
  8593. redirecting->to.number.str[ie_len] = 0;
  8594. }
  8595. break;
  8596. case AST_REDIRECTING_TO_NUMBER_PLAN:
  8597. if (ie_len != 1) {
  8598. ast_log(LOG_WARNING, "Invalid redirecting-to numbering plan (%u)\n",
  8599. (unsigned) ie_len);
  8600. break;
  8601. }
  8602. redirecting->to.number.plan = data[pos];
  8603. break;
  8604. case AST_REDIRECTING_TO_NUMBER_PRESENTATION:
  8605. if (ie_len != 1) {
  8606. ast_log(LOG_WARNING, "Invalid redirecting-to number presentation (%u)\n",
  8607. (unsigned) ie_len);
  8608. break;
  8609. }
  8610. redirecting->to.number.presentation = data[pos];
  8611. break;
  8612. case AST_REDIRECTING_TO_NUMBER_VALID:
  8613. if (ie_len != 1) {
  8614. ast_log(LOG_WARNING, "Invalid redirecting-to number valid (%u)\n",
  8615. (unsigned) ie_len);
  8616. break;
  8617. }
  8618. redirecting->to.number.valid = data[pos];
  8619. break;
  8620. /* Redirecting-to party id combined presentation */
  8621. case AST_REDIRECTING_TO_ID_PRESENTATION:
  8622. if (ie_len != 1) {
  8623. ast_log(LOG_WARNING, "Invalid redirecting-to combined presentation (%u)\n",
  8624. (unsigned) ie_len);
  8625. break;
  8626. }
  8627. to_combined_presentation = data[pos];
  8628. got_to_combined_presentation = 1;
  8629. break;
  8630. /* Redirecting-to party id subaddress */
  8631. case AST_REDIRECTING_TO_SUBADDRESS:
  8632. ast_free(redirecting->to.subaddress.str);
  8633. redirecting->to.subaddress.str = ast_malloc(ie_len + 1);
  8634. if (redirecting->to.subaddress.str) {
  8635. memcpy(redirecting->to.subaddress.str, data + pos, ie_len);
  8636. redirecting->to.subaddress.str[ie_len] = 0;
  8637. }
  8638. break;
  8639. case AST_REDIRECTING_TO_SUBADDRESS_TYPE:
  8640. if (ie_len != 1) {
  8641. ast_log(LOG_WARNING, "Invalid redirecting-to type of subaddress (%u)\n",
  8642. (unsigned) ie_len);
  8643. break;
  8644. }
  8645. redirecting->to.subaddress.type = data[pos];
  8646. break;
  8647. case AST_REDIRECTING_TO_SUBADDRESS_ODD_EVEN:
  8648. if (ie_len != 1) {
  8649. ast_log(LOG_WARNING,
  8650. "Invalid redirecting-to subaddress odd-even indicator (%u)\n",
  8651. (unsigned) ie_len);
  8652. break;
  8653. }
  8654. redirecting->to.subaddress.odd_even_indicator = data[pos];
  8655. break;
  8656. case AST_REDIRECTING_TO_SUBADDRESS_VALID:
  8657. if (ie_len != 1) {
  8658. ast_log(LOG_WARNING, "Invalid redirecting-to subaddress valid (%u)\n",
  8659. (unsigned) ie_len);
  8660. break;
  8661. }
  8662. redirecting->to.subaddress.valid = data[pos];
  8663. break;
  8664. /* Redirecting-to party id tag */
  8665. case AST_REDIRECTING_TO_TAG:
  8666. ast_free(redirecting->to.tag);
  8667. redirecting->to.tag = ast_malloc(ie_len + 1);
  8668. if (redirecting->to.tag) {
  8669. memcpy(redirecting->to.tag, data + pos, ie_len);
  8670. redirecting->to.tag[ie_len] = 0;
  8671. }
  8672. break;
  8673. /* Private redirecting-orig party id name */
  8674. case AST_REDIRECTING_PRIV_ORIG_NAME:
  8675. ast_free(redirecting->priv_orig.name.str);
  8676. redirecting->priv_orig.name.str = ast_malloc(ie_len + 1);
  8677. if (redirecting->priv_orig.name.str) {
  8678. memcpy(redirecting->priv_orig.name.str, data + pos, ie_len);
  8679. redirecting->priv_orig.name.str[ie_len] = 0;
  8680. }
  8681. break;
  8682. case AST_REDIRECTING_PRIV_ORIG_NAME_CHAR_SET:
  8683. if (ie_len != 1) {
  8684. ast_log(LOG_WARNING, "Invalid private redirecting-orig name char set (%u)\n",
  8685. (unsigned) ie_len);
  8686. break;
  8687. }
  8688. redirecting->priv_orig.name.char_set = data[pos];
  8689. break;
  8690. case AST_REDIRECTING_PRIV_ORIG_NAME_PRESENTATION:
  8691. if (ie_len != 1) {
  8692. ast_log(LOG_WARNING, "Invalid private redirecting-orig name presentation (%u)\n",
  8693. (unsigned) ie_len);
  8694. break;
  8695. }
  8696. redirecting->priv_orig.name.presentation = data[pos];
  8697. break;
  8698. case AST_REDIRECTING_PRIV_ORIG_NAME_VALID:
  8699. if (ie_len != 1) {
  8700. ast_log(LOG_WARNING, "Invalid private redirecting-orig name valid (%u)\n",
  8701. (unsigned) ie_len);
  8702. break;
  8703. }
  8704. redirecting->priv_orig.name.valid = data[pos];
  8705. break;
  8706. /* Private redirecting-orig party id number */
  8707. case AST_REDIRECTING_PRIV_ORIG_NUMBER:
  8708. ast_free(redirecting->priv_orig.number.str);
  8709. redirecting->priv_orig.number.str = ast_malloc(ie_len + 1);
  8710. if (redirecting->priv_orig.number.str) {
  8711. memcpy(redirecting->priv_orig.number.str, data + pos, ie_len);
  8712. redirecting->priv_orig.number.str[ie_len] = 0;
  8713. }
  8714. break;
  8715. case AST_REDIRECTING_PRIV_ORIG_NUMBER_PLAN:
  8716. if (ie_len != 1) {
  8717. ast_log(LOG_WARNING, "Invalid private redirecting-orig numbering plan (%u)\n",
  8718. (unsigned) ie_len);
  8719. break;
  8720. }
  8721. redirecting->priv_orig.number.plan = data[pos];
  8722. break;
  8723. case AST_REDIRECTING_PRIV_ORIG_NUMBER_PRESENTATION:
  8724. if (ie_len != 1) {
  8725. ast_log(LOG_WARNING, "Invalid private redirecting-orig number presentation (%u)\n",
  8726. (unsigned) ie_len);
  8727. break;
  8728. }
  8729. redirecting->priv_orig.number.presentation = data[pos];
  8730. break;
  8731. case AST_REDIRECTING_PRIV_ORIG_NUMBER_VALID:
  8732. if (ie_len != 1) {
  8733. ast_log(LOG_WARNING, "Invalid private redirecting-orig number valid (%u)\n",
  8734. (unsigned) ie_len);
  8735. break;
  8736. }
  8737. redirecting->priv_orig.number.valid = data[pos];
  8738. break;
  8739. /* Private redirecting-orig party id subaddress */
  8740. case AST_REDIRECTING_PRIV_ORIG_SUBADDRESS:
  8741. ast_free(redirecting->priv_orig.subaddress.str);
  8742. redirecting->priv_orig.subaddress.str = ast_malloc(ie_len + 1);
  8743. if (redirecting->priv_orig.subaddress.str) {
  8744. memcpy(redirecting->priv_orig.subaddress.str, data + pos, ie_len);
  8745. redirecting->priv_orig.subaddress.str[ie_len] = 0;
  8746. }
  8747. break;
  8748. case AST_REDIRECTING_PRIV_ORIG_SUBADDRESS_TYPE:
  8749. if (ie_len != 1) {
  8750. ast_log(LOG_WARNING, "Invalid private redirecting-orig type of subaddress (%u)\n",
  8751. (unsigned) ie_len);
  8752. break;
  8753. }
  8754. redirecting->priv_orig.subaddress.type = data[pos];
  8755. break;
  8756. case AST_REDIRECTING_PRIV_ORIG_SUBADDRESS_ODD_EVEN:
  8757. if (ie_len != 1) {
  8758. ast_log(LOG_WARNING,
  8759. "Invalid private redirecting-orig subaddress odd-even indicator (%u)\n",
  8760. (unsigned) ie_len);
  8761. break;
  8762. }
  8763. redirecting->priv_orig.subaddress.odd_even_indicator = data[pos];
  8764. break;
  8765. case AST_REDIRECTING_PRIV_ORIG_SUBADDRESS_VALID:
  8766. if (ie_len != 1) {
  8767. ast_log(LOG_WARNING, "Invalid private redirecting-orig subaddress valid (%u)\n",
  8768. (unsigned) ie_len);
  8769. break;
  8770. }
  8771. redirecting->priv_orig.subaddress.valid = data[pos];
  8772. break;
  8773. /* Private redirecting-orig party id tag */
  8774. case AST_REDIRECTING_PRIV_ORIG_TAG:
  8775. ast_free(redirecting->priv_orig.tag);
  8776. redirecting->priv_orig.tag = ast_malloc(ie_len + 1);
  8777. if (redirecting->priv_orig.tag) {
  8778. memcpy(redirecting->priv_orig.tag, data + pos, ie_len);
  8779. redirecting->priv_orig.tag[ie_len] = 0;
  8780. }
  8781. break;
  8782. /* Private redirecting-from party id name */
  8783. case AST_REDIRECTING_PRIV_FROM_NAME:
  8784. ast_free(redirecting->priv_from.name.str);
  8785. redirecting->priv_from.name.str = ast_malloc(ie_len + 1);
  8786. if (redirecting->priv_from.name.str) {
  8787. memcpy(redirecting->priv_from.name.str, data + pos, ie_len);
  8788. redirecting->priv_from.name.str[ie_len] = 0;
  8789. }
  8790. break;
  8791. case AST_REDIRECTING_PRIV_FROM_NAME_CHAR_SET:
  8792. if (ie_len != 1) {
  8793. ast_log(LOG_WARNING, "Invalid private redirecting-from name char set (%u)\n",
  8794. (unsigned) ie_len);
  8795. break;
  8796. }
  8797. redirecting->priv_from.name.char_set = data[pos];
  8798. break;
  8799. case AST_REDIRECTING_PRIV_FROM_NAME_PRESENTATION:
  8800. if (ie_len != 1) {
  8801. ast_log(LOG_WARNING, "Invalid private redirecting-from name presentation (%u)\n",
  8802. (unsigned) ie_len);
  8803. break;
  8804. }
  8805. redirecting->priv_from.name.presentation = data[pos];
  8806. break;
  8807. case AST_REDIRECTING_PRIV_FROM_NAME_VALID:
  8808. if (ie_len != 1) {
  8809. ast_log(LOG_WARNING, "Invalid private redirecting-from name valid (%u)\n",
  8810. (unsigned) ie_len);
  8811. break;
  8812. }
  8813. redirecting->priv_from.name.valid = data[pos];
  8814. break;
  8815. /* Private redirecting-from party id number */
  8816. case AST_REDIRECTING_PRIV_FROM_NUMBER:
  8817. ast_free(redirecting->priv_from.number.str);
  8818. redirecting->priv_from.number.str = ast_malloc(ie_len + 1);
  8819. if (redirecting->priv_from.number.str) {
  8820. memcpy(redirecting->priv_from.number.str, data + pos, ie_len);
  8821. redirecting->priv_from.number.str[ie_len] = 0;
  8822. }
  8823. break;
  8824. case AST_REDIRECTING_PRIV_FROM_NUMBER_PLAN:
  8825. if (ie_len != 1) {
  8826. ast_log(LOG_WARNING, "Invalid private redirecting-from numbering plan (%u)\n",
  8827. (unsigned) ie_len);
  8828. break;
  8829. }
  8830. redirecting->priv_from.number.plan = data[pos];
  8831. break;
  8832. case AST_REDIRECTING_PRIV_FROM_NUMBER_PRESENTATION:
  8833. if (ie_len != 1) {
  8834. ast_log(LOG_WARNING, "Invalid private redirecting-from number presentation (%u)\n",
  8835. (unsigned) ie_len);
  8836. break;
  8837. }
  8838. redirecting->priv_from.number.presentation = data[pos];
  8839. break;
  8840. case AST_REDIRECTING_PRIV_FROM_NUMBER_VALID:
  8841. if (ie_len != 1) {
  8842. ast_log(LOG_WARNING, "Invalid private redirecting-from number valid (%u)\n",
  8843. (unsigned) ie_len);
  8844. break;
  8845. }
  8846. redirecting->priv_from.number.valid = data[pos];
  8847. break;
  8848. /* Private redirecting-from party id subaddress */
  8849. case AST_REDIRECTING_PRIV_FROM_SUBADDRESS:
  8850. ast_free(redirecting->priv_from.subaddress.str);
  8851. redirecting->priv_from.subaddress.str = ast_malloc(ie_len + 1);
  8852. if (redirecting->priv_from.subaddress.str) {
  8853. memcpy(redirecting->priv_from.subaddress.str, data + pos, ie_len);
  8854. redirecting->priv_from.subaddress.str[ie_len] = 0;
  8855. }
  8856. break;
  8857. case AST_REDIRECTING_PRIV_FROM_SUBADDRESS_TYPE:
  8858. if (ie_len != 1) {
  8859. ast_log(LOG_WARNING, "Invalid private redirecting-from type of subaddress (%u)\n",
  8860. (unsigned) ie_len);
  8861. break;
  8862. }
  8863. redirecting->priv_from.subaddress.type = data[pos];
  8864. break;
  8865. case AST_REDIRECTING_PRIV_FROM_SUBADDRESS_ODD_EVEN:
  8866. if (ie_len != 1) {
  8867. ast_log(LOG_WARNING,
  8868. "Invalid private redirecting-from subaddress odd-even indicator (%u)\n",
  8869. (unsigned) ie_len);
  8870. break;
  8871. }
  8872. redirecting->priv_from.subaddress.odd_even_indicator = data[pos];
  8873. break;
  8874. case AST_REDIRECTING_PRIV_FROM_SUBADDRESS_VALID:
  8875. if (ie_len != 1) {
  8876. ast_log(LOG_WARNING, "Invalid private redirecting-from subaddress valid (%u)\n",
  8877. (unsigned) ie_len);
  8878. break;
  8879. }
  8880. redirecting->priv_from.subaddress.valid = data[pos];
  8881. break;
  8882. /* Private redirecting-from party id tag */
  8883. case AST_REDIRECTING_PRIV_FROM_TAG:
  8884. ast_free(redirecting->priv_from.tag);
  8885. redirecting->priv_from.tag = ast_malloc(ie_len + 1);
  8886. if (redirecting->priv_from.tag) {
  8887. memcpy(redirecting->priv_from.tag, data + pos, ie_len);
  8888. redirecting->priv_from.tag[ie_len] = 0;
  8889. }
  8890. break;
  8891. /* Private redirecting-to party id name */
  8892. case AST_REDIRECTING_PRIV_TO_NAME:
  8893. ast_free(redirecting->priv_to.name.str);
  8894. redirecting->priv_to.name.str = ast_malloc(ie_len + 1);
  8895. if (redirecting->priv_to.name.str) {
  8896. memcpy(redirecting->priv_to.name.str, data + pos, ie_len);
  8897. redirecting->priv_to.name.str[ie_len] = 0;
  8898. }
  8899. break;
  8900. case AST_REDIRECTING_PRIV_TO_NAME_CHAR_SET:
  8901. if (ie_len != 1) {
  8902. ast_log(LOG_WARNING, "Invalid private redirecting-to name char set (%u)\n",
  8903. (unsigned) ie_len);
  8904. break;
  8905. }
  8906. redirecting->priv_to.name.char_set = data[pos];
  8907. break;
  8908. case AST_REDIRECTING_PRIV_TO_NAME_PRESENTATION:
  8909. if (ie_len != 1) {
  8910. ast_log(LOG_WARNING, "Invalid private redirecting-to name presentation (%u)\n",
  8911. (unsigned) ie_len);
  8912. break;
  8913. }
  8914. redirecting->priv_to.name.presentation = data[pos];
  8915. break;
  8916. case AST_REDIRECTING_PRIV_TO_NAME_VALID:
  8917. if (ie_len != 1) {
  8918. ast_log(LOG_WARNING, "Invalid private redirecting-to name valid (%u)\n",
  8919. (unsigned) ie_len);
  8920. break;
  8921. }
  8922. redirecting->priv_to.name.valid = data[pos];
  8923. break;
  8924. /* Private redirecting-to party id number */
  8925. case AST_REDIRECTING_PRIV_TO_NUMBER:
  8926. ast_free(redirecting->priv_to.number.str);
  8927. redirecting->priv_to.number.str = ast_malloc(ie_len + 1);
  8928. if (redirecting->priv_to.number.str) {
  8929. memcpy(redirecting->priv_to.number.str, data + pos, ie_len);
  8930. redirecting->priv_to.number.str[ie_len] = 0;
  8931. }
  8932. break;
  8933. case AST_REDIRECTING_PRIV_TO_NUMBER_PLAN:
  8934. if (ie_len != 1) {
  8935. ast_log(LOG_WARNING, "Invalid private redirecting-to numbering plan (%u)\n",
  8936. (unsigned) ie_len);
  8937. break;
  8938. }
  8939. redirecting->priv_to.number.plan = data[pos];
  8940. break;
  8941. case AST_REDIRECTING_PRIV_TO_NUMBER_PRESENTATION:
  8942. if (ie_len != 1) {
  8943. ast_log(LOG_WARNING, "Invalid private redirecting-to number presentation (%u)\n",
  8944. (unsigned) ie_len);
  8945. break;
  8946. }
  8947. redirecting->priv_to.number.presentation = data[pos];
  8948. break;
  8949. case AST_REDIRECTING_PRIV_TO_NUMBER_VALID:
  8950. if (ie_len != 1) {
  8951. ast_log(LOG_WARNING, "Invalid private redirecting-to number valid (%u)\n",
  8952. (unsigned) ie_len);
  8953. break;
  8954. }
  8955. redirecting->priv_to.number.valid = data[pos];
  8956. break;
  8957. /* Private redirecting-to party id subaddress */
  8958. case AST_REDIRECTING_PRIV_TO_SUBADDRESS:
  8959. ast_free(redirecting->priv_to.subaddress.str);
  8960. redirecting->priv_to.subaddress.str = ast_malloc(ie_len + 1);
  8961. if (redirecting->priv_to.subaddress.str) {
  8962. memcpy(redirecting->priv_to.subaddress.str, data + pos, ie_len);
  8963. redirecting->priv_to.subaddress.str[ie_len] = 0;
  8964. }
  8965. break;
  8966. case AST_REDIRECTING_PRIV_TO_SUBADDRESS_TYPE:
  8967. if (ie_len != 1) {
  8968. ast_log(LOG_WARNING, "Invalid private redirecting-to type of subaddress (%u)\n",
  8969. (unsigned) ie_len);
  8970. break;
  8971. }
  8972. redirecting->priv_to.subaddress.type = data[pos];
  8973. break;
  8974. case AST_REDIRECTING_PRIV_TO_SUBADDRESS_ODD_EVEN:
  8975. if (ie_len != 1) {
  8976. ast_log(LOG_WARNING,
  8977. "Invalid private redirecting-to subaddress odd-even indicator (%u)\n",
  8978. (unsigned) ie_len);
  8979. break;
  8980. }
  8981. redirecting->priv_to.subaddress.odd_even_indicator = data[pos];
  8982. break;
  8983. case AST_REDIRECTING_PRIV_TO_SUBADDRESS_VALID:
  8984. if (ie_len != 1) {
  8985. ast_log(LOG_WARNING, "Invalid private redirecting-to subaddress valid (%u)\n",
  8986. (unsigned) ie_len);
  8987. break;
  8988. }
  8989. redirecting->priv_to.subaddress.valid = data[pos];
  8990. break;
  8991. /* Private redirecting-to party id tag */
  8992. case AST_REDIRECTING_PRIV_TO_TAG:
  8993. ast_free(redirecting->priv_to.tag);
  8994. redirecting->priv_to.tag = ast_malloc(ie_len + 1);
  8995. if (redirecting->priv_to.tag) {
  8996. memcpy(redirecting->priv_to.tag, data + pos, ie_len);
  8997. redirecting->priv_to.tag[ie_len] = 0;
  8998. }
  8999. break;
  9000. /* Redirecting reason code */
  9001. case AST_REDIRECTING_REASON_CODE:
  9002. if (ie_len != sizeof(value)) {
  9003. ast_log(LOG_WARNING, "Invalid redirecting reason (%u)\n",
  9004. (unsigned) ie_len);
  9005. break;
  9006. }
  9007. memcpy(&value, data + pos, sizeof(value));
  9008. redirecting->reason.code = ntohl(value);
  9009. break;
  9010. /* Redirecting reason string */
  9011. case AST_REDIRECTING_REASON_STR:
  9012. ast_free(redirecting->reason.str);
  9013. redirecting->reason.str = ast_malloc(ie_len + 1);
  9014. if (redirecting->reason.str) {
  9015. memcpy(redirecting->reason.str, data + pos, ie_len);
  9016. redirecting->reason.str[ie_len] = 0;
  9017. }
  9018. break;
  9019. /* Redirecting orig-reason code */
  9020. case AST_REDIRECTING_ORIG_REASON_CODE:
  9021. if (ie_len != sizeof(value)) {
  9022. ast_log(LOG_WARNING, "Invalid redirecting original reason (%u)\n",
  9023. (unsigned) ie_len);
  9024. break;
  9025. }
  9026. memcpy(&value, data + pos, sizeof(value));
  9027. redirecting->orig_reason.code = ntohl(value);
  9028. break;
  9029. /* Redirecting orig-reason string */
  9030. case AST_REDIRECTING_ORIG_REASON_STR:
  9031. ast_free(redirecting->orig_reason.str);
  9032. redirecting->orig_reason.str = ast_malloc(ie_len + 1);
  9033. if (redirecting->orig_reason.str) {
  9034. memcpy(redirecting->orig_reason.str, data + pos, ie_len);
  9035. redirecting->orig_reason.str[ie_len] = 0;
  9036. }
  9037. break;
  9038. /* Redirecting count */
  9039. case AST_REDIRECTING_COUNT:
  9040. if (ie_len != sizeof(value)) {
  9041. ast_log(LOG_WARNING, "Invalid redirecting count (%u)\n",
  9042. (unsigned) ie_len);
  9043. break;
  9044. }
  9045. memcpy(&value, data + pos, sizeof(value));
  9046. redirecting->count = ntohl(value);
  9047. break;
  9048. /* Redirecting unknown element */
  9049. default:
  9050. ast_debug(1, "Unknown redirecting element: %u (%u)\n",
  9051. (unsigned) ie_id, (unsigned) ie_len);
  9052. break;
  9053. }
  9054. }
  9055. switch (frame_version) {
  9056. case 1:
  9057. /*
  9058. * The other end is an earlier version that we need to adjust
  9059. * for compatibility.
  9060. *
  9061. * The earlier version did not have the orig party id or
  9062. * orig_reason value.
  9063. */
  9064. redirecting->from.name.valid = 1;
  9065. redirecting->from.name.char_set = AST_PARTY_CHAR_SET_ISO8859_1;
  9066. redirecting->from.number.valid = 1;
  9067. if (got_from_combined_presentation) {
  9068. redirecting->from.name.presentation = from_combined_presentation;
  9069. redirecting->from.number.presentation = from_combined_presentation;
  9070. }
  9071. redirecting->to.name.valid = 1;
  9072. redirecting->to.name.char_set = AST_PARTY_CHAR_SET_ISO8859_1;
  9073. redirecting->to.number.valid = 1;
  9074. if (got_to_combined_presentation) {
  9075. redirecting->to.name.presentation = to_combined_presentation;
  9076. redirecting->to.number.presentation = to_combined_presentation;
  9077. }
  9078. break;
  9079. case 2:
  9080. /* The other end is at the same level as we are. */
  9081. break;
  9082. default:
  9083. /*
  9084. * The other end is newer than we are.
  9085. * We need to assume that they are compatible with us.
  9086. */
  9087. ast_debug(1, "Redirecting frame has newer version: %u\n",
  9088. (unsigned) frame_version);
  9089. break;
  9090. }
  9091. return 0;
  9092. }
  9093. void ast_channel_update_redirecting(struct ast_channel *chan, const struct ast_party_redirecting *redirecting, const struct ast_set_party_redirecting *update)
  9094. {
  9095. unsigned char data[1024]; /* This should be large enough */
  9096. size_t datalen;
  9097. datalen = ast_redirecting_build_data(data, sizeof(data), redirecting, update);
  9098. if (datalen == (size_t) -1) {
  9099. return;
  9100. }
  9101. ast_indicate_data(chan, AST_CONTROL_REDIRECTING, data, datalen);
  9102. }
  9103. void ast_channel_queue_redirecting_update(struct ast_channel *chan, const struct ast_party_redirecting *redirecting, const struct ast_set_party_redirecting *update)
  9104. {
  9105. unsigned char data[1024]; /* This should be large enough */
  9106. size_t datalen;
  9107. datalen = ast_redirecting_build_data(data, sizeof(data), redirecting, update);
  9108. if (datalen == (size_t) -1) {
  9109. return;
  9110. }
  9111. ast_queue_control_data(chan, AST_CONTROL_REDIRECTING, data, datalen);
  9112. }
  9113. /*!
  9114. * Storage to determine if the current thread is running an intercept dialplan routine.
  9115. */
  9116. AST_THREADSTORAGE_RAW(in_intercept_routine);
  9117. /*!
  9118. * \internal
  9119. * \brief Set the current intercept dialplan routine status mode.
  9120. * \since 13.14.0
  9121. *
  9122. * \param in_intercept_mode New intercept mode. (Non-zero if in intercept mode)
  9123. *
  9124. * \return Nothing
  9125. */
  9126. static void channel_set_intercept_mode(int in_intercept_mode)
  9127. {
  9128. int status;
  9129. status = ast_threadstorage_set_ptr(&in_intercept_routine,
  9130. in_intercept_mode ? (void *) 1 : (void *) 0);
  9131. if (status) {
  9132. ast_log(LOG_ERROR, "Failed to set dialplan intercept mode\n");
  9133. }
  9134. }
  9135. int ast_channel_get_intercept_mode(void)
  9136. {
  9137. return ast_threadstorage_get_ptr(&in_intercept_routine) ? 1 : 0;
  9138. }
  9139. int ast_channel_connected_line_macro(struct ast_channel *autoservice_chan, struct ast_channel *macro_chan, const void *connected_info, int is_caller, int is_frame)
  9140. {
  9141. static int deprecation_warning = 0;
  9142. const char *macro;
  9143. const char *macro_args;
  9144. int retval;
  9145. ast_channel_lock(macro_chan);
  9146. macro = pbx_builtin_getvar_helper(macro_chan, is_caller
  9147. ? "CONNECTED_LINE_CALLER_SEND_MACRO" : "CONNECTED_LINE_CALLEE_SEND_MACRO");
  9148. macro = ast_strdupa(S_OR(macro, ""));
  9149. macro_args = pbx_builtin_getvar_helper(macro_chan, is_caller
  9150. ? "CONNECTED_LINE_CALLER_SEND_MACRO_ARGS" : "CONNECTED_LINE_CALLEE_SEND_MACRO_ARGS");
  9151. macro_args = ast_strdupa(S_OR(macro_args, ""));
  9152. if (ast_strlen_zero(macro)) {
  9153. ast_channel_unlock(macro_chan);
  9154. return -1;
  9155. }
  9156. if (!deprecation_warning) {
  9157. deprecation_warning = 1;
  9158. ast_log(LOG_WARNING, "Usage of CONNECTED_LINE_CALLE[ER]_SEND_MACRO is deprecated. Please use CONNECTED_LINE_SEND_SUB instead.\n");
  9159. }
  9160. if (is_frame) {
  9161. const struct ast_frame *frame = connected_info;
  9162. ast_connected_line_parse_data(frame->data.ptr, frame->datalen, ast_channel_connected(macro_chan));
  9163. } else {
  9164. const struct ast_party_connected_line *connected = connected_info;
  9165. ast_party_connected_line_copy(ast_channel_connected(macro_chan), connected);
  9166. }
  9167. ast_channel_unlock(macro_chan);
  9168. channel_set_intercept_mode(1);
  9169. retval = ast_app_run_macro(autoservice_chan, macro_chan, macro, macro_args);
  9170. channel_set_intercept_mode(0);
  9171. if (!retval) {
  9172. struct ast_party_connected_line saved_connected;
  9173. ast_party_connected_line_init(&saved_connected);
  9174. ast_channel_lock(macro_chan);
  9175. ast_party_connected_line_copy(&saved_connected, ast_channel_connected(macro_chan));
  9176. ast_channel_unlock(macro_chan);
  9177. ast_channel_update_connected_line(macro_chan, &saved_connected, NULL);
  9178. ast_party_connected_line_free(&saved_connected);
  9179. }
  9180. return retval;
  9181. }
  9182. int ast_channel_redirecting_macro(struct ast_channel *autoservice_chan, struct ast_channel *macro_chan, const void *redirecting_info, int is_caller, int is_frame)
  9183. {
  9184. static int deprecation_warning = 0;
  9185. const char *macro;
  9186. const char *macro_args;
  9187. int retval;
  9188. ast_channel_lock(macro_chan);
  9189. macro = pbx_builtin_getvar_helper(macro_chan, is_caller
  9190. ? "REDIRECTING_CALLER_SEND_MACRO" : "REDIRECTING_CALLEE_SEND_MACRO");
  9191. macro = ast_strdupa(S_OR(macro, ""));
  9192. macro_args = pbx_builtin_getvar_helper(macro_chan, is_caller
  9193. ? "REDIRECTING_CALLER_SEND_MACRO_ARGS" : "REDIRECTING_CALLEE_SEND_MACRO_ARGS");
  9194. macro_args = ast_strdupa(S_OR(macro_args, ""));
  9195. if (ast_strlen_zero(macro)) {
  9196. ast_channel_unlock(macro_chan);
  9197. return -1;
  9198. }
  9199. if (!deprecation_warning) {
  9200. deprecation_warning = 1;
  9201. ast_log(LOG_WARNING, "Usage of REDIRECTING_CALLE[ER]_SEND_MACRO is deprecated. Please use REDIRECTING_SEND_SUB instead.\n");
  9202. }
  9203. if (is_frame) {
  9204. const struct ast_frame *frame = redirecting_info;
  9205. ast_redirecting_parse_data(frame->data.ptr, frame->datalen, ast_channel_redirecting(macro_chan));
  9206. } else {
  9207. const struct ast_party_redirecting *redirecting = redirecting_info;
  9208. ast_party_redirecting_copy(ast_channel_redirecting(macro_chan), redirecting);
  9209. }
  9210. ast_channel_unlock(macro_chan);
  9211. channel_set_intercept_mode(1);
  9212. retval = ast_app_run_macro(autoservice_chan, macro_chan, macro, macro_args);
  9213. channel_set_intercept_mode(0);
  9214. if (!retval) {
  9215. struct ast_party_redirecting saved_redirecting;
  9216. ast_party_redirecting_init(&saved_redirecting);
  9217. ast_channel_lock(macro_chan);
  9218. ast_party_redirecting_copy(&saved_redirecting, ast_channel_redirecting(macro_chan));
  9219. ast_channel_unlock(macro_chan);
  9220. ast_channel_update_redirecting(macro_chan, &saved_redirecting, NULL);
  9221. ast_party_redirecting_free(&saved_redirecting);
  9222. }
  9223. return retval;
  9224. }
  9225. int ast_channel_connected_line_sub(struct ast_channel *autoservice_chan, struct ast_channel *sub_chan, const void *connected_info, int is_frame)
  9226. {
  9227. const char *sub;
  9228. const char *sub_args;
  9229. int retval;
  9230. ast_channel_lock(sub_chan);
  9231. sub = pbx_builtin_getvar_helper(sub_chan, "CONNECTED_LINE_SEND_SUB");
  9232. sub = ast_strdupa(S_OR(sub, ""));
  9233. sub_args = pbx_builtin_getvar_helper(sub_chan, "CONNECTED_LINE_SEND_SUB_ARGS");
  9234. sub_args = ast_strdupa(S_OR(sub_args, ""));
  9235. if (ast_strlen_zero(sub)) {
  9236. ast_channel_unlock(sub_chan);
  9237. return -1;
  9238. }
  9239. if (is_frame) {
  9240. const struct ast_frame *frame = connected_info;
  9241. ast_connected_line_parse_data(frame->data.ptr, frame->datalen, ast_channel_connected(sub_chan));
  9242. } else {
  9243. const struct ast_party_connected_line *connected = connected_info;
  9244. ast_party_connected_line_copy(ast_channel_connected(sub_chan), connected);
  9245. }
  9246. ast_channel_unlock(sub_chan);
  9247. channel_set_intercept_mode(1);
  9248. retval = ast_app_run_sub(autoservice_chan, sub_chan, sub, sub_args, 0);
  9249. channel_set_intercept_mode(0);
  9250. if (!retval) {
  9251. struct ast_party_connected_line saved_connected;
  9252. ast_party_connected_line_init(&saved_connected);
  9253. ast_channel_lock(sub_chan);
  9254. ast_party_connected_line_copy(&saved_connected, ast_channel_connected(sub_chan));
  9255. ast_channel_unlock(sub_chan);
  9256. ast_channel_update_connected_line(sub_chan, &saved_connected, NULL);
  9257. ast_party_connected_line_free(&saved_connected);
  9258. }
  9259. return retval;
  9260. }
  9261. int ast_channel_redirecting_sub(struct ast_channel *autoservice_chan, struct ast_channel *sub_chan, const void *redirecting_info, int is_frame)
  9262. {
  9263. const char *sub;
  9264. const char *sub_args;
  9265. int retval;
  9266. ast_channel_lock(sub_chan);
  9267. sub = pbx_builtin_getvar_helper(sub_chan, "REDIRECTING_SEND_SUB");
  9268. sub = ast_strdupa(S_OR(sub, ""));
  9269. sub_args = pbx_builtin_getvar_helper(sub_chan, "REDIRECTING_SEND_SUB_ARGS");
  9270. sub_args = ast_strdupa(S_OR(sub_args, ""));
  9271. if (ast_strlen_zero(sub)) {
  9272. ast_channel_unlock(sub_chan);
  9273. return -1;
  9274. }
  9275. if (is_frame) {
  9276. const struct ast_frame *frame = redirecting_info;
  9277. ast_redirecting_parse_data(frame->data.ptr, frame->datalen, ast_channel_redirecting(sub_chan));
  9278. } else {
  9279. const struct ast_party_redirecting *redirecting = redirecting_info;
  9280. ast_party_redirecting_copy(ast_channel_redirecting(sub_chan), redirecting);
  9281. }
  9282. ast_channel_unlock(sub_chan);
  9283. channel_set_intercept_mode(1);
  9284. retval = ast_app_run_sub(autoservice_chan, sub_chan, sub, sub_args, 0);
  9285. channel_set_intercept_mode(0);
  9286. if (!retval) {
  9287. struct ast_party_redirecting saved_redirecting;
  9288. ast_party_redirecting_init(&saved_redirecting);
  9289. ast_channel_lock(sub_chan);
  9290. ast_party_redirecting_copy(&saved_redirecting, ast_channel_redirecting(sub_chan));
  9291. ast_channel_unlock(sub_chan);
  9292. ast_channel_update_redirecting(sub_chan, &saved_redirecting, NULL);
  9293. ast_party_redirecting_free(&saved_redirecting);
  9294. }
  9295. return retval;
  9296. }
  9297. static void *channel_cc_params_copy(void *data)
  9298. {
  9299. const struct ast_cc_config_params *src = data;
  9300. struct ast_cc_config_params *dest = ast_cc_config_params_init();
  9301. if (!dest) {
  9302. return NULL;
  9303. }
  9304. ast_cc_copy_config_params(dest, src);
  9305. return dest;
  9306. }
  9307. static void channel_cc_params_destroy(void *data)
  9308. {
  9309. struct ast_cc_config_params *cc_params = data;
  9310. ast_cc_config_params_destroy(cc_params);
  9311. }
  9312. static const struct ast_datastore_info cc_channel_datastore_info = {
  9313. .type = "Call Completion",
  9314. .duplicate = channel_cc_params_copy,
  9315. .destroy = channel_cc_params_destroy,
  9316. };
  9317. int ast_channel_cc_params_init(struct ast_channel *chan,
  9318. const struct ast_cc_config_params *base_params)
  9319. {
  9320. struct ast_cc_config_params *cc_params;
  9321. struct ast_datastore *cc_datastore;
  9322. if (!(cc_params = ast_cc_config_params_init())) {
  9323. return -1;
  9324. }
  9325. if (!(cc_datastore = ast_datastore_alloc(&cc_channel_datastore_info, NULL))) {
  9326. ast_cc_config_params_destroy(cc_params);
  9327. return -1;
  9328. }
  9329. if (base_params) {
  9330. ast_cc_copy_config_params(cc_params, base_params);
  9331. }
  9332. cc_datastore->data = cc_params;
  9333. ast_channel_datastore_add(chan, cc_datastore);
  9334. return 0;
  9335. }
  9336. struct ast_cc_config_params *ast_channel_get_cc_config_params(struct ast_channel *chan)
  9337. {
  9338. struct ast_datastore *cc_datastore;
  9339. if (!(cc_datastore = ast_channel_datastore_find(chan, &cc_channel_datastore_info, NULL))) {
  9340. /* If we can't find the datastore, it almost definitely means that the channel type being
  9341. * used has not had its driver modified to parse CC config parameters. The best action
  9342. * to take here is to create the parameters on the spot with the defaults set.
  9343. */
  9344. if (ast_channel_cc_params_init(chan, NULL)) {
  9345. return NULL;
  9346. }
  9347. if (!(cc_datastore = ast_channel_datastore_find(chan, &cc_channel_datastore_info, NULL))) {
  9348. /* Should be impossible */
  9349. return NULL;
  9350. }
  9351. }
  9352. ast_assert(cc_datastore->data != NULL);
  9353. return cc_datastore->data;
  9354. }
  9355. int ast_channel_get_device_name(struct ast_channel *chan, char *device_name, size_t name_buffer_length)
  9356. {
  9357. int len = name_buffer_length;
  9358. char *dash;
  9359. if (!ast_channel_queryoption(chan, AST_OPTION_DEVICE_NAME, device_name, &len, 0)) {
  9360. return 0;
  9361. }
  9362. /* Dang. Do it the old-fashioned way */
  9363. ast_copy_string(device_name, ast_channel_name(chan), name_buffer_length);
  9364. if ((dash = strrchr(device_name, '-'))) {
  9365. *dash = '\0';
  9366. }
  9367. return 0;
  9368. }
  9369. int ast_channel_get_cc_agent_type(struct ast_channel *chan, char *agent_type, size_t size)
  9370. {
  9371. int len = size;
  9372. char *slash;
  9373. if (!ast_channel_queryoption(chan, AST_OPTION_CC_AGENT_TYPE, agent_type, &len, 0)) {
  9374. return 0;
  9375. }
  9376. ast_copy_string(agent_type, ast_channel_name(chan), size);
  9377. if ((slash = strchr(agent_type, '/'))) {
  9378. *slash = '\0';
  9379. }
  9380. return 0;
  9381. }
  9382. void ast_channel_unlink(struct ast_channel *chan)
  9383. {
  9384. ao2_unlink(channels, chan);
  9385. }
  9386. struct ast_bridge *ast_channel_get_bridge(const struct ast_channel *chan)
  9387. {
  9388. struct ast_bridge *bridge;
  9389. bridge = ast_channel_internal_bridge(chan);
  9390. if (bridge) {
  9391. ao2_ref(bridge, +1);
  9392. }
  9393. return bridge;
  9394. }
  9395. int ast_channel_is_bridged(const struct ast_channel *chan)
  9396. {
  9397. return ast_channel_internal_bridge(chan) != NULL;
  9398. }
  9399. int ast_channel_is_leaving_bridge(struct ast_channel *chan)
  9400. {
  9401. int hangup_flags = ast_channel_softhangup_internal_flag(chan);
  9402. int hangup_test = hangup_flags & AST_SOFTHANGUP_ASYNCGOTO;
  9403. int unbridge = ast_channel_unbridged(chan);
  9404. /* This function should only return true if either the unbridged flag or
  9405. * the ASYNCGOTO soft hangup flag is set and when no other soft hangup
  9406. * flags are set. Any other soft hangup flags being set should make it
  9407. * return false.
  9408. */
  9409. return ((hangup_test || unbridge) && (hangup_test == hangup_flags));
  9410. }
  9411. struct ast_channel *ast_channel_bridge_peer(struct ast_channel *chan)
  9412. {
  9413. struct ast_channel *peer;
  9414. struct ast_bridge *bridge;
  9415. /* Get the bridge the channel is in. */
  9416. ast_channel_lock(chan);
  9417. bridge = ast_channel_get_bridge(chan);
  9418. ast_channel_unlock(chan);
  9419. if (!bridge) {
  9420. return NULL;
  9421. }
  9422. peer = ast_bridge_peer(bridge, chan);
  9423. ao2_ref(bridge, -1);
  9424. return peer;
  9425. }
  9426. struct ast_bridge_channel *ast_channel_get_bridge_channel(struct ast_channel *chan)
  9427. {
  9428. struct ast_bridge_channel *bridge_channel;
  9429. bridge_channel = ast_channel_internal_bridge_channel(chan);
  9430. if (bridge_channel) {
  9431. ao2_ref(bridge_channel, +1);
  9432. }
  9433. return bridge_channel;
  9434. }
  9435. struct ast_channel *ast_channel_yank(struct ast_channel *yankee)
  9436. {
  9437. struct ast_channel *yanked_chan;
  9438. struct {
  9439. char *accountcode;
  9440. char *exten;
  9441. char *context;
  9442. char *name;
  9443. int amaflags;
  9444. int priority;
  9445. struct ast_format *readformat;
  9446. struct ast_format *writeformat;
  9447. } my_vars = { 0, };
  9448. ast_channel_lock(yankee);
  9449. my_vars.accountcode = ast_strdupa(ast_channel_accountcode(yankee));
  9450. my_vars.exten = ast_strdupa(ast_channel_exten(yankee));
  9451. my_vars.context = ast_strdupa(ast_channel_context(yankee));
  9452. my_vars.name = ast_strdupa(ast_channel_name(yankee));
  9453. my_vars.amaflags = ast_channel_amaflags(yankee);
  9454. my_vars.priority = ast_channel_priority(yankee);
  9455. /* The priority as returned by ast_channel_yank is where the channel
  9456. * should go if the dialplan is executed on it. If the channel is
  9457. * already executing dialplan then the priority currently set is
  9458. * where it is currently. We increment it so it becomes where it should
  9459. * execute.
  9460. */
  9461. if (ast_test_flag(ast_channel_flags(yankee), AST_FLAG_IN_AUTOLOOP)) {
  9462. my_vars.priority++;
  9463. }
  9464. my_vars.writeformat = ao2_bump(ast_channel_writeformat(yankee));
  9465. my_vars.readformat = ao2_bump(ast_channel_readformat(yankee));
  9466. ast_channel_unlock(yankee);
  9467. /* Do not hold any channel locks while calling channel_alloc() since the function
  9468. * locks the channel container when linking the new channel in. */
  9469. if (!(yanked_chan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, my_vars.accountcode,
  9470. my_vars.exten, my_vars.context, NULL, yankee, my_vars.amaflags,
  9471. "Surrogate/%s", my_vars.name))) {
  9472. ao2_cleanup(my_vars.writeformat);
  9473. ao2_cleanup(my_vars.readformat);
  9474. return NULL;
  9475. }
  9476. /* Make formats okay */
  9477. ast_channel_set_readformat(yanked_chan, my_vars.readformat);
  9478. ast_channel_set_writeformat(yanked_chan, my_vars.writeformat);
  9479. ao2_cleanup(my_vars.readformat);
  9480. ao2_cleanup(my_vars.writeformat);
  9481. ast_channel_priority_set(yanked_chan, my_vars.priority);
  9482. ast_channel_unlock(yanked_chan);
  9483. if (ast_channel_move(yanked_chan, yankee)) {
  9484. ast_hangup(yanked_chan);
  9485. return NULL;
  9486. }
  9487. return yanked_chan;
  9488. }
  9489. /*!
  9490. * Mutex that prevents multiple ast_channel_move() operations
  9491. * from occurring simultaneously. This is necessary since the
  9492. * involved channels have to be locked and unlocked throughout
  9493. * the move operation.
  9494. *
  9495. * The most important data being protected are the masq and masqr
  9496. * data on channels. We don't want them getting criss-crossed due
  9497. * to multiple moves mucking with them.
  9498. */
  9499. AST_MUTEX_DEFINE_STATIC(channel_move_lock);
  9500. int ast_channel_move(struct ast_channel *dest, struct ast_channel *source)
  9501. {
  9502. SCOPED_MUTEX(lock, &channel_move_lock);
  9503. if (dest == source) {
  9504. ast_log(LOG_WARNING, "Can't move channel '%s' into itself!\n",
  9505. ast_channel_name(dest));
  9506. return -1;
  9507. }
  9508. ast_channel_lock_both(dest, source);
  9509. if (ast_test_flag(ast_channel_flags(dest), AST_FLAG_ZOMBIE)
  9510. || ast_test_flag(ast_channel_flags(source), AST_FLAG_ZOMBIE)) {
  9511. /* Zombies! Run! */
  9512. ast_log(LOG_WARNING,
  9513. "Can't move channel. One or both is dead (%s <-- %s)\n",
  9514. ast_channel_name(dest), ast_channel_name(source));
  9515. ast_channel_unlock(source);
  9516. ast_channel_unlock(dest);
  9517. return -1;
  9518. }
  9519. ast_channel_masq_set(dest, source);
  9520. ast_channel_masqr_set(source, dest);
  9521. ast_channel_unlock(dest);
  9522. ast_channel_unlock(source);
  9523. channel_do_masquerade(dest, source);
  9524. return 0;
  9525. }
  9526. static void suppress_datastore_destroy_cb(void *data)
  9527. {
  9528. ao2_cleanup(data);
  9529. }
  9530. static const struct ast_datastore_info suppress_datastore_voice = {
  9531. .type = "suppressvoice",
  9532. .destroy = suppress_datastore_destroy_cb
  9533. };
  9534. static void suppress_framehook_destroy_cb(void *data)
  9535. {
  9536. ao2_cleanup(data);
  9537. }
  9538. struct suppress_data {
  9539. enum ast_frame_type frametype;
  9540. unsigned int direction;
  9541. int framehook_id;
  9542. };
  9543. static void suppress_framehook_fixup_cb(void *data, int framehook_id, struct ast_channel *old_chan, struct ast_channel *new_chan)
  9544. {
  9545. struct suppress_data *suppress = data;
  9546. suppress->framehook_id = framehook_id;
  9547. }
  9548. static struct ast_frame *suppress_framehook_event_cb(struct ast_channel *chan, struct ast_frame *frame, enum ast_framehook_event event, void *data)
  9549. {
  9550. struct suppress_data *suppress = data;
  9551. int suppress_frame = 0;
  9552. if (!frame) {
  9553. return NULL;
  9554. }
  9555. if (frame->frametype != suppress->frametype) {
  9556. return frame;
  9557. }
  9558. if (event == AST_FRAMEHOOK_EVENT_READ && (suppress->direction & AST_MUTE_DIRECTION_READ)) {
  9559. suppress_frame = 1;
  9560. } else if (event == AST_FRAMEHOOK_EVENT_WRITE && (suppress->direction & AST_MUTE_DIRECTION_WRITE)) {
  9561. suppress_frame = 1;
  9562. }
  9563. if (suppress_frame) {
  9564. switch (frame->frametype) {
  9565. case AST_FRAME_VOICE:
  9566. frame = &ast_null_frame;
  9567. break;
  9568. default:
  9569. break;
  9570. }
  9571. }
  9572. return frame;
  9573. }
  9574. static const struct ast_datastore_info *suppress_get_datastore_information(enum ast_frame_type frametype)
  9575. {
  9576. switch (frametype) {
  9577. case AST_FRAME_VOICE:
  9578. return &suppress_datastore_voice;
  9579. default:
  9580. return NULL;
  9581. }
  9582. }
  9583. int ast_channel_suppress(struct ast_channel *chan, unsigned int direction, enum ast_frame_type frametype)
  9584. {
  9585. struct suppress_data *suppress;
  9586. const struct ast_datastore_info *datastore_info = NULL;
  9587. struct ast_datastore *datastore = NULL;
  9588. struct ast_framehook_interface interface = {
  9589. .version = AST_FRAMEHOOK_INTERFACE_VERSION,
  9590. .event_cb = suppress_framehook_event_cb,
  9591. .destroy_cb = suppress_framehook_destroy_cb,
  9592. .chan_fixup_cb = suppress_framehook_fixup_cb,
  9593. };
  9594. int framehook_id;
  9595. if (!(datastore_info = suppress_get_datastore_information(frametype))) {
  9596. ast_log(LOG_WARNING, "Attempted to suppress an unsupported frame type (%u).\n", frametype);
  9597. return -1;
  9598. }
  9599. if ((datastore = ast_channel_datastore_find(chan, datastore_info, NULL))) {
  9600. suppress = datastore->data;
  9601. suppress->direction |= direction;
  9602. return 0;
  9603. }
  9604. if (!(suppress = ao2_alloc(sizeof(*suppress), NULL))) {
  9605. ast_log(LOG_WARNING, "Failed to allocate data while attempting to suppress a stream.\n");
  9606. return -1;
  9607. }
  9608. suppress->frametype = frametype;
  9609. suppress->direction |= direction;
  9610. interface.data = suppress;
  9611. framehook_id = ast_framehook_attach(chan, &interface);
  9612. if (framehook_id < 0) {
  9613. /* Hook attach failed. Get rid of the evidence. */
  9614. ast_log(LOG_WARNING, "Failed to attach framehook while attempting to suppress a stream.\n");
  9615. ao2_ref(suppress, -1);
  9616. return -1;
  9617. }
  9618. /* One ref for the framehook */
  9619. ao2_ref(suppress, +1);
  9620. suppress->framehook_id = framehook_id;
  9621. if (!(datastore = ast_datastore_alloc(datastore_info, NULL))) {
  9622. ast_log(LOG_WARNING, "Failed to allocate datastore while attempting to suppress a stream.\n");
  9623. ast_framehook_detach(chan, framehook_id);
  9624. ao2_ref(suppress, -1);
  9625. return -1;
  9626. }
  9627. /* the ref provided by the allocation is taken by the datastore */
  9628. datastore->data = suppress;
  9629. ast_channel_datastore_add(chan, datastore);
  9630. return 0;
  9631. }
  9632. int ast_channel_unsuppress(struct ast_channel *chan, unsigned int direction, enum ast_frame_type frametype)
  9633. {
  9634. const struct ast_datastore_info *datastore_info = NULL;
  9635. struct ast_datastore *datastore = NULL;
  9636. struct suppress_data *suppress;
  9637. if (!(datastore_info = suppress_get_datastore_information(frametype))) {
  9638. ast_log(LOG_WARNING, "Attempted to unsuppress an unsupported frame type (%u).\n", frametype);
  9639. return -1;
  9640. }
  9641. if (!(datastore = ast_channel_datastore_find(chan, datastore_info, NULL))) {
  9642. /* Nothing to do! */
  9643. return 0;
  9644. }
  9645. suppress = datastore->data;
  9646. suppress->direction &= ~(direction);
  9647. if (suppress->direction == 0) {
  9648. /* Nothing left to suppress. Bye! */
  9649. ast_framehook_detach(chan, suppress->framehook_id);
  9650. ast_channel_datastore_remove(chan, datastore);
  9651. ast_datastore_free(datastore);
  9652. }
  9653. return 0;
  9654. }
  9655. void ast_channel_end_dtmf(struct ast_channel *chan, char digit, struct timeval start, const char *why)
  9656. {
  9657. int dead;
  9658. long duration;
  9659. ast_channel_lock(chan);
  9660. dead = ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE)
  9661. || (ast_channel_softhangup_internal_flag(chan)
  9662. & ~AST_SOFTHANGUP_ASYNCGOTO);
  9663. ast_channel_unlock(chan);
  9664. if (dead) {
  9665. /* Channel is a zombie or a real hangup. */
  9666. return;
  9667. }
  9668. duration = ast_tvdiff_ms(ast_tvnow(), start);
  9669. if (duration < option_dtmfminduration) {
  9670. duration = option_dtmfminduration;
  9671. }
  9672. ast_senddigit_end(chan, digit, duration);
  9673. ast_log(LOG_DTMF, "DTMF end '%c' simulated on %s due to %s, duration %ld ms\n",
  9674. digit, ast_channel_name(chan), why, duration);
  9675. }
  9676. static void features_destroy(void *obj)
  9677. {
  9678. ast_bridge_features_destroy(obj);
  9679. }
  9680. static const struct ast_datastore_info bridge_features_info = {
  9681. .type = "bridge-features",
  9682. .destroy = features_destroy,
  9683. };
  9684. struct ast_bridge_features *ast_channel_feature_hooks_get(struct ast_channel *chan)
  9685. {
  9686. struct ast_datastore *datastore;
  9687. datastore = ast_channel_datastore_find(chan, &bridge_features_info, NULL);
  9688. if (!datastore) {
  9689. return NULL;
  9690. }
  9691. return datastore->data;
  9692. }
  9693. static int channel_feature_hooks_set_full(struct ast_channel *chan, struct ast_bridge_features *features, int replace)
  9694. {
  9695. struct ast_datastore *datastore;
  9696. struct ast_bridge_features *ds_features;
  9697. datastore = ast_channel_datastore_find(chan, &bridge_features_info, NULL);
  9698. if (datastore) {
  9699. ds_features = datastore->data;
  9700. if (replace) {
  9701. ast_bridge_features_cleanup(ds_features);
  9702. ast_bridge_features_init(ds_features);
  9703. }
  9704. if (features) {
  9705. ast_bridge_features_merge(ds_features, features);
  9706. }
  9707. return 0;
  9708. }
  9709. datastore = ast_datastore_alloc(&bridge_features_info, NULL);
  9710. if (!datastore) {
  9711. return -1;
  9712. }
  9713. ds_features = ast_bridge_features_new();
  9714. if (!ds_features) {
  9715. ast_datastore_free(datastore);
  9716. return -1;
  9717. }
  9718. if (features) {
  9719. ast_bridge_features_merge(ds_features, features);
  9720. }
  9721. datastore->data = ds_features;
  9722. ast_channel_datastore_add(chan, datastore);
  9723. return 0;
  9724. }
  9725. int ast_channel_feature_hooks_append(struct ast_channel *chan, struct ast_bridge_features *features)
  9726. {
  9727. return channel_feature_hooks_set_full(chan, features, 0);
  9728. }
  9729. int ast_channel_feature_hooks_replace(struct ast_channel *chan, struct ast_bridge_features *features)
  9730. {
  9731. return channel_feature_hooks_set_full(chan, features, 1);
  9732. }
  9733. enum ast_channel_error ast_channel_errno(void)
  9734. {
  9735. return ast_channel_internal_errno();
  9736. }
  9737. void ast_channel_set_flag(struct ast_channel *chan, unsigned int flag)
  9738. {
  9739. ast_channel_lock(chan);
  9740. ast_set_flag(ast_channel_flags(chan), flag);
  9741. ast_channel_unlock(chan);
  9742. }
  9743. void ast_channel_clear_flag(struct ast_channel *chan, unsigned int flag)
  9744. {
  9745. ast_channel_lock(chan);
  9746. ast_clear_flag(ast_channel_flags(chan), flag);
  9747. ast_channel_unlock(chan);
  9748. }