channel.c 287 KB

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