res_pjsip_session.c 237 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Mark Michelson <mmichelson@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. /*** MODULEINFO
  19. <depend>pjproject</depend>
  20. <depend>res_pjsip</depend>
  21. <support_level>core</support_level>
  22. ***/
  23. #include "asterisk.h"
  24. #include <pjsip.h>
  25. #include <pjsip_ua.h>
  26. #include <pjlib.h>
  27. #include <pjmedia.h>
  28. #include "asterisk/res_pjsip.h"
  29. #include "asterisk/res_pjsip_session.h"
  30. #include "asterisk/res_pjsip_session_caps.h"
  31. #include "asterisk/callerid.h"
  32. #include "asterisk/datastore.h"
  33. #include "asterisk/module.h"
  34. #include "asterisk/logger.h"
  35. #include "asterisk/res_pjsip.h"
  36. #include "asterisk/astobj2.h"
  37. #include "asterisk/lock.h"
  38. #include "asterisk/uuid.h"
  39. #include "asterisk/pbx.h"
  40. #include "asterisk/taskprocessor.h"
  41. #include "asterisk/causes.h"
  42. #include "asterisk/sdp_srtp.h"
  43. #include "asterisk/dsp.h"
  44. #include "asterisk/acl.h"
  45. #include "asterisk/features_config.h"
  46. #include "asterisk/pickup.h"
  47. #include "asterisk/test.h"
  48. #include "asterisk/stream.h"
  49. #include "asterisk/vector.h"
  50. #include "res_pjsip_session/pjsip_session.h"
  51. #define SDP_HANDLER_BUCKETS 11
  52. #define MOD_DATA_ON_RESPONSE "on_response"
  53. #define MOD_DATA_NAT_HOOK "nat_hook"
  54. /* Most common case is one audio and one video stream */
  55. #define DEFAULT_NUM_SESSION_MEDIA 2
  56. /* Some forward declarations */
  57. static void handle_session_begin(struct ast_sip_session *session);
  58. static void handle_session_end(struct ast_sip_session *session);
  59. static void handle_session_destroy(struct ast_sip_session *session);
  60. static void handle_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata);
  61. static void handle_incoming_response(struct ast_sip_session *session, pjsip_rx_data *rdata,
  62. enum ast_sip_session_response_priority response_priority);
  63. static int handle_incoming(struct ast_sip_session *session, pjsip_rx_data *rdata,
  64. enum ast_sip_session_response_priority response_priority);
  65. static void handle_outgoing_request(struct ast_sip_session *session, pjsip_tx_data *tdata);
  66. static void handle_outgoing_response(struct ast_sip_session *session, pjsip_tx_data *tdata);
  67. static int sip_session_refresh(struct ast_sip_session *session,
  68. ast_sip_session_request_creation_cb on_request_creation,
  69. ast_sip_session_sdp_creation_cb on_sdp_creation,
  70. ast_sip_session_response_cb on_response,
  71. enum ast_sip_session_refresh_method method, int generate_new_sdp,
  72. struct ast_sip_session_media_state *pending_media_state,
  73. struct ast_sip_session_media_state *active_media_state,
  74. int queued);
  75. /*! \brief NAT hook for modifying outgoing messages with SDP */
  76. static struct ast_sip_nat_hook *nat_hook;
  77. /*!
  78. * \brief Registered SDP stream handlers
  79. *
  80. * This container is keyed on stream types. Each
  81. * object in the container is a linked list of
  82. * handlers for the stream type.
  83. */
  84. static struct ao2_container *sdp_handlers;
  85. /*!
  86. * These are the objects in the sdp_handlers container
  87. */
  88. struct sdp_handler_list {
  89. /* The list of handlers to visit */
  90. AST_LIST_HEAD_NOLOCK(, ast_sip_session_sdp_handler) list;
  91. /* The handlers in this list handle streams of this type */
  92. char stream_type[1];
  93. };
  94. static struct pjmedia_sdp_session *create_local_sdp(pjsip_inv_session *inv, struct ast_sip_session *session, const pjmedia_sdp_session *offer, const unsigned int ignore_active_stream_topology);
  95. static int sdp_handler_list_hash(const void *obj, int flags)
  96. {
  97. const struct sdp_handler_list *handler_list = obj;
  98. const char *stream_type = flags & OBJ_KEY ? obj : handler_list->stream_type;
  99. return ast_str_hash(stream_type);
  100. }
  101. const char *ast_sip_session_get_name(const struct ast_sip_session *session)
  102. {
  103. if (!session) {
  104. return "(null session)";
  105. }
  106. if (session->channel) {
  107. return ast_channel_name(session->channel);
  108. } else if (session->endpoint) {
  109. return ast_sorcery_object_get_id(session->endpoint);
  110. } else {
  111. return "unknown";
  112. }
  113. }
  114. int ast_sip_can_present_connected_id(const struct ast_sip_session *session, const struct ast_party_id *id)
  115. {
  116. return id->number.valid
  117. && (session->endpoint->id.trust_outbound
  118. || (ast_party_id_presentation(id) & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED);
  119. }
  120. static int sdp_handler_list_cmp(void *obj, void *arg, int flags)
  121. {
  122. struct sdp_handler_list *handler_list1 = obj;
  123. struct sdp_handler_list *handler_list2 = arg;
  124. const char *stream_type2 = flags & OBJ_KEY ? arg : handler_list2->stream_type;
  125. return strcmp(handler_list1->stream_type, stream_type2) ? 0 : CMP_MATCH | CMP_STOP;
  126. }
  127. int ast_sip_session_register_sdp_handler(struct ast_sip_session_sdp_handler *handler, const char *stream_type)
  128. {
  129. RAII_VAR(struct sdp_handler_list *, handler_list,
  130. ao2_find(sdp_handlers, stream_type, OBJ_KEY), ao2_cleanup);
  131. SCOPED_AO2LOCK(lock, sdp_handlers);
  132. if (handler_list) {
  133. struct ast_sip_session_sdp_handler *iter;
  134. /* Check if this handler is already registered for this stream type */
  135. AST_LIST_TRAVERSE(&handler_list->list, iter, next) {
  136. if (!strcmp(iter->id, handler->id)) {
  137. ast_log(LOG_WARNING, "Handler '%s' already registered for stream type '%s'.\n", handler->id, stream_type);
  138. return -1;
  139. }
  140. }
  141. AST_LIST_INSERT_TAIL(&handler_list->list, handler, next);
  142. ast_debug(1, "Registered SDP stream handler '%s' for stream type '%s'\n", handler->id, stream_type);
  143. return 0;
  144. }
  145. /* No stream of this type has been registered yet, so we need to create a new list */
  146. handler_list = ao2_alloc(sizeof(*handler_list) + strlen(stream_type), NULL);
  147. if (!handler_list) {
  148. return -1;
  149. }
  150. /* Safe use of strcpy */
  151. strcpy(handler_list->stream_type, stream_type);
  152. AST_LIST_HEAD_INIT_NOLOCK(&handler_list->list);
  153. AST_LIST_INSERT_TAIL(&handler_list->list, handler, next);
  154. if (!ao2_link(sdp_handlers, handler_list)) {
  155. return -1;
  156. }
  157. ast_debug(1, "Registered SDP stream handler '%s' for stream type '%s'\n", handler->id, stream_type);
  158. return 0;
  159. }
  160. static int remove_handler(void *obj, void *arg, void *data, int flags)
  161. {
  162. struct sdp_handler_list *handler_list = obj;
  163. struct ast_sip_session_sdp_handler *handler = data;
  164. struct ast_sip_session_sdp_handler *iter;
  165. const char *stream_type = arg;
  166. AST_LIST_TRAVERSE_SAFE_BEGIN(&handler_list->list, iter, next) {
  167. if (!strcmp(iter->id, handler->id)) {
  168. AST_LIST_REMOVE_CURRENT(next);
  169. ast_debug(1, "Unregistered SDP stream handler '%s' for stream type '%s'\n", handler->id, stream_type);
  170. }
  171. }
  172. AST_LIST_TRAVERSE_SAFE_END;
  173. if (AST_LIST_EMPTY(&handler_list->list)) {
  174. ast_debug(3, "No more handlers exist for stream type '%s'\n", stream_type);
  175. return CMP_MATCH;
  176. } else {
  177. return CMP_STOP;
  178. }
  179. }
  180. void ast_sip_session_unregister_sdp_handler(struct ast_sip_session_sdp_handler *handler, const char *stream_type)
  181. {
  182. ao2_callback_data(sdp_handlers, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA, remove_handler, (void *)stream_type, handler);
  183. }
  184. static int media_stats_local_ssrc_cmp(
  185. const struct ast_rtp_instance_stats *vec_elem, const struct ast_rtp_instance_stats *srch)
  186. {
  187. if (vec_elem->local_ssrc == srch->local_ssrc) {
  188. return 1;
  189. }
  190. return 0;
  191. }
  192. static struct ast_sip_session_media_state *internal_sip_session_media_state_alloc(
  193. size_t sessions, size_t read_callbacks)
  194. {
  195. struct ast_sip_session_media_state *media_state;
  196. media_state = ast_calloc(1, sizeof(*media_state));
  197. if (!media_state) {
  198. return NULL;
  199. }
  200. if (AST_VECTOR_INIT(&media_state->sessions, sessions) < 0) {
  201. ast_free(media_state);
  202. return NULL;
  203. }
  204. if (AST_VECTOR_INIT(&media_state->read_callbacks, read_callbacks) < 0) {
  205. AST_VECTOR_FREE(&media_state->sessions);
  206. ast_free(media_state);
  207. return NULL;
  208. }
  209. return media_state;
  210. }
  211. struct ast_sip_session_media_state *ast_sip_session_media_state_alloc(void)
  212. {
  213. return internal_sip_session_media_state_alloc(
  214. DEFAULT_NUM_SESSION_MEDIA, DEFAULT_NUM_SESSION_MEDIA);
  215. }
  216. void ast_sip_session_media_stats_save(struct ast_sip_session *sip_session, struct ast_sip_session_media_state *media_state)
  217. {
  218. int i;
  219. int ret;
  220. if (!media_state || !sip_session) {
  221. return;
  222. }
  223. for (i = 0; i < AST_VECTOR_SIZE(&media_state->sessions); i++) {
  224. struct ast_rtp_instance_stats *stats_tmp = NULL;
  225. struct ast_sip_session_media *media = AST_VECTOR_GET(&media_state->sessions, i);
  226. if (!media || !media->rtp) {
  227. continue;
  228. }
  229. stats_tmp = ast_calloc(1, sizeof(struct ast_rtp_instance_stats));
  230. if (!stats_tmp) {
  231. return;
  232. }
  233. ret = ast_rtp_instance_get_stats(media->rtp, stats_tmp, AST_RTP_INSTANCE_STAT_ALL);
  234. if (ret) {
  235. ast_free(stats_tmp);
  236. continue;
  237. }
  238. /* remove all the duplicated stats if exist */
  239. AST_VECTOR_REMOVE_CMP_UNORDERED(&sip_session->media_stats, stats_tmp, media_stats_local_ssrc_cmp, ast_free);
  240. AST_VECTOR_APPEND(&sip_session->media_stats, stats_tmp);
  241. }
  242. }
  243. void ast_sip_session_media_state_reset(struct ast_sip_session_media_state *media_state)
  244. {
  245. int index;
  246. if (!media_state) {
  247. return;
  248. }
  249. AST_VECTOR_RESET(&media_state->sessions, ao2_cleanup);
  250. AST_VECTOR_RESET(&media_state->read_callbacks, AST_VECTOR_ELEM_CLEANUP_NOOP);
  251. for (index = 0; index < AST_MEDIA_TYPE_END; ++index) {
  252. media_state->default_session[index] = NULL;
  253. }
  254. ast_stream_topology_free(media_state->topology);
  255. media_state->topology = NULL;
  256. }
  257. struct ast_sip_session_media_state *ast_sip_session_media_state_clone(const struct ast_sip_session_media_state *media_state)
  258. {
  259. struct ast_sip_session_media_state *cloned;
  260. int index;
  261. if (!media_state) {
  262. return NULL;
  263. }
  264. cloned = internal_sip_session_media_state_alloc(
  265. AST_VECTOR_SIZE(&media_state->sessions),
  266. AST_VECTOR_SIZE(&media_state->read_callbacks));
  267. if (!cloned) {
  268. return NULL;
  269. }
  270. if (media_state->topology) {
  271. cloned->topology = ast_stream_topology_clone(media_state->topology);
  272. if (!cloned->topology) {
  273. ast_sip_session_media_state_free(cloned);
  274. return NULL;
  275. }
  276. }
  277. for (index = 0; index < AST_VECTOR_SIZE(&media_state->sessions); ++index) {
  278. struct ast_sip_session_media *session_media = AST_VECTOR_GET(&media_state->sessions, index);
  279. enum ast_media_type type = ast_stream_get_type(ast_stream_topology_get_stream(cloned->topology, index));
  280. ao2_bump(session_media);
  281. if (AST_VECTOR_REPLACE(&cloned->sessions, index, session_media)) {
  282. ao2_cleanup(session_media);
  283. }
  284. if (ast_stream_get_state(ast_stream_topology_get_stream(cloned->topology, index)) != AST_STREAM_STATE_REMOVED &&
  285. !cloned->default_session[type]) {
  286. cloned->default_session[type] = session_media;
  287. }
  288. }
  289. for (index = 0; index < AST_VECTOR_SIZE(&media_state->read_callbacks); ++index) {
  290. struct ast_sip_session_media_read_callback_state *read_callback = AST_VECTOR_GET_ADDR(&media_state->read_callbacks, index);
  291. AST_VECTOR_REPLACE(&cloned->read_callbacks, index, *read_callback);
  292. }
  293. return cloned;
  294. }
  295. void ast_sip_session_media_state_free(struct ast_sip_session_media_state *media_state)
  296. {
  297. if (!media_state) {
  298. return;
  299. }
  300. /* This will reset the internal state so we only have to free persistent things */
  301. ast_sip_session_media_state_reset(media_state);
  302. AST_VECTOR_FREE(&media_state->sessions);
  303. AST_VECTOR_FREE(&media_state->read_callbacks);
  304. ast_free(media_state);
  305. }
  306. int ast_sip_session_is_pending_stream_default(const struct ast_sip_session *session, const struct ast_stream *stream)
  307. {
  308. int index;
  309. if (!session->pending_media_state->topology) {
  310. ast_log(LOG_WARNING, "Pending topology was NULL for channel '%s'\n",
  311. session->channel ? ast_channel_name(session->channel) : "unknown");
  312. return 0;
  313. }
  314. if (ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED) {
  315. return 0;
  316. }
  317. for (index = 0; index < ast_stream_topology_get_count(session->pending_media_state->topology); ++index) {
  318. if (ast_stream_get_type(ast_stream_topology_get_stream(session->pending_media_state->topology, index)) !=
  319. ast_stream_get_type(stream)) {
  320. continue;
  321. }
  322. return ast_stream_topology_get_stream(session->pending_media_state->topology, index) == stream ? 1 : 0;
  323. }
  324. return 0;
  325. }
  326. int ast_sip_session_media_add_read_callback(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
  327. int fd, ast_sip_session_media_read_cb callback)
  328. {
  329. struct ast_sip_session_media_read_callback_state callback_state = {
  330. .fd = fd,
  331. .read_callback = callback,
  332. .session = session_media,
  333. };
  334. /* The contents of the vector are whole structs and not pointers */
  335. return AST_VECTOR_APPEND(&session->pending_media_state->read_callbacks, callback_state);
  336. }
  337. int ast_sip_session_media_set_write_callback(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
  338. ast_sip_session_media_write_cb callback)
  339. {
  340. if (session_media->write_callback) {
  341. if (session_media->write_callback == callback) {
  342. return 0;
  343. }
  344. return -1;
  345. }
  346. session_media->write_callback = callback;
  347. return 0;
  348. }
  349. struct ast_sip_session_media *ast_sip_session_media_get_transport(struct ast_sip_session *session, struct ast_sip_session_media *session_media)
  350. {
  351. int index;
  352. if (!session->endpoint->media.bundle || ast_strlen_zero(session_media->mid)) {
  353. return session_media;
  354. }
  355. for (index = 0; index < AST_VECTOR_SIZE(&session->pending_media_state->sessions); ++index) {
  356. struct ast_sip_session_media *bundle_group_session_media;
  357. bundle_group_session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, index);
  358. /* The first session which is in the bundle group is considered the authoritative session for transport */
  359. if (bundle_group_session_media->bundle_group == session_media->bundle_group) {
  360. return bundle_group_session_media;
  361. }
  362. }
  363. return session_media;
  364. }
  365. /*!
  366. * \brief Set an SDP stream handler for a corresponding session media.
  367. *
  368. * \note Always use this function to set the SDP handler for a session media.
  369. *
  370. * This function will properly free resources on the SDP handler currently being
  371. * used by the session media, then set the session media to use the new SDP
  372. * handler.
  373. */
  374. static void session_media_set_handler(struct ast_sip_session_media *session_media,
  375. struct ast_sip_session_sdp_handler *handler)
  376. {
  377. ast_assert(session_media->handler != handler);
  378. if (session_media->handler) {
  379. session_media->handler->stream_destroy(session_media);
  380. }
  381. session_media->handler = handler;
  382. }
  383. static int stream_destroy(void *obj, void *arg, int flags)
  384. {
  385. struct sdp_handler_list *handler_list = obj;
  386. struct ast_sip_session_media *session_media = arg;
  387. struct ast_sip_session_sdp_handler *handler;
  388. AST_LIST_TRAVERSE(&handler_list->list, handler, next) {
  389. handler->stream_destroy(session_media);
  390. }
  391. return 0;
  392. }
  393. static void session_media_dtor(void *obj)
  394. {
  395. struct ast_sip_session_media *session_media = obj;
  396. /* It is possible for multiple handlers to have allocated memory on the
  397. * session media (usually through a stream changing types). Therefore, we
  398. * traverse all the SDP handlers and let them all call stream_destroy on
  399. * the session_media
  400. */
  401. ao2_callback(sdp_handlers, 0, stream_destroy, session_media);
  402. if (session_media->srtp) {
  403. ast_sdp_srtp_destroy(session_media->srtp);
  404. }
  405. ast_free(session_media->mid);
  406. ast_free(session_media->remote_mslabel);
  407. ast_free(session_media->remote_label);
  408. ast_free(session_media->stream_name);
  409. }
  410. struct ast_sip_session_media *ast_sip_session_media_state_add(struct ast_sip_session *session,
  411. struct ast_sip_session_media_state *media_state, enum ast_media_type type, int position)
  412. {
  413. struct ast_sip_session_media *session_media = NULL;
  414. struct ast_sip_session_media *current_session_media = NULL;
  415. SCOPE_ENTER(1, "%s Adding position %d\n", ast_sip_session_get_name(session), position);
  416. /* It is possible for this media state to already contain a session for the stream. If this
  417. * is the case we simply return it.
  418. */
  419. if (position < AST_VECTOR_SIZE(&media_state->sessions)) {
  420. current_session_media = AST_VECTOR_GET(&media_state->sessions, position);
  421. if (current_session_media && current_session_media->type == type) {
  422. SCOPE_EXIT_RTN_VALUE(current_session_media, "Using existing media_session\n");
  423. }
  424. }
  425. /* Determine if we can reuse the session media from the active media state if present */
  426. if (position < AST_VECTOR_SIZE(&session->active_media_state->sessions)) {
  427. session_media = AST_VECTOR_GET(&session->active_media_state->sessions, position);
  428. /* A stream can never exist without an accompanying media session */
  429. if (session_media->type == type) {
  430. ao2_ref(session_media, +1);
  431. ast_trace(1, "Reusing existing media session\n");
  432. /*
  433. * If this session_media was previously removed, its bundle group was probably reset
  434. * to -1 so if bundling is enabled on the endpoint, we need to reset it to 0, set
  435. * the bundled flag and reset its mid.
  436. */
  437. if (session->endpoint->media.bundle && session_media->bundle_group == -1) {
  438. session_media->bundled = session->endpoint->media.webrtc;
  439. session_media->bundle_group = 0;
  440. ast_free(session_media->mid);
  441. if (ast_asprintf(&session_media->mid, "%s-%d", ast_codec_media_type2str(type), position) < 0) {
  442. ao2_ref(session_media, -1);
  443. SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't alloc mid\n");
  444. }
  445. }
  446. } else {
  447. ast_trace(1, "Can't reuse existing media session because the types are different. %s <> %s\n",
  448. ast_codec_media_type2str(type), ast_codec_media_type2str(session_media->type));
  449. session_media = NULL;
  450. }
  451. }
  452. if (!session_media) {
  453. /* No existing media session we can use so create a new one */
  454. session_media = ao2_alloc_options(sizeof(*session_media), session_media_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK);
  455. if (!session_media) {
  456. return NULL;
  457. }
  458. ast_trace(1, "Creating new media session\n");
  459. session_media->encryption = session->endpoint->media.rtp.encryption;
  460. session_media->remote_ice = session->endpoint->media.rtp.ice_support;
  461. session_media->remote_rtcp_mux = session->endpoint->media.rtcp_mux;
  462. session_media->keepalive_sched_id = -1;
  463. session_media->timeout_sched_id = -1;
  464. session_media->type = type;
  465. session_media->stream_num = position;
  466. if (session->endpoint->media.bundle) {
  467. /* This is a new stream so create a new mid based on media type and position, which makes it unique.
  468. * If this is the result of an offer the mid will just end up getting replaced.
  469. */
  470. if (ast_asprintf(&session_media->mid, "%s-%d", ast_codec_media_type2str(type), position) < 0) {
  471. ao2_ref(session_media, -1);
  472. SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't alloc mid\n");
  473. }
  474. session_media->bundle_group = 0;
  475. /* Some WebRTC clients can't handle an offer to bundle media streams. Instead they expect them to
  476. * already be bundled. Every client handles this scenario though so if WebRTC is enabled just go
  477. * ahead and treat the streams as having already been bundled.
  478. */
  479. session_media->bundled = session->endpoint->media.webrtc;
  480. } else {
  481. session_media->bundle_group = -1;
  482. }
  483. }
  484. ast_free(session_media->stream_name);
  485. session_media->stream_name = ast_strdup(ast_stream_get_name(ast_stream_topology_get_stream(media_state->topology, position)));
  486. if (AST_VECTOR_REPLACE(&media_state->sessions, position, session_media)) {
  487. ao2_ref(session_media, -1);
  488. SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't replace media_session\n");
  489. }
  490. ao2_cleanup(current_session_media);
  491. /* If this stream will be active in some way and it is the first of this type then consider this the default media session to match */
  492. if (!media_state->default_session[type] && ast_stream_get_state(ast_stream_topology_get_stream(media_state->topology, position)) != AST_STREAM_STATE_REMOVED) {
  493. ast_trace(1, "Setting media session as default for %s\n", ast_codec_media_type2str(session_media->type));
  494. media_state->default_session[type] = session_media;
  495. }
  496. SCOPE_EXIT_RTN_VALUE(session_media, "Done\n");
  497. }
  498. static int is_stream_limitation_reached(enum ast_media_type type, const struct ast_sip_endpoint *endpoint, int *type_streams)
  499. {
  500. switch (type) {
  501. case AST_MEDIA_TYPE_AUDIO:
  502. return !(type_streams[type] < endpoint->media.max_audio_streams);
  503. case AST_MEDIA_TYPE_VIDEO:
  504. return !(type_streams[type] < endpoint->media.max_video_streams);
  505. case AST_MEDIA_TYPE_IMAGE:
  506. /* We don't have an option for image (T.38) streams so cap it to one. */
  507. return (type_streams[type] > 0);
  508. case AST_MEDIA_TYPE_UNKNOWN:
  509. case AST_MEDIA_TYPE_TEXT:
  510. default:
  511. /* We don't want any unknown or "other" streams on our endpoint,
  512. * so always just say we've reached the limit
  513. */
  514. return 1;
  515. }
  516. }
  517. static int get_mid_bundle_group(const pjmedia_sdp_session *sdp, const char *mid)
  518. {
  519. int bundle_group = 0;
  520. int index;
  521. for (index = 0; index < sdp->attr_count; ++index) {
  522. pjmedia_sdp_attr *attr = sdp->attr[index];
  523. char value[pj_strlen(&attr->value) + 1], *mids = value, *attr_mid;
  524. if (pj_strcmp2(&attr->name, "group") || pj_strncmp2(&attr->value, "BUNDLE", 6)) {
  525. continue;
  526. }
  527. ast_copy_pj_str(value, &attr->value, sizeof(value));
  528. /* Skip the BUNDLE at the front */
  529. mids += 7;
  530. while ((attr_mid = strsep(&mids, " "))) {
  531. if (!strcmp(attr_mid, mid)) {
  532. /* The ordering of attributes determines our internal identification of the bundle group based on number,
  533. * with -1 being not in a bundle group. Since this is only exposed internally for response purposes it's
  534. * actually even fine if things move around.
  535. */
  536. return bundle_group;
  537. }
  538. }
  539. bundle_group++;
  540. }
  541. return -1;
  542. }
  543. static int set_mid_and_bundle_group(struct ast_sip_session *session,
  544. struct ast_sip_session_media *session_media,
  545. const pjmedia_sdp_session *sdp,
  546. const struct pjmedia_sdp_media *stream)
  547. {
  548. pjmedia_sdp_attr *attr;
  549. if (!session->endpoint->media.bundle) {
  550. return 0;
  551. }
  552. /* By default on an incoming negotiation we assume no mid and bundle group is present */
  553. ast_free(session_media->mid);
  554. session_media->mid = NULL;
  555. session_media->bundle_group = -1;
  556. session_media->bundled = 0;
  557. /* Grab the media identifier for the stream */
  558. attr = pjmedia_sdp_media_find_attr2(stream, "mid", NULL);
  559. if (!attr) {
  560. return 0;
  561. }
  562. session_media->mid = ast_calloc(1, attr->value.slen + 1);
  563. if (!session_media->mid) {
  564. return 0;
  565. }
  566. ast_copy_pj_str(session_media->mid, &attr->value, attr->value.slen + 1);
  567. /* Determine what bundle group this is part of */
  568. session_media->bundle_group = get_mid_bundle_group(sdp, session_media->mid);
  569. /* If this is actually part of a bundle group then the other side requested or accepted the bundle request */
  570. session_media->bundled = session_media->bundle_group != -1;
  571. return 0;
  572. }
  573. static void set_remote_mslabel_and_stream_group(struct ast_sip_session *session,
  574. struct ast_sip_session_media *session_media,
  575. const pjmedia_sdp_session *sdp,
  576. const struct pjmedia_sdp_media *stream,
  577. struct ast_stream *asterisk_stream)
  578. {
  579. int index;
  580. ast_free(session_media->remote_mslabel);
  581. session_media->remote_mslabel = NULL;
  582. ast_free(session_media->remote_label);
  583. session_media->remote_label = NULL;
  584. for (index = 0; index < stream->attr_count; ++index) {
  585. pjmedia_sdp_attr *attr = stream->attr[index];
  586. char attr_value[pj_strlen(&attr->value) + 1];
  587. char *ssrc_attribute_name, *ssrc_attribute_value = NULL;
  588. char *msid, *tmp = attr_value;
  589. static const pj_str_t STR_msid = { "msid", 4 };
  590. static const pj_str_t STR_ssrc = { "ssrc", 4 };
  591. static const pj_str_t STR_label = { "label", 5 };
  592. if (!pj_strcmp(&attr->name, &STR_label)) {
  593. ast_copy_pj_str(attr_value, &attr->value, sizeof(attr_value));
  594. session_media->remote_label = ast_strdup(attr_value);
  595. } else if (!pj_strcmp(&attr->name, &STR_msid)) {
  596. ast_copy_pj_str(attr_value, &attr->value, sizeof(attr_value));
  597. msid = strsep(&tmp, " ");
  598. session_media->remote_mslabel = ast_strdup(msid);
  599. break;
  600. } else if (!pj_strcmp(&attr->name, &STR_ssrc)) {
  601. ast_copy_pj_str(attr_value, &attr->value, sizeof(attr_value));
  602. if ((ssrc_attribute_name = strchr(attr_value, ' '))) {
  603. /* This has an actual attribute */
  604. *ssrc_attribute_name++ = '\0';
  605. ssrc_attribute_value = strchr(ssrc_attribute_name, ':');
  606. if (ssrc_attribute_value) {
  607. /* Values are actually optional according to the spec */
  608. *ssrc_attribute_value++ = '\0';
  609. }
  610. if (!strcasecmp(ssrc_attribute_name, "mslabel") && !ast_strlen_zero(ssrc_attribute_value)) {
  611. session_media->remote_mslabel = ast_strdup(ssrc_attribute_value);
  612. break;
  613. }
  614. }
  615. }
  616. }
  617. if (ast_strlen_zero(session_media->remote_mslabel)) {
  618. return;
  619. }
  620. /* Iterate through the existing streams looking for a match and if so then group this with it */
  621. for (index = 0; index < AST_VECTOR_SIZE(&session->pending_media_state->sessions); ++index) {
  622. struct ast_sip_session_media *group_session_media;
  623. group_session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, index);
  624. if (ast_strlen_zero(group_session_media->remote_mslabel) ||
  625. strcmp(group_session_media->remote_mslabel, session_media->remote_mslabel)) {
  626. continue;
  627. }
  628. ast_stream_set_group(asterisk_stream, index);
  629. break;
  630. }
  631. }
  632. static void remove_stream_from_bundle(struct ast_sip_session_media *session_media,
  633. struct ast_stream *stream)
  634. {
  635. ast_stream_set_state(stream, AST_STREAM_STATE_REMOVED);
  636. ast_free(session_media->mid);
  637. session_media->mid = NULL;
  638. session_media->bundle_group = -1;
  639. session_media->bundled = 0;
  640. }
  641. static int handle_incoming_sdp(struct ast_sip_session *session, const pjmedia_sdp_session *sdp)
  642. {
  643. int i;
  644. int handled = 0;
  645. int type_streams[AST_MEDIA_TYPE_END] = {0};
  646. SCOPE_ENTER(3, "%s: Media count: %d\n", ast_sip_session_get_name(session), sdp->media_count);
  647. if (session->inv_session && session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
  648. SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Failed to handle incoming SDP. Session has been already disconnected\n",
  649. ast_sip_session_get_name(session));
  650. }
  651. /* It is possible for SDP deferral to have already created a pending topology */
  652. if (!session->pending_media_state->topology) {
  653. session->pending_media_state->topology = ast_stream_topology_alloc();
  654. if (!session->pending_media_state->topology) {
  655. SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Couldn't alloc pending topology\n",
  656. ast_sip_session_get_name(session));
  657. }
  658. }
  659. for (i = 0; i < sdp->media_count; ++i) {
  660. /* See if there are registered handlers for this media stream type */
  661. char media[20];
  662. struct ast_sip_session_sdp_handler *handler;
  663. RAII_VAR(struct sdp_handler_list *, handler_list, NULL, ao2_cleanup);
  664. struct ast_sip_session_media *session_media = NULL;
  665. int res;
  666. enum ast_media_type type;
  667. struct ast_stream *stream = NULL;
  668. pjmedia_sdp_media *remote_stream = sdp->media[i];
  669. SCOPE_ENTER(4, "%s: Processing stream %d\n", ast_sip_session_get_name(session), i);
  670. /* We need a null-terminated version of the media string */
  671. ast_copy_pj_str(media, &sdp->media[i]->desc.media, sizeof(media));
  672. type = ast_media_type_from_str(media);
  673. /* See if we have an already existing stream, which can occur from SDP deferral checking */
  674. if (i < ast_stream_topology_get_count(session->pending_media_state->topology)) {
  675. stream = ast_stream_topology_get_stream(session->pending_media_state->topology, i);
  676. ast_trace(-1, "%s: Using existing pending stream %s\n", ast_sip_session_get_name(session),
  677. ast_str_tmp(128, ast_stream_to_str(stream, &STR_TMP)));
  678. }
  679. if (!stream) {
  680. struct ast_stream *existing_stream = NULL;
  681. char *stream_name = NULL, *stream_name_allocated = NULL;
  682. const char *stream_label = NULL;
  683. if (session->active_media_state->topology &&
  684. (i < ast_stream_topology_get_count(session->active_media_state->topology))) {
  685. existing_stream = ast_stream_topology_get_stream(session->active_media_state->topology, i);
  686. ast_trace(-1, "%s: Found existing active stream %s\n", ast_sip_session_get_name(session),
  687. ast_str_tmp(128, ast_stream_to_str(existing_stream, &STR_TMP)));
  688. if (ast_stream_get_state(existing_stream) != AST_STREAM_STATE_REMOVED) {
  689. stream_name = (char *)ast_stream_get_name(existing_stream);
  690. stream_label = ast_stream_get_metadata(existing_stream, "SDP:LABEL");
  691. }
  692. }
  693. if (ast_strlen_zero(stream_name)) {
  694. if (ast_asprintf(&stream_name_allocated, "%s-%d", ast_codec_media_type2str(type), i) < 0) {
  695. handled = 0;
  696. SCOPE_EXIT_LOG_EXPR(goto end, LOG_ERROR, "%s: Couldn't alloc stream name\n",
  697. ast_sip_session_get_name(session));
  698. }
  699. stream_name = stream_name_allocated;
  700. ast_trace(-1, "%s: Using %s for new stream name\n", ast_sip_session_get_name(session),
  701. stream_name);
  702. }
  703. stream = ast_stream_alloc(stream_name, type);
  704. ast_free(stream_name_allocated);
  705. if (!stream) {
  706. handled = 0;
  707. SCOPE_EXIT_LOG_EXPR(goto end, LOG_ERROR, "%s: Couldn't alloc stream\n",
  708. ast_sip_session_get_name(session));
  709. }
  710. if (!ast_strlen_zero(stream_label)) {
  711. ast_stream_set_metadata(stream, "SDP:LABEL", stream_label);
  712. ast_trace(-1, "%s: Using %s for new stream label\n", ast_sip_session_get_name(session),
  713. stream_label);
  714. }
  715. if (ast_stream_topology_set_stream(session->pending_media_state->topology, i, stream)) {
  716. ast_stream_free(stream);
  717. handled = 0;
  718. SCOPE_EXIT_LOG_EXPR(goto end, LOG_ERROR, "%s: Couldn't set stream in topology\n",
  719. ast_sip_session_get_name(session));
  720. }
  721. /* For backwards compatibility with the core the default audio stream is always sendrecv */
  722. if (!ast_sip_session_is_pending_stream_default(session, stream) || strcmp(media, "audio")) {
  723. if (pjmedia_sdp_media_find_attr2(remote_stream, "sendonly", NULL)) {
  724. /* Stream state reflects our state of a stream, so in the case of
  725. * sendonly and recvonly we store the opposite since that is what ours
  726. * is.
  727. */
  728. ast_stream_set_state(stream, AST_STREAM_STATE_RECVONLY);
  729. } else if (pjmedia_sdp_media_find_attr2(remote_stream, "recvonly", NULL)) {
  730. ast_stream_set_state(stream, AST_STREAM_STATE_SENDONLY);
  731. } else if (pjmedia_sdp_media_find_attr2(remote_stream, "inactive", NULL)) {
  732. ast_stream_set_state(stream, AST_STREAM_STATE_INACTIVE);
  733. } else {
  734. ast_stream_set_state(stream, AST_STREAM_STATE_SENDRECV);
  735. }
  736. } else {
  737. ast_stream_set_state(stream, AST_STREAM_STATE_SENDRECV);
  738. }
  739. ast_trace(-1, "%s: Using new stream %s\n", ast_sip_session_get_name(session),
  740. ast_str_tmp(128, ast_stream_to_str(stream, &STR_TMP)));
  741. }
  742. session_media = ast_sip_session_media_state_add(session, session->pending_media_state, ast_media_type_from_str(media), i);
  743. if (!session_media) {
  744. SCOPE_EXIT_LOG_EXPR(goto end, LOG_ERROR, "%s: Couldn't alloc session media\n",
  745. ast_sip_session_get_name(session));
  746. }
  747. /* If this stream is already declined mark it as such, or mark it as such if we've reached the limit */
  748. if (!remote_stream->desc.port || is_stream_limitation_reached(type, session->endpoint, type_streams)) {
  749. remove_stream_from_bundle(session_media, stream);
  750. SCOPE_EXIT_EXPR(continue, "%s: Declining incoming SDP media stream %s'\n",
  751. ast_sip_session_get_name(session), ast_str_tmp(128, ast_stream_to_str(stream, &STR_TMP)));
  752. }
  753. set_mid_and_bundle_group(session, session_media, sdp, remote_stream);
  754. set_remote_mslabel_and_stream_group(session, session_media, sdp, remote_stream, stream);
  755. if (session_media->handler) {
  756. handler = session_media->handler;
  757. ast_trace(-1, "%s: Negotiating incoming SDP media stream %s using %s SDP handler\n",
  758. ast_sip_session_get_name(session), ast_str_tmp(128, ast_stream_to_str(stream, &STR_TMP)),
  759. session_media->handler->id);
  760. res = handler->negotiate_incoming_sdp_stream(session, session_media, sdp, i, stream);
  761. if (res < 0) {
  762. /* Catastrophic failure. Abort! */
  763. SCOPE_EXIT_LOG_EXPR(goto end, LOG_ERROR, "%s: Couldn't negotiate stream %s\n",
  764. ast_sip_session_get_name(session), ast_str_tmp(128, ast_stream_to_str(stream, &STR_TMP)));
  765. } else if (res == 0) {
  766. remove_stream_from_bundle(session_media, stream);
  767. SCOPE_EXIT_EXPR(continue, "%s: Declining incoming SDP media stream %s\n",
  768. ast_sip_session_get_name(session), ast_str_tmp(128, ast_stream_to_str(stream, &STR_TMP)));
  769. } else if (res > 0) {
  770. handled = 1;
  771. ++type_streams[type];
  772. /* Handled by this handler. Move to the next stream */
  773. SCOPE_EXIT_EXPR(continue, "%s: Media stream %s handled by %s\n",
  774. ast_sip_session_get_name(session), ast_str_tmp(128, ast_stream_to_str(stream, &STR_TMP)),
  775. session_media->handler->id);
  776. }
  777. }
  778. handler_list = ao2_find(sdp_handlers, media, OBJ_KEY);
  779. if (!handler_list) {
  780. SCOPE_EXIT_EXPR(continue, "%s: Media stream %s has no registered handlers\n",
  781. ast_sip_session_get_name(session), ast_str_tmp(128, ast_stream_to_str(stream, &STR_TMP)));
  782. }
  783. AST_LIST_TRAVERSE(&handler_list->list, handler, next) {
  784. if (handler == session_media->handler) {
  785. continue;
  786. }
  787. ast_trace(-1, "%s: Negotiating incoming SDP media stream %s using %s SDP handler\n",
  788. ast_sip_session_get_name(session), ast_str_tmp(128, ast_stream_to_str(stream, &STR_TMP)),
  789. handler->id);
  790. res = handler->negotiate_incoming_sdp_stream(session, session_media, sdp, i, stream);
  791. if (res < 0) {
  792. /* Catastrophic failure. Abort! */
  793. handled = 0;
  794. SCOPE_EXIT_LOG_EXPR(goto end, LOG_ERROR, "%s: Couldn't negotiate stream %s\n",
  795. ast_sip_session_get_name(session), ast_str_tmp(128, ast_stream_to_str(stream, &STR_TMP)));
  796. } else if (res == 0) {
  797. remove_stream_from_bundle(session_media, stream);
  798. ast_trace(-1, "%s: Declining incoming SDP media stream %s\n",
  799. ast_sip_session_get_name(session), ast_str_tmp(128, ast_stream_to_str(stream, &STR_TMP)));
  800. continue;
  801. } else if (res > 0) {
  802. session_media_set_handler(session_media, handler);
  803. handled = 1;
  804. ++type_streams[type];
  805. ast_trace(-1, "%s: Media stream %s handled by %s\n",
  806. ast_sip_session_get_name(session), ast_str_tmp(128, ast_stream_to_str(stream, &STR_TMP)),
  807. session_media->handler->id);
  808. break;
  809. }
  810. }
  811. SCOPE_EXIT("%s: Done with stream %s\n", ast_sip_session_get_name(session),
  812. ast_str_tmp(128, ast_stream_to_str(stream, &STR_TMP)));
  813. }
  814. end:
  815. SCOPE_EXIT_RTN_VALUE(handled ? 0 : -1, "%s: Handled? %s\n", ast_sip_session_get_name(session),
  816. handled ? "yes" : "no");
  817. }
  818. static int handle_negotiated_sdp_session_media(struct ast_sip_session_media *session_media,
  819. struct ast_sip_session *session, const pjmedia_sdp_session *local,
  820. const pjmedia_sdp_session *remote, int index, struct ast_stream *asterisk_stream)
  821. {
  822. /* See if there are registered handlers for this media stream type */
  823. struct pjmedia_sdp_media *local_stream = local->media[index];
  824. char media[20];
  825. struct ast_sip_session_sdp_handler *handler;
  826. RAII_VAR(struct sdp_handler_list *, handler_list, NULL, ao2_cleanup);
  827. int res;
  828. SCOPE_ENTER(1, "%s\n", session ? ast_sip_session_get_name(session) : "unknown");
  829. /* We need a null-terminated version of the media string */
  830. ast_copy_pj_str(media, &local->media[index]->desc.media, sizeof(media));
  831. /* For backwards compatibility we only reflect the stream state correctly on
  832. * the non-default streams and any non-audio streams. This is because the stream
  833. * state of the default audio stream is also used for signaling that someone has
  834. * placed us on hold. This situation is not handled currently and can result in
  835. * the remote side being sorted of placed on hold too.
  836. */
  837. if (!ast_sip_session_is_pending_stream_default(session, asterisk_stream) || strcmp(media, "audio")) {
  838. /* Determine the state of the stream based on our local SDP */
  839. if (pjmedia_sdp_media_find_attr2(local_stream, "sendonly", NULL)) {
  840. ast_stream_set_state(asterisk_stream, AST_STREAM_STATE_SENDONLY);
  841. } else if (pjmedia_sdp_media_find_attr2(local_stream, "recvonly", NULL)) {
  842. ast_stream_set_state(asterisk_stream, AST_STREAM_STATE_RECVONLY);
  843. } else if (pjmedia_sdp_media_find_attr2(local_stream, "inactive", NULL)) {
  844. ast_stream_set_state(asterisk_stream, AST_STREAM_STATE_INACTIVE);
  845. } else {
  846. ast_stream_set_state(asterisk_stream, AST_STREAM_STATE_SENDRECV);
  847. }
  848. } else {
  849. ast_stream_set_state(asterisk_stream, AST_STREAM_STATE_SENDRECV);
  850. }
  851. set_mid_and_bundle_group(session, session_media, remote, remote->media[index]);
  852. set_remote_mslabel_and_stream_group(session, session_media, remote, remote->media[index], asterisk_stream);
  853. handler = session_media->handler;
  854. if (handler) {
  855. ast_debug(4, "%s: Applying negotiated SDP media stream '%s' using %s SDP handler\n",
  856. ast_sip_session_get_name(session), ast_codec_media_type2str(session_media->type),
  857. handler->id);
  858. res = handler->apply_negotiated_sdp_stream(session, session_media, local, remote, index, asterisk_stream);
  859. if (res >= 0) {
  860. ast_debug(4, "%s: Applied negotiated SDP media stream '%s' using %s SDP handler\n",
  861. ast_sip_session_get_name(session), ast_codec_media_type2str(session_media->type),
  862. handler->id);
  863. SCOPE_EXIT_RTN_VALUE(0, "%s: Applied negotiated SDP media stream '%s' using %s SDP handler\n",
  864. ast_sip_session_get_name(session), ast_codec_media_type2str(session_media->type),
  865. handler->id);
  866. }
  867. SCOPE_EXIT_RTN_VALUE(-1, "%s: Failed to apply negotiated SDP media stream '%s' using %s SDP handler\n",
  868. ast_sip_session_get_name(session), ast_codec_media_type2str(session_media->type),
  869. handler->id);
  870. }
  871. handler_list = ao2_find(sdp_handlers, media, OBJ_KEY);
  872. if (!handler_list) {
  873. ast_debug(4, "%s: No registered SDP handlers for media type '%s'\n", ast_sip_session_get_name(session), media);
  874. return -1;
  875. }
  876. AST_LIST_TRAVERSE(&handler_list->list, handler, next) {
  877. if (handler == session_media->handler) {
  878. continue;
  879. }
  880. ast_debug(4, "%s: Applying negotiated SDP media stream '%s' using %s SDP handler\n",
  881. ast_sip_session_get_name(session), ast_codec_media_type2str(session_media->type),
  882. handler->id);
  883. res = handler->apply_negotiated_sdp_stream(session, session_media, local, remote, index, asterisk_stream);
  884. if (res < 0) {
  885. /* Catastrophic failure. Abort! */
  886. SCOPE_EXIT_RTN_VALUE(-1, "%s: Handler '%s' returned %d\n",
  887. ast_sip_session_get_name(session), handler->id, res);
  888. }
  889. if (res > 0) {
  890. ast_debug(4, "%s: Applied negotiated SDP media stream '%s' using %s SDP handler\n",
  891. ast_sip_session_get_name(session), ast_codec_media_type2str(session_media->type),
  892. handler->id);
  893. /* Handled by this handler. Move to the next stream */
  894. session_media_set_handler(session_media, handler);
  895. SCOPE_EXIT_RTN_VALUE(0, "%s: Handler '%s' handled this sdp stream\n",
  896. ast_sip_session_get_name(session), handler->id);
  897. }
  898. }
  899. res = 0;
  900. if (session_media->handler && session_media->handler->stream_stop) {
  901. ast_debug(4, "%s: Stopping SDP media stream '%s' as it is not currently negotiated\n",
  902. ast_sip_session_get_name(session), ast_codec_media_type2str(session_media->type));
  903. session_media->handler->stream_stop(session_media);
  904. }
  905. SCOPE_EXIT_RTN_VALUE(0, "%s: Media type '%s' %s\n",
  906. ast_sip_session_get_name(session), ast_codec_media_type2str(session_media->type),
  907. res ? "not negotiated. Stopped" : "handled");
  908. }
  909. static int handle_negotiated_sdp(struct ast_sip_session *session, const pjmedia_sdp_session *local, const pjmedia_sdp_session *remote)
  910. {
  911. int i;
  912. struct ast_stream_topology *topology;
  913. unsigned int changed = 0; /* 0 = unchanged, 1 = new source, 2 = new topology */
  914. SCOPE_ENTER(1, "%s\n", ast_sip_session_get_name(session));
  915. if (!session->pending_media_state->topology) {
  916. if (session->active_media_state->topology) {
  917. /*
  918. * This happens when we have negotiated media after receiving a 183,
  919. * and we're now receiving a 200 with a new SDP. In this case, there
  920. * is active_media_state, but the pending_media_state has been reset.
  921. */
  922. struct ast_sip_session_media_state *active_media_state_clone;
  923. active_media_state_clone =
  924. ast_sip_session_media_state_clone(session->active_media_state);
  925. if (!active_media_state_clone) {
  926. ast_log(LOG_WARNING, "%s: Unable to clone active media state\n",
  927. ast_sip_session_get_name(session));
  928. return -1;
  929. }
  930. ast_sip_session_media_state_free(session->pending_media_state);
  931. session->pending_media_state = active_media_state_clone;
  932. } else {
  933. ast_log(LOG_WARNING, "%s: No pending or active media state\n",
  934. ast_sip_session_get_name(session));
  935. return -1;
  936. }
  937. }
  938. /* If we're handling negotiated streams, then we should already have set
  939. * up session media instances (and Asterisk streams) that correspond to
  940. * the local SDP, and there should be the same number of session medias
  941. * and streams as there are local SDP streams
  942. */
  943. if (ast_stream_topology_get_count(session->pending_media_state->topology) != local->media_count
  944. || AST_VECTOR_SIZE(&session->pending_media_state->sessions) != local->media_count) {
  945. ast_log(LOG_WARNING, "%s: Local SDP contains %d media streams while we expected it to contain %u\n",
  946. ast_sip_session_get_name(session),
  947. ast_stream_topology_get_count(session->pending_media_state->topology), local->media_count);
  948. SCOPE_EXIT_RTN_VALUE(-1, "Media stream count mismatch\n");
  949. }
  950. AST_VECTOR_RESET(&session->pending_media_state->read_callbacks, AST_VECTOR_ELEM_CLEANUP_NOOP);
  951. for (i = 0; i < local->media_count; ++i) {
  952. struct ast_sip_session_media *session_media;
  953. struct ast_stream *stream;
  954. if (!remote->media[i]) {
  955. continue;
  956. }
  957. session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, i);
  958. stream = ast_stream_topology_get_stream(session->pending_media_state->topology, i);
  959. /* Make sure that this stream is in the correct state. If we need to change
  960. * the state to REMOVED, then our work here is done, so go ahead and move on
  961. * to the next stream.
  962. */
  963. if (!remote->media[i]->desc.port) {
  964. ast_stream_set_state(stream, AST_STREAM_STATE_REMOVED);
  965. continue;
  966. }
  967. /* If the stream state is REMOVED, nothing needs to be done, so move on to the
  968. * next stream. This can occur if an internal thing has requested it to be
  969. * removed, or if we remove it as a result of the stream limit being reached.
  970. */
  971. if (ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED) {
  972. /*
  973. * Defer removing the handler until we are ready to activate
  974. * the new topology. The channel's thread may still be using
  975. * the stream and we could crash before we are ready.
  976. */
  977. continue;
  978. }
  979. if (handle_negotiated_sdp_session_media(session_media, session, local, remote, i, stream)) {
  980. SCOPE_EXIT_RTN_VALUE(-1, "Unable to handle negotiated session media\n");
  981. }
  982. changed |= session_media->changed;
  983. session_media->changed = 0;
  984. }
  985. /* Apply the pending media state to the channel and make it active */
  986. ast_channel_lock(session->channel);
  987. /* Now update the stream handler for any declined/removed streams */
  988. for (i = 0; i < local->media_count; ++i) {
  989. struct ast_sip_session_media *session_media;
  990. struct ast_stream *stream;
  991. if (!remote->media[i]) {
  992. continue;
  993. }
  994. session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, i);
  995. stream = ast_stream_topology_get_stream(session->pending_media_state->topology, i);
  996. if (ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED
  997. && session_media->handler) {
  998. /*
  999. * This stream is no longer being used and the channel's thread
  1000. * is held off because we have the channel lock so release any
  1001. * resources the handler may have on it.
  1002. */
  1003. session_media_set_handler(session_media, NULL);
  1004. }
  1005. }
  1006. /* Update the topology on the channel to match the accepted one */
  1007. topology = ast_stream_topology_clone(session->pending_media_state->topology);
  1008. if (topology) {
  1009. ast_channel_set_stream_topology(session->channel, topology);
  1010. /* If this is a remotely done renegotiation that has changed the stream topology notify what is
  1011. * currently handling this channel. Note that fax uses its own process, so if we are transitioning
  1012. * between audio and fax or vice versa we don't notify.
  1013. */
  1014. if (pjmedia_sdp_neg_was_answer_remote(session->inv_session->neg) == PJ_FALSE &&
  1015. session->active_media_state && session->active_media_state->topology &&
  1016. !ast_stream_topology_equal(session->active_media_state->topology, topology) &&
  1017. !session->active_media_state->default_session[AST_MEDIA_TYPE_IMAGE] &&
  1018. !session->pending_media_state->default_session[AST_MEDIA_TYPE_IMAGE]) {
  1019. changed = 2;
  1020. }
  1021. }
  1022. /* Remove all current file descriptors from the channel */
  1023. for (i = 0; i < AST_VECTOR_SIZE(&session->active_media_state->read_callbacks); ++i) {
  1024. ast_channel_internal_fd_clear(session->channel, i + AST_EXTENDED_FDS);
  1025. }
  1026. /* Add all the file descriptors from the pending media state */
  1027. for (i = 0; i < AST_VECTOR_SIZE(&session->pending_media_state->read_callbacks); ++i) {
  1028. struct ast_sip_session_media_read_callback_state *callback_state;
  1029. callback_state = AST_VECTOR_GET_ADDR(&session->pending_media_state->read_callbacks, i);
  1030. ast_channel_internal_fd_set(session->channel, i + AST_EXTENDED_FDS, callback_state->fd);
  1031. }
  1032. /* Active and pending flip flop as needed */
  1033. ast_sip_session_media_stats_save(session, session->active_media_state);
  1034. SWAP(session->active_media_state, session->pending_media_state);
  1035. ast_sip_session_media_state_reset(session->pending_media_state);
  1036. ast_channel_unlock(session->channel);
  1037. if (changed == 1) {
  1038. struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_STREAM_TOPOLOGY_SOURCE_CHANGED };
  1039. ast_queue_frame(session->channel, &f);
  1040. } else if (changed == 2) {
  1041. ast_channel_stream_topology_changed_externally(session->channel);
  1042. } else {
  1043. ast_queue_frame(session->channel, &ast_null_frame);
  1044. }
  1045. SCOPE_EXIT_RTN_VALUE(0);
  1046. }
  1047. #define DATASTORE_BUCKETS 53
  1048. #define MEDIA_BUCKETS 7
  1049. static void session_datastore_destroy(void *obj)
  1050. {
  1051. struct ast_datastore *datastore = obj;
  1052. /* Using the destroy function (if present) destroy the data */
  1053. if (datastore->info->destroy != NULL && datastore->data != NULL) {
  1054. datastore->info->destroy(datastore->data);
  1055. datastore->data = NULL;
  1056. }
  1057. ast_free((void *) datastore->uid);
  1058. datastore->uid = NULL;
  1059. }
  1060. struct ast_datastore *ast_sip_session_alloc_datastore(const struct ast_datastore_info *info, const char *uid)
  1061. {
  1062. RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
  1063. char uuid_buf[AST_UUID_STR_LEN];
  1064. const char *uid_ptr = uid;
  1065. if (!info) {
  1066. return NULL;
  1067. }
  1068. datastore = ao2_alloc(sizeof(*datastore), session_datastore_destroy);
  1069. if (!datastore) {
  1070. return NULL;
  1071. }
  1072. datastore->info = info;
  1073. if (ast_strlen_zero(uid)) {
  1074. /* They didn't provide an ID so we'll provide one ourself */
  1075. uid_ptr = ast_uuid_generate_str(uuid_buf, sizeof(uuid_buf));
  1076. }
  1077. datastore->uid = ast_strdup(uid_ptr);
  1078. if (!datastore->uid) {
  1079. return NULL;
  1080. }
  1081. ao2_ref(datastore, +1);
  1082. return datastore;
  1083. }
  1084. int ast_sip_session_add_datastore(struct ast_sip_session *session, struct ast_datastore *datastore)
  1085. {
  1086. ast_assert(datastore != NULL);
  1087. ast_assert(datastore->info != NULL);
  1088. ast_assert(ast_strlen_zero(datastore->uid) == 0);
  1089. if (!ao2_link(session->datastores, datastore)) {
  1090. return -1;
  1091. }
  1092. return 0;
  1093. }
  1094. struct ast_datastore *ast_sip_session_get_datastore(struct ast_sip_session *session, const char *name)
  1095. {
  1096. return ao2_find(session->datastores, name, OBJ_KEY);
  1097. }
  1098. void ast_sip_session_remove_datastore(struct ast_sip_session *session, const char *name)
  1099. {
  1100. ao2_callback(session->datastores, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA, NULL, (void *) name);
  1101. }
  1102. enum delayed_method {
  1103. DELAYED_METHOD_INVITE,
  1104. DELAYED_METHOD_UPDATE,
  1105. DELAYED_METHOD_BYE,
  1106. };
  1107. /*!
  1108. * \internal
  1109. * \brief Convert delayed method enum value to a string.
  1110. * \since 13.3.0
  1111. *
  1112. * \param method Delayed method enum value to convert to a string.
  1113. *
  1114. * \return String value of delayed method.
  1115. */
  1116. static const char *delayed_method2str(enum delayed_method method)
  1117. {
  1118. const char *str = "<unknown>";
  1119. switch (method) {
  1120. case DELAYED_METHOD_INVITE:
  1121. str = "INVITE";
  1122. break;
  1123. case DELAYED_METHOD_UPDATE:
  1124. str = "UPDATE";
  1125. break;
  1126. case DELAYED_METHOD_BYE:
  1127. str = "BYE";
  1128. break;
  1129. }
  1130. return str;
  1131. }
  1132. /*!
  1133. * \brief Structure used for sending delayed requests
  1134. *
  1135. * Requests are typically delayed because the current transaction
  1136. * state of an INVITE. Once the pending INVITE transaction terminates,
  1137. * the delayed request will be sent
  1138. */
  1139. struct ast_sip_session_delayed_request {
  1140. /*! Method of the request */
  1141. enum delayed_method method;
  1142. /*! Callback to call when the delayed request is created. */
  1143. ast_sip_session_request_creation_cb on_request_creation;
  1144. /*! Callback to call when the delayed request SDP is created */
  1145. ast_sip_session_sdp_creation_cb on_sdp_creation;
  1146. /*! Callback to call when the delayed request receives a response */
  1147. ast_sip_session_response_cb on_response;
  1148. /*! Whether to generate new SDP */
  1149. int generate_new_sdp;
  1150. /*! Requested media state for the SDP */
  1151. struct ast_sip_session_media_state *pending_media_state;
  1152. /*! Active media state at the time of the original request */
  1153. struct ast_sip_session_media_state *active_media_state;
  1154. AST_LIST_ENTRY(ast_sip_session_delayed_request) next;
  1155. };
  1156. static struct ast_sip_session_delayed_request *delayed_request_alloc(
  1157. enum delayed_method method,
  1158. ast_sip_session_request_creation_cb on_request_creation,
  1159. ast_sip_session_sdp_creation_cb on_sdp_creation,
  1160. ast_sip_session_response_cb on_response,
  1161. int generate_new_sdp,
  1162. struct ast_sip_session_media_state *pending_media_state,
  1163. struct ast_sip_session_media_state *active_media_state)
  1164. {
  1165. struct ast_sip_session_delayed_request *delay = ast_calloc(1, sizeof(*delay));
  1166. if (!delay) {
  1167. return NULL;
  1168. }
  1169. delay->method = method;
  1170. delay->on_request_creation = on_request_creation;
  1171. delay->on_sdp_creation = on_sdp_creation;
  1172. delay->on_response = on_response;
  1173. delay->generate_new_sdp = generate_new_sdp;
  1174. delay->pending_media_state = pending_media_state;
  1175. delay->active_media_state = active_media_state;
  1176. return delay;
  1177. }
  1178. static void delayed_request_free(struct ast_sip_session_delayed_request *delay)
  1179. {
  1180. ast_sip_session_media_state_free(delay->pending_media_state);
  1181. ast_sip_session_media_state_free(delay->active_media_state);
  1182. ast_free(delay);
  1183. }
  1184. /*!
  1185. * \internal
  1186. * \brief Send a delayed request
  1187. *
  1188. * \retval -1 failure
  1189. * \retval 0 success
  1190. * \retval 1 refresh request not sent as no change would occur
  1191. */
  1192. static int send_delayed_request(struct ast_sip_session *session, struct ast_sip_session_delayed_request *delay)
  1193. {
  1194. int res;
  1195. SCOPE_ENTER(3, "%s: sending delayed %s request\n",
  1196. ast_sip_session_get_name(session),
  1197. delayed_method2str(delay->method));
  1198. switch (delay->method) {
  1199. case DELAYED_METHOD_INVITE:
  1200. res = sip_session_refresh(session, delay->on_request_creation,
  1201. delay->on_sdp_creation, delay->on_response,
  1202. AST_SIP_SESSION_REFRESH_METHOD_INVITE, delay->generate_new_sdp, delay->pending_media_state,
  1203. delay->active_media_state, 1);
  1204. /* Ownership of media state transitions to ast_sip_session_refresh */
  1205. delay->pending_media_state = NULL;
  1206. delay->active_media_state = NULL;
  1207. SCOPE_EXIT_RTN_VALUE(res, "%s\n", ast_sip_session_get_name(session));
  1208. case DELAYED_METHOD_UPDATE:
  1209. res = sip_session_refresh(session, delay->on_request_creation,
  1210. delay->on_sdp_creation, delay->on_response,
  1211. AST_SIP_SESSION_REFRESH_METHOD_UPDATE, delay->generate_new_sdp, delay->pending_media_state,
  1212. delay->active_media_state, 1);
  1213. /* Ownership of media state transitions to ast_sip_session_refresh */
  1214. delay->pending_media_state = NULL;
  1215. delay->active_media_state = NULL;
  1216. SCOPE_EXIT_RTN_VALUE(res, "%s\n", ast_sip_session_get_name(session));
  1217. case DELAYED_METHOD_BYE:
  1218. ast_sip_session_terminate(session, 0);
  1219. SCOPE_EXIT_RTN_VALUE(0, "%s: Terminating session on delayed BYE\n", ast_sip_session_get_name(session));
  1220. }
  1221. SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_WARNING, "%s: Don't know how to send delayed %s(%d) request.\n",
  1222. ast_sip_session_get_name(session),
  1223. delayed_method2str(delay->method), delay->method);
  1224. }
  1225. /*!
  1226. * \internal
  1227. * \brief The current INVITE transaction is in the PROCEEDING state.
  1228. * \since 13.3.0
  1229. *
  1230. * \param vsession Session object.
  1231. *
  1232. * \retval 0 on success.
  1233. * \retval -1 on error.
  1234. */
  1235. static int invite_proceeding(void *vsession)
  1236. {
  1237. struct ast_sip_session *session = vsession;
  1238. struct ast_sip_session_delayed_request *delay;
  1239. int found = 0;
  1240. int res = 0;
  1241. SCOPE_ENTER(3, "%s\n", ast_sip_session_get_name(session));
  1242. AST_LIST_TRAVERSE_SAFE_BEGIN(&session->delayed_requests, delay, next) {
  1243. switch (delay->method) {
  1244. case DELAYED_METHOD_INVITE:
  1245. break;
  1246. case DELAYED_METHOD_UPDATE:
  1247. AST_LIST_REMOVE_CURRENT(next);
  1248. ast_trace(-1, "%s: Sending delayed %s request\n", ast_sip_session_get_name(session),
  1249. delayed_method2str(delay->method));
  1250. res = send_delayed_request(session, delay);
  1251. delayed_request_free(delay);
  1252. if (!res) {
  1253. found = 1;
  1254. }
  1255. break;
  1256. case DELAYED_METHOD_BYE:
  1257. /* A BYE is pending so don't bother anymore. */
  1258. found = 1;
  1259. break;
  1260. }
  1261. if (found) {
  1262. break;
  1263. }
  1264. }
  1265. AST_LIST_TRAVERSE_SAFE_END;
  1266. ao2_ref(session, -1);
  1267. SCOPE_EXIT_RTN_VALUE(res, "%s\n", ast_sip_session_get_name(session));
  1268. }
  1269. /*!
  1270. * \internal
  1271. * \brief The current INVITE transaction is in the TERMINATED state.
  1272. * \since 13.3.0
  1273. *
  1274. * \param vsession Session object.
  1275. *
  1276. * \retval 0 on success.
  1277. * \retval -1 on error.
  1278. */
  1279. static int invite_terminated(void *vsession)
  1280. {
  1281. struct ast_sip_session *session = vsession;
  1282. struct ast_sip_session_delayed_request *delay;
  1283. int found = 0;
  1284. int res = 0;
  1285. int timer_running;
  1286. SCOPE_ENTER(3, "%s\n", ast_sip_session_get_name(session));
  1287. /* re-INVITE collision timer running? */
  1288. timer_running = pj_timer_entry_running(&session->rescheduled_reinvite);
  1289. AST_LIST_TRAVERSE_SAFE_BEGIN(&session->delayed_requests, delay, next) {
  1290. switch (delay->method) {
  1291. case DELAYED_METHOD_INVITE:
  1292. if (!timer_running) {
  1293. found = 1;
  1294. }
  1295. break;
  1296. case DELAYED_METHOD_UPDATE:
  1297. case DELAYED_METHOD_BYE:
  1298. found = 1;
  1299. break;
  1300. }
  1301. if (found) {
  1302. AST_LIST_REMOVE_CURRENT(next);
  1303. ast_trace(-1, "%s: Sending delayed %s request\n", ast_sip_session_get_name(session),
  1304. delayed_method2str(delay->method));
  1305. res = send_delayed_request(session, delay);
  1306. delayed_request_free(delay);
  1307. if (!res) {
  1308. break;
  1309. }
  1310. }
  1311. }
  1312. AST_LIST_TRAVERSE_SAFE_END;
  1313. ao2_ref(session, -1);
  1314. SCOPE_EXIT_RTN_VALUE(res, "%s\n", ast_sip_session_get_name(session));
  1315. }
  1316. /*!
  1317. * \internal
  1318. * \brief INVITE collision timeout.
  1319. * \since 13.3.0
  1320. *
  1321. * \param vsession Session object.
  1322. *
  1323. * \retval 0 on success.
  1324. * \retval -1 on error.
  1325. */
  1326. static int invite_collision_timeout(void *vsession)
  1327. {
  1328. struct ast_sip_session *session = vsession;
  1329. int res;
  1330. SCOPE_ENTER(3, "%s\n", ast_sip_session_get_name(session));
  1331. if (session->inv_session->invite_tsx) {
  1332. /*
  1333. * INVITE transaction still active. Let it send
  1334. * the collision re-INVITE when it terminates.
  1335. */
  1336. ao2_ref(session, -1);
  1337. res = 0;
  1338. } else {
  1339. res = invite_terminated(session);
  1340. }
  1341. SCOPE_EXIT_RTN_VALUE(res, "%s\n", ast_sip_session_get_name(session));
  1342. }
  1343. /*!
  1344. * \internal
  1345. * \brief The current UPDATE transaction is in the COMPLETED state.
  1346. * \since 13.3.0
  1347. *
  1348. * \param vsession Session object.
  1349. *
  1350. * \retval 0 on success.
  1351. * \retval -1 on error.
  1352. */
  1353. static int update_completed(void *vsession)
  1354. {
  1355. struct ast_sip_session *session = vsession;
  1356. int res;
  1357. if (session->inv_session->invite_tsx) {
  1358. res = invite_proceeding(session);
  1359. } else {
  1360. res = invite_terminated(session);
  1361. }
  1362. return res;
  1363. }
  1364. static void check_delayed_requests(struct ast_sip_session *session,
  1365. int (*cb)(void *vsession))
  1366. {
  1367. ao2_ref(session, +1);
  1368. if (ast_sip_push_task(session->serializer, cb, session)) {
  1369. ao2_ref(session, -1);
  1370. }
  1371. }
  1372. static int delay_request(struct ast_sip_session *session,
  1373. ast_sip_session_request_creation_cb on_request,
  1374. ast_sip_session_sdp_creation_cb on_sdp_creation,
  1375. ast_sip_session_response_cb on_response,
  1376. int generate_new_sdp,
  1377. enum delayed_method method,
  1378. struct ast_sip_session_media_state *pending_media_state,
  1379. struct ast_sip_session_media_state *active_media_state,
  1380. int queue_head)
  1381. {
  1382. struct ast_sip_session_delayed_request *delay = delayed_request_alloc(method,
  1383. on_request, on_sdp_creation, on_response, generate_new_sdp, pending_media_state,
  1384. active_media_state);
  1385. SCOPE_ENTER(3, "%s\n", ast_sip_session_get_name(session));
  1386. if (!delay) {
  1387. ast_sip_session_media_state_free(pending_media_state);
  1388. ast_sip_session_media_state_free(active_media_state);
  1389. SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "Unable to allocate delay request\n");
  1390. }
  1391. if (method == DELAYED_METHOD_BYE || queue_head) {
  1392. /* Send BYE as early as possible */
  1393. AST_LIST_INSERT_HEAD(&session->delayed_requests, delay, next);
  1394. } else {
  1395. AST_LIST_INSERT_TAIL(&session->delayed_requests, delay, next);
  1396. }
  1397. SCOPE_EXIT_RTN_VALUE(0);
  1398. }
  1399. static pjmedia_sdp_session *generate_session_refresh_sdp(struct ast_sip_session *session)
  1400. {
  1401. pjsip_inv_session *inv_session = session->inv_session;
  1402. const pjmedia_sdp_session *previous_sdp = NULL;
  1403. SCOPE_ENTER(3, "%s\n", ast_sip_session_get_name(session));
  1404. if (inv_session->neg) {
  1405. if (pjmedia_sdp_neg_was_answer_remote(inv_session->neg)) {
  1406. pjmedia_sdp_neg_get_active_remote(inv_session->neg, &previous_sdp);
  1407. } else {
  1408. pjmedia_sdp_neg_get_active_local(inv_session->neg, &previous_sdp);
  1409. }
  1410. }
  1411. SCOPE_EXIT_RTN_VALUE(create_local_sdp(inv_session, session, previous_sdp, 0));
  1412. }
  1413. static void set_from_header(struct ast_sip_session *session)
  1414. {
  1415. struct ast_party_id effective_id;
  1416. struct ast_party_id connected_id;
  1417. pj_pool_t *dlg_pool;
  1418. pjsip_fromto_hdr *dlg_info;
  1419. pjsip_contact_hdr *dlg_contact;
  1420. pjsip_name_addr *dlg_info_name_addr;
  1421. pjsip_sip_uri *dlg_info_uri;
  1422. pjsip_sip_uri *dlg_contact_uri;
  1423. int restricted;
  1424. const char *pjsip_from_domain;
  1425. if (!session->channel || session->saved_from_hdr) {
  1426. return;
  1427. }
  1428. /* We need to save off connected_id for RPID/PAI generation */
  1429. ast_party_id_init(&connected_id);
  1430. ast_channel_lock(session->channel);
  1431. effective_id = ast_channel_connected_effective_id(session->channel);
  1432. ast_party_id_copy(&connected_id, &effective_id);
  1433. ast_channel_unlock(session->channel);
  1434. restricted =
  1435. ((ast_party_id_presentation(&connected_id) & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED);
  1436. /* Now set up dlg->local.info so pjsip can correctly generate From */
  1437. dlg_pool = session->inv_session->dlg->pool;
  1438. dlg_info = session->inv_session->dlg->local.info;
  1439. dlg_contact = session->inv_session->dlg->local.contact;
  1440. dlg_info_name_addr = (pjsip_name_addr *) dlg_info->uri;
  1441. dlg_info_uri = pjsip_uri_get_uri(dlg_info_name_addr);
  1442. dlg_contact_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(dlg_contact->uri);
  1443. if (session->endpoint->id.trust_outbound || !restricted) {
  1444. ast_sip_modify_id_header(dlg_pool, dlg_info, &connected_id);
  1445. if (ast_sip_get_use_callerid_contact() && ast_strlen_zero(session->endpoint->contact_user)) {
  1446. pj_strdup2(dlg_pool, &dlg_contact_uri->user, S_COR(connected_id.number.valid, connected_id.number.str, ""));
  1447. }
  1448. }
  1449. ast_party_id_free(&connected_id);
  1450. if (!ast_strlen_zero(session->endpoint->fromuser)) {
  1451. dlg_info_name_addr->display.ptr = NULL;
  1452. dlg_info_name_addr->display.slen = 0;
  1453. pj_strdup2(dlg_pool, &dlg_info_uri->user, session->endpoint->fromuser);
  1454. }
  1455. if (!ast_strlen_zero(session->endpoint->fromdomain)) {
  1456. pj_strdup2(dlg_pool, &dlg_info_uri->host, session->endpoint->fromdomain);
  1457. }
  1458. /*
  1459. * Channel variable for compatibility with chan_sip SIPFROMDOMAIN
  1460. */
  1461. ast_channel_lock(session->channel);
  1462. pjsip_from_domain = pbx_builtin_getvar_helper(session->channel, "SIPFROMDOMAIN");
  1463. if (!ast_strlen_zero(pjsip_from_domain)) {
  1464. ast_debug(3, "%s: From header domain reset by channel variable SIPFROMDOMAIN (%s)\n",
  1465. ast_sip_session_get_name(session), pjsip_from_domain);
  1466. pj_strdup2(dlg_pool, &dlg_info_uri->host, pjsip_from_domain);
  1467. }
  1468. ast_channel_unlock(session->channel);
  1469. /* We need to save off the non-anonymized From for RPID/PAI generation (for domain) */
  1470. session->saved_from_hdr = pjsip_hdr_clone(dlg_pool, dlg_info);
  1471. ast_sip_add_usereqphone(session->endpoint, dlg_pool, session->saved_from_hdr->uri);
  1472. /* In chan_sip, fromuser and fromdomain trump restricted so we only
  1473. * anonymize if they're not set.
  1474. */
  1475. if (restricted) {
  1476. /* fromuser doesn't provide a display name so we always set it */
  1477. pj_strdup2(dlg_pool, &dlg_info_name_addr->display, "Anonymous");
  1478. if (ast_strlen_zero(session->endpoint->fromuser)) {
  1479. pj_strdup2(dlg_pool, &dlg_info_uri->user, "anonymous");
  1480. }
  1481. if (ast_sip_get_use_callerid_contact() && ast_strlen_zero(session->endpoint->contact_user)) {
  1482. pj_strdup2(dlg_pool, &dlg_contact_uri->user, "anonymous");
  1483. }
  1484. if (ast_strlen_zero(session->endpoint->fromdomain)) {
  1485. pj_strdup2(dlg_pool, &dlg_info_uri->host, "anonymous.invalid");
  1486. }
  1487. } else {
  1488. ast_sip_add_usereqphone(session->endpoint, dlg_pool, dlg_info->uri);
  1489. }
  1490. }
  1491. /*
  1492. * Helper macros for merging and validating media states
  1493. */
  1494. #define STREAM_REMOVED(_stream) (ast_stream_get_state(_stream) == AST_STREAM_STATE_REMOVED)
  1495. #define STATE_REMOVED(_stream_state) (_stream_state == AST_STREAM_STATE_REMOVED)
  1496. #define STATE_NONE(_stream_state) (_stream_state == AST_STREAM_STATE_END)
  1497. #define GET_STREAM_SAFE(_topology, _i) (_i < ast_stream_topology_get_count(_topology) ? ast_stream_topology_get_stream(_topology, _i) : NULL)
  1498. #define GET_STREAM_STATE_SAFE(_stream) (_stream ? ast_stream_get_state(_stream) : AST_STREAM_STATE_END)
  1499. #define GET_STREAM_NAME_SAFE(_stream) (_stream ? ast_stream_get_name(_stream) : "")
  1500. /*!
  1501. * \internal
  1502. * \brief Validate a media state
  1503. *
  1504. * \param session_name For log messages
  1505. * \param state Media state
  1506. *
  1507. * \retval 1 The media state is valid
  1508. * \retval 0 The media state is NOT valid
  1509. *
  1510. */
  1511. static int is_media_state_valid(const char *session_name, struct ast_sip_session_media_state *state)
  1512. {
  1513. int stream_count = ast_stream_topology_get_count(state->topology);
  1514. int session_count = AST_VECTOR_SIZE(&state->sessions);
  1515. int i;
  1516. int res = 0;
  1517. SCOPE_ENTER(3, "%s: Topology: %s\n", session_name,
  1518. ast_str_tmp(256, ast_stream_topology_to_str(state->topology, &STR_TMP)));
  1519. if (session_count != stream_count) {
  1520. SCOPE_EXIT_RTN_VALUE(0, "%s: %d media sessions but %d streams\n", session_name,
  1521. session_count, stream_count);
  1522. }
  1523. for (i = 0; i < stream_count; i++) {
  1524. struct ast_sip_session_media *media = NULL;
  1525. struct ast_stream *stream = ast_stream_topology_get_stream(state->topology, i);
  1526. const char *stream_name = NULL;
  1527. int j;
  1528. SCOPE_ENTER(4, "%s: Checking stream %s\n", session_name, ast_str_tmp(128, ast_stream_to_str(stream, &STR_TMP)));
  1529. if (!stream) {
  1530. SCOPE_EXIT_EXPR(goto end, "%s: stream %d is null\n", session_name, i);
  1531. }
  1532. stream_name = ast_stream_get_name(stream);
  1533. for (j = 0; j < stream_count; j++) {
  1534. struct ast_stream *possible_dup = ast_stream_topology_get_stream(state->topology, j);
  1535. if (j == i || !possible_dup) {
  1536. continue;
  1537. }
  1538. if (!STREAM_REMOVED(stream) && ast_strings_equal(stream_name, GET_STREAM_NAME_SAFE(possible_dup))) {
  1539. SCOPE_EXIT_EXPR(goto end, "%s: stream %i %s is duplicated to %d\n", session_name,
  1540. i, stream_name, j);
  1541. }
  1542. }
  1543. media = AST_VECTOR_GET(&state->sessions, i);
  1544. if (!media) {
  1545. SCOPE_EXIT_EXPR(continue, "%s: media %d is null\n", session_name, i);
  1546. }
  1547. for (j = 0; j < session_count; j++) {
  1548. struct ast_sip_session_media *possible_dup = AST_VECTOR_GET(&state->sessions, j);
  1549. if (j == i || !possible_dup) {
  1550. continue;
  1551. }
  1552. if (!ast_strlen_zero(media->label) && !ast_strlen_zero(possible_dup->label)
  1553. && ast_strings_equal(media->label, possible_dup->label)) {
  1554. SCOPE_EXIT_EXPR(goto end, "%s: media %d %s is duplicated to %d\n", session_name,
  1555. i, media->label, j);
  1556. }
  1557. }
  1558. if (media->stream_num != i) {
  1559. SCOPE_EXIT_EXPR(goto end, "%s: media %d has stream_num %d\n", session_name,
  1560. i, media->stream_num);
  1561. }
  1562. if (media->type != ast_stream_get_type(stream)) {
  1563. SCOPE_EXIT_EXPR(goto end, "%s: media %d has type %s but stream has type %s\n", stream_name,
  1564. i, ast_codec_media_type2str(media->type), ast_codec_media_type2str(ast_stream_get_type(stream)));
  1565. }
  1566. SCOPE_EXIT("%s: Done with stream %s\n", session_name, ast_str_tmp(128, ast_stream_to_str(stream, &STR_TMP)));
  1567. }
  1568. res = 1;
  1569. end:
  1570. SCOPE_EXIT_RTN_VALUE(res, "%s: %s\n", session_name, res ? "Valid" : "NOT Valid");
  1571. }
  1572. /*!
  1573. * \internal
  1574. * \brief Merge media states for a delayed session refresh
  1575. *
  1576. * \param session_name For log messages
  1577. * \param delayed_pending_state The pending media state at the time the resuest was queued
  1578. * \param delayed_active_state The active media state at the time the resuest was queued
  1579. * \param current_active_state The current active media state
  1580. * \param run_post_validation Whether to run validation on the resulting media state or not
  1581. *
  1582. * \returns New merged topology or NULL if there's an error
  1583. *
  1584. */
  1585. static struct ast_sip_session_media_state *resolve_refresh_media_states(
  1586. const char *session_name,
  1587. struct ast_sip_session_media_state *delayed_pending_state,
  1588. struct ast_sip_session_media_state *delayed_active_state,
  1589. struct ast_sip_session_media_state *current_active_state,
  1590. int run_post_validation)
  1591. {
  1592. RAII_VAR(struct ast_sip_session_media_state *, new_pending_state, NULL, ast_sip_session_media_state_free);
  1593. struct ast_sip_session_media_state *returned_media_state = NULL;
  1594. struct ast_stream_topology *delayed_pending = delayed_pending_state->topology;
  1595. struct ast_stream_topology *delayed_active = delayed_active_state->topology;
  1596. struct ast_stream_topology *current_active = current_active_state->topology;
  1597. struct ast_stream_topology *new_pending = NULL;
  1598. int i;
  1599. int max_stream_count;
  1600. int res;
  1601. SCOPE_ENTER(2, "%s: DP: %s DA: %s CA: %s\n", session_name,
  1602. ast_str_tmp(256, ast_stream_topology_to_str(delayed_pending, &STR_TMP)),
  1603. ast_str_tmp(256, ast_stream_topology_to_str(delayed_active, &STR_TMP)),
  1604. ast_str_tmp(256, ast_stream_topology_to_str(current_active, &STR_TMP))
  1605. );
  1606. max_stream_count = MAX(ast_stream_topology_get_count(delayed_pending),
  1607. ast_stream_topology_get_count(delayed_active));
  1608. max_stream_count = MAX(max_stream_count, ast_stream_topology_get_count(current_active));
  1609. /*
  1610. * The new_pending_state is always based on the currently negotiated state because
  1611. * the stream ordering in its topology must be preserved.
  1612. */
  1613. new_pending_state = ast_sip_session_media_state_clone(current_active_state);
  1614. if (!new_pending_state) {
  1615. SCOPE_EXIT_LOG_RTN_VALUE(NULL, LOG_ERROR, "%s: Couldn't clone current_active_state to new_pending_state\n", session_name);
  1616. }
  1617. new_pending = new_pending_state->topology;
  1618. for (i = 0; i < max_stream_count; i++) {
  1619. struct ast_stream *dp_stream = GET_STREAM_SAFE(delayed_pending, i);
  1620. struct ast_stream *da_stream = GET_STREAM_SAFE(delayed_active, i);
  1621. struct ast_stream *ca_stream = GET_STREAM_SAFE(current_active, i);
  1622. struct ast_stream *np_stream = GET_STREAM_SAFE(new_pending, i);
  1623. struct ast_stream *found_da_stream = NULL;
  1624. struct ast_stream *found_np_stream = NULL;
  1625. enum ast_stream_state dp_state = GET_STREAM_STATE_SAFE(dp_stream);
  1626. enum ast_stream_state da_state = GET_STREAM_STATE_SAFE(da_stream);
  1627. enum ast_stream_state ca_state = GET_STREAM_STATE_SAFE(ca_stream);
  1628. enum ast_stream_state np_state = GET_STREAM_STATE_SAFE(np_stream);
  1629. enum ast_stream_state found_da_state = AST_STREAM_STATE_END;
  1630. enum ast_stream_state found_np_state = AST_STREAM_STATE_END;
  1631. const char *da_name = GET_STREAM_NAME_SAFE(da_stream);
  1632. const char *dp_name = GET_STREAM_NAME_SAFE(dp_stream);
  1633. const char *ca_name = GET_STREAM_NAME_SAFE(ca_stream);
  1634. const char *np_name = GET_STREAM_NAME_SAFE(np_stream);
  1635. const char *found_da_name __attribute__((unused)) = "";
  1636. const char *found_np_name __attribute__((unused)) = "";
  1637. int found_da_slot __attribute__((unused)) = -1;
  1638. int found_np_slot = -1;
  1639. int removed_np_slot = -1;
  1640. int j;
  1641. SCOPE_ENTER(3, "%s: slot: %d DP: %s DA: %s CA: %s\n", session_name, i,
  1642. ast_str_tmp(128, ast_stream_to_str(dp_stream, &STR_TMP)),
  1643. ast_str_tmp(128, ast_stream_to_str(da_stream, &STR_TMP)),
  1644. ast_str_tmp(128, ast_stream_to_str(ca_stream, &STR_TMP)));
  1645. if (STATE_NONE(da_state) && STATE_NONE(dp_state) && STATE_NONE(ca_state)) {
  1646. SCOPE_EXIT_EXPR(break, "%s: All gone\n", session_name);
  1647. }
  1648. /*
  1649. * Simple cases are handled first to avoid having to search the NP and DA
  1650. * topologies for streams with the same name but not in the same position.
  1651. */
  1652. if (STATE_NONE(dp_state) && !STATE_NONE(da_state)) {
  1653. /*
  1654. * The slot in the delayed pending topology can't be empty if the delayed
  1655. * active topology has a stream there. Streams can't just go away. They
  1656. * can be reused or marked "removed" but they can't go away.
  1657. */
  1658. SCOPE_EXIT_LOG_RTN_VALUE(NULL, LOG_WARNING, "%s: DP slot is empty but DA is not\n", session_name);
  1659. }
  1660. if (STATE_NONE(dp_state)) {
  1661. /*
  1662. * The current active topology can certainly have streams that weren't
  1663. * in existence when the delayed request was queued. In this case,
  1664. * no action is needed since we already copied the current active topology
  1665. * to the new pending one.
  1666. */
  1667. SCOPE_EXIT_EXPR(continue, "%s: No DP stream so use CA stream as is\n", session_name);
  1668. }
  1669. if (ast_strings_equal(dp_name, da_name) && ast_strings_equal(da_name, ca_name)) {
  1670. /*
  1671. * The delayed pending stream in this slot matches by name, the streams
  1672. * in the same slot in the other two topologies. Easy case.
  1673. */
  1674. ast_trace(-1, "%s: Same stream in all 3 states\n", session_name);
  1675. if (dp_state == da_state && da_state == ca_state) {
  1676. /* All the same state, no need to update. */
  1677. SCOPE_EXIT_EXPR(continue, "%s: All in the same state so nothing to do\n", session_name);
  1678. }
  1679. if (da_state != ca_state) {
  1680. /*
  1681. * Something set the CA state between the time this request was queued
  1682. * and now. The CA state wins so we don't do anything.
  1683. */
  1684. SCOPE_EXIT_EXPR(continue, "%s: Ignoring request to change state from %s to %s\n",
  1685. session_name, ast_stream_state2str(ca_state), ast_stream_state2str(dp_state));
  1686. }
  1687. if (dp_state != da_state) {
  1688. /* DP needs to update the state */
  1689. ast_stream_set_state(np_stream, dp_state);
  1690. SCOPE_EXIT_EXPR(continue, "%s: Changed NP stream state from %s to %s\n",
  1691. session_name, ast_stream_state2str(ca_state), ast_stream_state2str(dp_state));
  1692. }
  1693. }
  1694. /*
  1695. * We're done with the simple cases. For the rest, we need to identify if the
  1696. * DP stream we're trying to take action on is already in the other topologies
  1697. * possibly in a different slot. To do that, if the stream in the DA or CA slots
  1698. * doesn't match the current DP stream, we need to iterate over the topology
  1699. * looking for a stream with the same name.
  1700. */
  1701. /*
  1702. * Since we already copied all of the CA streams to the NP topology, we'll use it
  1703. * instead of CA because we'll be updating the NP as we go.
  1704. */
  1705. if (!ast_strings_equal(dp_name, np_name)) {
  1706. /*
  1707. * The NP stream in this slot doesn't have the same name as the DP stream
  1708. * so we need to see if it's in another NP slot. We're not going to stop
  1709. * when we find a matching stream because we also want to find the first
  1710. * removed removed slot, if any, so we can re-use this slot. We'll break
  1711. * early if we find both before we reach the end.
  1712. */
  1713. ast_trace(-1, "%s: Checking if DP is already in NP somewhere\n", session_name);
  1714. for (j = 0; j < ast_stream_topology_get_count(new_pending); j++) {
  1715. struct ast_stream *possible_existing = ast_stream_topology_get_stream(new_pending, j);
  1716. const char *possible_existing_name = GET_STREAM_NAME_SAFE(possible_existing);
  1717. ast_trace(-1, "%s: Checking %s against %s\n", session_name, dp_name, possible_existing_name);
  1718. if (found_np_slot == -1 && ast_strings_equal(dp_name, possible_existing_name)) {
  1719. ast_trace(-1, "%s: Pending stream %s slot %d is in NP slot %d\n", session_name,
  1720. dp_name, i, j);
  1721. found_np_slot = j;
  1722. found_np_stream = possible_existing;
  1723. found_np_state = ast_stream_get_state(possible_existing);
  1724. found_np_name = ast_stream_get_name(possible_existing);
  1725. }
  1726. if (STREAM_REMOVED(possible_existing) && removed_np_slot == -1) {
  1727. removed_np_slot = j;
  1728. }
  1729. if (removed_np_slot >= 0 && found_np_slot >= 0) {
  1730. break;
  1731. }
  1732. }
  1733. } else {
  1734. /* Makes the subsequent code easier */
  1735. found_np_slot = i;
  1736. found_np_stream = np_stream;
  1737. found_np_state = np_state;
  1738. found_np_name = np_name;
  1739. }
  1740. if (!ast_strings_equal(dp_name, da_name)) {
  1741. /*
  1742. * The DA stream in this slot doesn't have the same name as the DP stream
  1743. * so we need to see if it's in another DA slot. In real life, the DA stream
  1744. * in this slot could have a different name but there shouldn't be a case
  1745. * where the DP stream is another slot in the DA topology. Just in case though.
  1746. * We don't care about removed slots in the DA topology.
  1747. */
  1748. ast_trace(-1, "%s: Checking if DP is already in DA somewhere\n", session_name);
  1749. for (j = 0; j < ast_stream_topology_get_count(delayed_active); j++) {
  1750. struct ast_stream *possible_existing = ast_stream_topology_get_stream(delayed_active, j);
  1751. const char *possible_existing_name = GET_STREAM_NAME_SAFE(possible_existing);
  1752. ast_trace(-1, "%s: Checking %s against %s\n", session_name, dp_name, possible_existing_name);
  1753. if (ast_strings_equal(dp_name, possible_existing_name)) {
  1754. ast_trace(-1, "%s: Pending stream %s slot %d is already in delayed active slot %d\n",
  1755. session_name, dp_name, i, j);
  1756. found_da_slot = j;
  1757. found_da_stream = possible_existing;
  1758. found_da_state = ast_stream_get_state(possible_existing);
  1759. found_da_name = ast_stream_get_name(possible_existing);
  1760. break;
  1761. }
  1762. }
  1763. } else {
  1764. /* Makes the subsequent code easier */
  1765. found_da_slot = i;
  1766. found_da_stream = da_stream;
  1767. found_da_state = da_state;
  1768. found_da_name = da_name;
  1769. }
  1770. ast_trace(-1, "%s: Found NP slot: %d Found removed NP slot: %d Found DA slot: %d\n",
  1771. session_name, found_np_slot, removed_np_slot, found_da_slot);
  1772. /*
  1773. * Now we know whether the DP stream is new or changing state and we know if the DP
  1774. * stream exists in the other topologies and if so, where in those topologies it exists.
  1775. */
  1776. if (!found_da_stream) {
  1777. /*
  1778. * The DP stream isn't in the DA topology which would imply that the intention of the
  1779. * request was to add the stream, not change its state. It's possible though that
  1780. * the stream was added by another request between the time this request was queued
  1781. * and now so we need to check the CA topology as well.
  1782. */
  1783. ast_trace(-1, "%s: There was no corresponding DA stream so the request was to add a stream\n", session_name);
  1784. if (found_np_stream) {
  1785. /*
  1786. * We found it in the CA topology. Since the intention was to add it
  1787. * and it's already there, there's nothing to do.
  1788. */
  1789. SCOPE_EXIT_EXPR(continue, "%s: New stream requested but it's already in CA\n", session_name);
  1790. } else {
  1791. /* OK, it's not in either which would again imply that the intention of the
  1792. * request was to add the stream.
  1793. */
  1794. ast_trace(-1, "%s: There was no corresponding NP stream\n", session_name);
  1795. if (STATE_REMOVED(dp_state)) {
  1796. /*
  1797. * How can DP request to remove a stream that doesn't seem to exist anythere?
  1798. * It's not. It's possible that the stream was already removed and the slot
  1799. * reused in the CA topology, but it would still have to exist in the DA
  1800. * topology. Bail.
  1801. */
  1802. SCOPE_EXIT_LOG_RTN_VALUE(NULL, LOG_ERROR,
  1803. "%s: Attempting to remove stream %d:%s but it doesn't exist anywhere.\n", session_name, i, dp_name);
  1804. } else {
  1805. /*
  1806. * We're now sure we want to add the the stream. Since we can re-use
  1807. * slots in the CA topology that have streams marked as "removed", we
  1808. * use the slot we saved in removed_np_slot if it exists.
  1809. */
  1810. ast_trace(-1, "%s: Checking for open slot\n", session_name);
  1811. if (removed_np_slot >= 0) {
  1812. struct ast_sip_session_media *old_media = AST_VECTOR_GET(&new_pending_state->sessions, removed_np_slot);
  1813. res = ast_stream_topology_set_stream(new_pending, removed_np_slot, ast_stream_clone(dp_stream, NULL));
  1814. if (res != 0) {
  1815. SCOPE_EXIT_LOG_RTN_VALUE(NULL, LOG_WARNING, "%s: Couldn't set stream in new topology\n", session_name);
  1816. }
  1817. /*
  1818. * Since we're reusing the removed_np_slot slot for something else, we need
  1819. * to free and remove any session media already in it.
  1820. * ast_stream_topology_set_stream() took care of freeing the old stream.
  1821. */
  1822. res = AST_VECTOR_REPLACE(&new_pending_state->sessions, removed_np_slot, NULL);
  1823. if (res != 0) {
  1824. SCOPE_EXIT_LOG_RTN_VALUE(NULL, LOG_WARNING, "%s: Couldn't replace media session\n", session_name);
  1825. }
  1826. ao2_cleanup(old_media);
  1827. SCOPE_EXIT_EXPR(continue, "%s: Replaced removed stream in slot %d\n",
  1828. session_name, removed_np_slot);
  1829. } else {
  1830. int new_slot = ast_stream_topology_append_stream(new_pending, ast_stream_clone(dp_stream, NULL));
  1831. if (new_slot < 0) {
  1832. SCOPE_EXIT_LOG_RTN_VALUE(NULL, LOG_WARNING, "%s: Couldn't append stream in new topology\n", session_name);
  1833. }
  1834. res = AST_VECTOR_REPLACE(&new_pending_state->sessions, new_slot, NULL);
  1835. if (res != 0) {
  1836. SCOPE_EXIT_LOG_RTN_VALUE(NULL, LOG_WARNING, "%s: Couldn't replace media session\n", session_name);
  1837. }
  1838. SCOPE_EXIT_EXPR(continue, "%s: Appended new stream to slot %d\n",
  1839. session_name, new_slot);
  1840. }
  1841. }
  1842. }
  1843. } else {
  1844. /*
  1845. * The DP stream exists in the DA topology so it's a change of some sort.
  1846. */
  1847. ast_trace(-1, "%s: There was a corresponding DA stream so the request was to change/remove a stream\n", session_name);
  1848. if (dp_state == found_da_state) {
  1849. /* No change? Let's see if it's in CA */
  1850. if (!found_np_stream) {
  1851. /*
  1852. * The DP and DA state are the same which would imply that the stream
  1853. * already exists but it's not in the CA topology. It's possible that
  1854. * between the time this request was queued and now the stream was removed
  1855. * from the CA topology and the slot used for something else. Nothing
  1856. * we can do here.
  1857. */
  1858. SCOPE_EXIT_EXPR(continue, "%s: Stream doesn't exist in CA so nothing to do\n", session_name);
  1859. } else if (dp_state == found_np_state) {
  1860. SCOPE_EXIT_EXPR(continue, "%s: States are the same all around so nothing to do\n", session_name);
  1861. } else {
  1862. SCOPE_EXIT_EXPR(continue, "%s: Something changed the CA state so we're going to leave it as is\n", session_name);
  1863. }
  1864. } else {
  1865. /* We have a state change. */
  1866. ast_trace(-1, "%s: Requesting state change to %s\n", session_name, ast_stream_state2str(dp_state));
  1867. if (!found_np_stream) {
  1868. SCOPE_EXIT_EXPR(continue, "%s: Stream doesn't exist in CA so nothing to do\n", session_name);
  1869. } else if (da_state == found_np_state) {
  1870. ast_stream_set_state(found_np_stream, dp_state);
  1871. SCOPE_EXIT_EXPR(continue, "%s: Changed NP stream state from %s to %s\n",
  1872. session_name, ast_stream_state2str(found_np_state), ast_stream_state2str(dp_state));
  1873. } else {
  1874. SCOPE_EXIT_EXPR(continue, "%s: Something changed the CA state so we're going to leave it as is\n",
  1875. session_name);
  1876. }
  1877. }
  1878. }
  1879. SCOPE_EXIT("%s: Done with slot %d\n", session_name, i);
  1880. }
  1881. ast_trace(-1, "%s: Resetting default media states\n", session_name);
  1882. for (i = 0; i < AST_MEDIA_TYPE_END; i++) {
  1883. int j;
  1884. new_pending_state->default_session[i] = NULL;
  1885. for (j = 0; j < AST_VECTOR_SIZE(&new_pending_state->sessions); j++) {
  1886. struct ast_sip_session_media *media = AST_VECTOR_GET(&new_pending_state->sessions, j);
  1887. struct ast_stream *stream = ast_stream_topology_get_stream(new_pending_state->topology, j);
  1888. if (media && media->type == i && !STREAM_REMOVED(stream)) {
  1889. new_pending_state->default_session[i] = media;
  1890. break;
  1891. }
  1892. }
  1893. }
  1894. if (run_post_validation) {
  1895. ast_trace(-1, "%s: Running post-validation\n", session_name);
  1896. if (!is_media_state_valid(session_name, new_pending_state)) {
  1897. SCOPE_EXIT_LOG_RTN_VALUE(NULL, LOG_ERROR, "State not consistent\n");
  1898. }
  1899. }
  1900. /*
  1901. * We need to move the new pending state to another variable and set new_pending_state to NULL
  1902. * so RAII_VAR doesn't free it.
  1903. */
  1904. returned_media_state = new_pending_state;
  1905. new_pending_state = NULL;
  1906. SCOPE_EXIT_RTN_VALUE(returned_media_state, "%s: NP: %s\n", session_name,
  1907. ast_str_tmp(256, ast_stream_topology_to_str(new_pending, &STR_TMP)));
  1908. }
  1909. static int sip_session_refresh(struct ast_sip_session *session,
  1910. ast_sip_session_request_creation_cb on_request_creation,
  1911. ast_sip_session_sdp_creation_cb on_sdp_creation,
  1912. ast_sip_session_response_cb on_response,
  1913. enum ast_sip_session_refresh_method method, int generate_new_sdp,
  1914. struct ast_sip_session_media_state *pending_media_state,
  1915. struct ast_sip_session_media_state *active_media_state,
  1916. int queued)
  1917. {
  1918. pjsip_inv_session *inv_session = session->inv_session;
  1919. pjmedia_sdp_session *new_sdp = NULL;
  1920. pjsip_tx_data *tdata;
  1921. int res = -1;
  1922. SCOPE_ENTER(3, "%s: New SDP? %s Queued? %s DP: %s DA: %s\n", ast_sip_session_get_name(session),
  1923. generate_new_sdp ? "yes" : "no", queued ? "yes" : "no",
  1924. pending_media_state ? ast_str_tmp(256, ast_stream_topology_to_str(pending_media_state->topology, &STR_TMP)) : "none",
  1925. active_media_state ? ast_str_tmp(256, ast_stream_topology_to_str(active_media_state->topology, &STR_TMP)) : "none");
  1926. if (pending_media_state && (!pending_media_state->topology || !generate_new_sdp)) {
  1927. ast_sip_session_media_state_free(pending_media_state);
  1928. ast_sip_session_media_state_free(active_media_state);
  1929. SCOPE_EXIT_RTN_VALUE(-1, "%s: Not sending reinvite because %s%s\n", ast_sip_session_get_name(session),
  1930. pending_media_state->topology == NULL ? "pending topology is null " : "",
  1931. !generate_new_sdp ? "generate_new_sdp is false" : "");
  1932. }
  1933. if (inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
  1934. /* Don't try to do anything with a hung-up call */
  1935. ast_sip_session_media_state_free(pending_media_state);
  1936. ast_sip_session_media_state_free(active_media_state);
  1937. SCOPE_EXIT_RTN_VALUE(0, "%s: Not sending reinvite because of disconnected state\n",
  1938. ast_sip_session_get_name(session));
  1939. }
  1940. /* If the dialog has not yet been established we have to defer until it has */
  1941. if (inv_session->dlg->state != PJSIP_DIALOG_STATE_ESTABLISHED) {
  1942. res = delay_request(session, on_request_creation, on_sdp_creation, on_response,
  1943. generate_new_sdp,
  1944. method == AST_SIP_SESSION_REFRESH_METHOD_INVITE
  1945. ? DELAYED_METHOD_INVITE : DELAYED_METHOD_UPDATE,
  1946. pending_media_state, active_media_state ? active_media_state : ast_sip_session_media_state_clone(session->active_media_state), queued);
  1947. SCOPE_EXIT_RTN_VALUE(res, "%s: Delay sending reinvite because dialog has not been established\n",
  1948. ast_sip_session_get_name(session));
  1949. }
  1950. if (method == AST_SIP_SESSION_REFRESH_METHOD_INVITE) {
  1951. if (inv_session->invite_tsx) {
  1952. /* We can't send a reinvite yet, so delay it */
  1953. res = delay_request(session, on_request_creation, on_sdp_creation,
  1954. on_response, generate_new_sdp, DELAYED_METHOD_INVITE, pending_media_state,
  1955. active_media_state ? active_media_state : ast_sip_session_media_state_clone(session->active_media_state), queued);
  1956. SCOPE_EXIT_RTN_VALUE(res, "%s: Delay sending reinvite because of outstanding transaction\n",
  1957. ast_sip_session_get_name(session));
  1958. } else if (inv_session->state != PJSIP_INV_STATE_CONFIRMED) {
  1959. /* Initial INVITE transaction failed to progress us to a confirmed state
  1960. * which means re-invites are not possible
  1961. */
  1962. ast_sip_session_media_state_free(pending_media_state);
  1963. ast_sip_session_media_state_free(active_media_state);
  1964. SCOPE_EXIT_RTN_VALUE(0, "%s: Not sending reinvite because not in confirmed state\n",
  1965. ast_sip_session_get_name(session));
  1966. }
  1967. }
  1968. if (generate_new_sdp) {
  1969. /* SDP can only be generated if current negotiation has already completed */
  1970. if (inv_session->neg
  1971. && pjmedia_sdp_neg_get_state(inv_session->neg)
  1972. != PJMEDIA_SDP_NEG_STATE_DONE) {
  1973. res = delay_request(session, on_request_creation, on_sdp_creation,
  1974. on_response, generate_new_sdp,
  1975. method == AST_SIP_SESSION_REFRESH_METHOD_INVITE
  1976. ? DELAYED_METHOD_INVITE : DELAYED_METHOD_UPDATE, pending_media_state,
  1977. active_media_state ? active_media_state : ast_sip_session_media_state_clone(session->active_media_state), queued);
  1978. SCOPE_EXIT_RTN_VALUE(res, "%s: Delay session refresh with new SDP because SDP negotiation is not yet done\n",
  1979. ast_sip_session_get_name(session));
  1980. }
  1981. /* If an explicitly requested media state has been provided use it instead of any pending one */
  1982. if (pending_media_state) {
  1983. int index;
  1984. int type_streams[AST_MEDIA_TYPE_END] = {0};
  1985. ast_trace(-1, "%s: Pending media state exists\n", ast_sip_session_get_name(session));
  1986. /* Media state conveys a desired media state, so if there are outstanding
  1987. * delayed requests we need to ensure we go into the queue and not jump
  1988. * ahead. If we sent this media state now then updates could go out of
  1989. * order.
  1990. */
  1991. if (!queued && !AST_LIST_EMPTY(&session->delayed_requests)) {
  1992. res = delay_request(session, on_request_creation, on_sdp_creation,
  1993. on_response, generate_new_sdp,
  1994. method == AST_SIP_SESSION_REFRESH_METHOD_INVITE
  1995. ? DELAYED_METHOD_INVITE : DELAYED_METHOD_UPDATE, pending_media_state,
  1996. active_media_state ? active_media_state : ast_sip_session_media_state_clone(session->active_media_state), queued);
  1997. SCOPE_EXIT_RTN_VALUE(res, "%s: Delay sending reinvite because of outstanding requests\n",
  1998. ast_sip_session_get_name(session));
  1999. }
  2000. /*
  2001. * Attempt to resolve only if objects are available, and it's not
  2002. * switching to or from an image type.
  2003. */
  2004. if (active_media_state && active_media_state->topology &&
  2005. (!active_media_state->default_session[AST_MEDIA_TYPE_IMAGE] ==
  2006. !pending_media_state->default_session[AST_MEDIA_TYPE_IMAGE])) {
  2007. struct ast_sip_session_media_state *new_pending_state;
  2008. ast_trace(-1, "%s: Active media state exists and is%s equal to pending\n", ast_sip_session_get_name(session),
  2009. !ast_stream_topology_equal(active_media_state->topology,pending_media_state->topology) ? " not" : "");
  2010. ast_trace(-1, "%s: DP: %s\n", ast_sip_session_get_name(session), ast_str_tmp(256, ast_stream_topology_to_str(pending_media_state->topology, &STR_TMP)));
  2011. ast_trace(-1, "%s: DA: %s\n", ast_sip_session_get_name(session), ast_str_tmp(256, ast_stream_topology_to_str(active_media_state->topology, &STR_TMP)));
  2012. ast_trace(-1, "%s: CP: %s\n", ast_sip_session_get_name(session), ast_str_tmp(256, ast_stream_topology_to_str(session->pending_media_state->topology, &STR_TMP)));
  2013. ast_trace(-1, "%s: CA: %s\n", ast_sip_session_get_name(session), ast_str_tmp(256, ast_stream_topology_to_str(session->active_media_state->topology, &STR_TMP)));
  2014. new_pending_state = resolve_refresh_media_states(ast_sip_session_get_name(session),
  2015. pending_media_state, active_media_state, session->active_media_state, 1);
  2016. if (new_pending_state) {
  2017. ast_trace(-1, "%s: NP: %s\n", ast_sip_session_get_name(session), ast_str_tmp(256, ast_stream_topology_to_str(new_pending_state->topology, &STR_TMP)));
  2018. ast_sip_session_media_state_free(pending_media_state);
  2019. pending_media_state = new_pending_state;
  2020. } else {
  2021. ast_sip_session_media_state_reset(pending_media_state);
  2022. ast_sip_session_media_state_free(active_media_state);
  2023. SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_WARNING, "%s: Unable to merge media states\n", ast_sip_session_get_name(session));
  2024. }
  2025. }
  2026. /* Prune the media state so the number of streams fit within the configured limits - we do it here
  2027. * so that the index of the resulting streams in the SDP match. If we simply left the streams out
  2028. * of the SDP when producing it we'd be in trouble. We also enforce formats here for media types that
  2029. * are configurable on the endpoint.
  2030. */
  2031. ast_trace(-1, "%s: Pruning and checking formats of streams\n", ast_sip_session_get_name(session));
  2032. for (index = 0; index < ast_stream_topology_get_count(pending_media_state->topology); ++index) {
  2033. struct ast_stream *existing_stream = NULL;
  2034. struct ast_stream *stream = ast_stream_topology_get_stream(pending_media_state->topology, index);
  2035. SCOPE_ENTER(4, "%s: Checking stream %s\n", ast_sip_session_get_name(session),
  2036. ast_stream_get_name(stream));
  2037. if (session->active_media_state->topology &&
  2038. index < ast_stream_topology_get_count(session->active_media_state->topology)) {
  2039. existing_stream = ast_stream_topology_get_stream(session->active_media_state->topology, index);
  2040. ast_trace(-1, "%s: Found existing stream %s\n", ast_sip_session_get_name(session),
  2041. ast_stream_get_name(existing_stream));
  2042. }
  2043. if (is_stream_limitation_reached(ast_stream_get_type(stream), session->endpoint, type_streams)) {
  2044. if (index < AST_VECTOR_SIZE(&pending_media_state->sessions)) {
  2045. struct ast_sip_session_media *session_media = AST_VECTOR_GET(&pending_media_state->sessions, index);
  2046. ao2_cleanup(session_media);
  2047. AST_VECTOR_REMOVE(&pending_media_state->sessions, index, 1);
  2048. }
  2049. ast_stream_topology_del_stream(pending_media_state->topology, index);
  2050. ast_trace(-1, "%s: Dropped overlimit stream %s\n", ast_sip_session_get_name(session),
  2051. ast_stream_get_name(stream));
  2052. /* A stream has potentially moved into our spot so we need to jump back so we process it */
  2053. index -= 1;
  2054. SCOPE_EXIT_EXPR(continue);
  2055. }
  2056. /* No need to do anything with stream if it's media state is removed */
  2057. if (ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED) {
  2058. /* If there is no existing stream we can just not have this stream in the topology at all. */
  2059. if (!existing_stream) {
  2060. ast_trace(-1, "%s: Dropped removed stream %s\n", ast_sip_session_get_name(session),
  2061. ast_stream_get_name(stream));
  2062. ast_stream_topology_del_stream(pending_media_state->topology, index);
  2063. /* TODO: Do we need to remove the corresponding media state? */
  2064. index -= 1;
  2065. }
  2066. SCOPE_EXIT_EXPR(continue);
  2067. }
  2068. /* Enforce the configured allowed codecs on audio and video streams */
  2069. if ((ast_stream_get_type(stream) == AST_MEDIA_TYPE_AUDIO || ast_stream_get_type(stream) == AST_MEDIA_TYPE_VIDEO) &&
  2070. !ast_stream_get_metadata(stream, "pjsip_session_refresh")) {
  2071. struct ast_format_cap *joint_cap;
  2072. joint_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  2073. if (!joint_cap) {
  2074. ast_sip_session_media_state_free(pending_media_state);
  2075. ast_sip_session_media_state_free(active_media_state);
  2076. res = -1;
  2077. SCOPE_EXIT_LOG_EXPR(goto end, LOG_ERROR, "%s: Unable to alloc format caps\n", ast_sip_session_get_name(session));
  2078. }
  2079. ast_format_cap_get_compatible(ast_stream_get_formats(stream), session->endpoint->media.codecs, joint_cap);
  2080. if (!ast_format_cap_count(joint_cap)) {
  2081. ao2_ref(joint_cap, -1);
  2082. if (!existing_stream) {
  2083. /* If there is no existing stream we can just not have this stream in the topology
  2084. * at all.
  2085. */
  2086. ast_stream_topology_del_stream(pending_media_state->topology, index);
  2087. index -= 1;
  2088. SCOPE_EXIT_EXPR(continue, "%s: Dropped incompatible stream %s\n",
  2089. ast_sip_session_get_name(session), ast_stream_get_name(stream));
  2090. } else if (ast_stream_get_state(stream) != ast_stream_get_state(existing_stream) ||
  2091. strcmp(ast_stream_get_name(stream), ast_stream_get_name(existing_stream))) {
  2092. /* If the underlying stream is a different type or different name then we have to
  2093. * mark it as removed, as it is replacing an existing stream. We do this so order
  2094. * is preserved.
  2095. */
  2096. ast_stream_set_state(stream, AST_STREAM_STATE_REMOVED);
  2097. SCOPE_EXIT_EXPR(continue, "%s: Dropped incompatible stream %s\n",
  2098. ast_sip_session_get_name(session), ast_stream_get_name(stream));
  2099. } else {
  2100. /* However if the stream is otherwise remaining the same we can keep the formats
  2101. * that exist on it already which allows media to continue to flow. We don't modify
  2102. * the format capabilities but do need to cast it so that ao2_bump can raise the
  2103. * reference count.
  2104. */
  2105. joint_cap = ao2_bump((struct ast_format_cap *)ast_stream_get_formats(existing_stream));
  2106. }
  2107. }
  2108. ast_stream_set_formats(stream, joint_cap);
  2109. ao2_cleanup(joint_cap);
  2110. }
  2111. ++type_streams[ast_stream_get_type(stream)];
  2112. SCOPE_EXIT();
  2113. }
  2114. if (session->active_media_state->topology) {
  2115. /* SDP is a fun thing. Take for example the fact that streams are never removed. They just become
  2116. * declined. To better handle this in the case where something requests a topology change for fewer
  2117. * streams than are currently present we fill in the topology to match the current number of streams
  2118. * that are active.
  2119. */
  2120. for (index = ast_stream_topology_get_count(pending_media_state->topology);
  2121. index < ast_stream_topology_get_count(session->active_media_state->topology); ++index) {
  2122. struct ast_stream *stream = ast_stream_topology_get_stream(session->active_media_state->topology, index);
  2123. struct ast_stream *cloned;
  2124. int position;
  2125. SCOPE_ENTER(4, "%s: Stream %s not in pending\n", ast_sip_session_get_name(session),
  2126. ast_stream_get_name(stream));
  2127. cloned = ast_stream_clone(stream, NULL);
  2128. if (!cloned) {
  2129. ast_sip_session_media_state_free(pending_media_state);
  2130. ast_sip_session_media_state_free(active_media_state);
  2131. res = -1;
  2132. SCOPE_EXIT_LOG_EXPR(goto end, LOG_ERROR, "%s: Unable to clone stream %s\n",
  2133. ast_sip_session_get_name(session), ast_stream_get_name(stream));
  2134. }
  2135. ast_stream_set_state(cloned, AST_STREAM_STATE_REMOVED);
  2136. position = ast_stream_topology_append_stream(pending_media_state->topology, cloned);
  2137. if (position < 0) {
  2138. ast_stream_free(cloned);
  2139. ast_sip_session_media_state_free(pending_media_state);
  2140. ast_sip_session_media_state_free(active_media_state);
  2141. res = -1;
  2142. SCOPE_EXIT_LOG_EXPR(goto end, LOG_ERROR, "%s: Unable to append cloned stream\n",
  2143. ast_sip_session_get_name(session));
  2144. }
  2145. SCOPE_EXIT("%s: Appended empty stream in position %d to make counts match\n",
  2146. ast_sip_session_get_name(session), position);
  2147. }
  2148. /*
  2149. * We can suppress this re-invite if the pending topology is equal to the currently
  2150. * active topology.
  2151. */
  2152. if (ast_stream_topology_equal(session->active_media_state->topology, pending_media_state->topology)) {
  2153. ast_trace(-1, "%s: CA: %s\n", ast_sip_session_get_name(session), ast_str_tmp(256, ast_stream_topology_to_str(session->active_media_state->topology, &STR_TMP)));
  2154. ast_trace(-1, "%s: NP: %s\n", ast_sip_session_get_name(session), ast_str_tmp(256, ast_stream_topology_to_str(pending_media_state->topology, &STR_TMP)));
  2155. ast_sip_session_media_state_free(pending_media_state);
  2156. ast_sip_session_media_state_free(active_media_state);
  2157. /* For external consumers we return 0 to say success, but internally for
  2158. * send_delayed_request we return a separate value to indicate that this
  2159. * session refresh would be redundant so we didn't send it
  2160. */
  2161. SCOPE_EXIT_RTN_VALUE(queued ? 1 : 0, "%s: Topologies are equal. Not sending re-invite\n",
  2162. ast_sip_session_get_name(session));
  2163. }
  2164. }
  2165. ast_sip_session_media_state_free(session->pending_media_state);
  2166. session->pending_media_state = pending_media_state;
  2167. }
  2168. new_sdp = generate_session_refresh_sdp(session);
  2169. if (!new_sdp) {
  2170. ast_sip_session_media_state_reset(session->pending_media_state);
  2171. ast_sip_session_media_state_free(active_media_state);
  2172. SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_WARNING, "%s: Failed to generate session refresh SDP. Not sending session refresh\n",
  2173. ast_sip_session_get_name(session));
  2174. }
  2175. if (on_sdp_creation) {
  2176. if (on_sdp_creation(session, new_sdp)) {
  2177. ast_sip_session_media_state_reset(session->pending_media_state);
  2178. ast_sip_session_media_state_free(active_media_state);
  2179. SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_WARNING, "%s: on_sdp_creation failed\n", ast_sip_session_get_name(session));
  2180. }
  2181. }
  2182. }
  2183. if (method == AST_SIP_SESSION_REFRESH_METHOD_INVITE) {
  2184. if (pjsip_inv_reinvite(inv_session, NULL, new_sdp, &tdata)) {
  2185. if (generate_new_sdp) {
  2186. ast_sip_session_media_state_reset(session->pending_media_state);
  2187. }
  2188. ast_sip_session_media_state_free(active_media_state);
  2189. SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_WARNING, "%s: Failed to create reinvite properly\n", ast_sip_session_get_name(session));
  2190. }
  2191. } else if (pjsip_inv_update(inv_session, NULL, new_sdp, &tdata)) {
  2192. if (generate_new_sdp) {
  2193. ast_sip_session_media_state_reset(session->pending_media_state);
  2194. }
  2195. ast_sip_session_media_state_free(active_media_state);
  2196. SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_WARNING, "%s: Failed to create UPDATE properly\n", ast_sip_session_get_name(session));
  2197. }
  2198. if (on_request_creation) {
  2199. if (on_request_creation(session, tdata)) {
  2200. if (generate_new_sdp) {
  2201. ast_sip_session_media_state_reset(session->pending_media_state);
  2202. }
  2203. ast_sip_session_media_state_free(active_media_state);
  2204. SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_WARNING, "%s: on_request_creation failed.\n", ast_sip_session_get_name(session));
  2205. }
  2206. }
  2207. ast_sip_session_send_request_with_cb(session, tdata, on_response);
  2208. ast_sip_session_media_state_free(active_media_state);
  2209. end:
  2210. SCOPE_EXIT_RTN_VALUE(res, "%s: Sending session refresh SDP via %s\n", ast_sip_session_get_name(session),
  2211. method == AST_SIP_SESSION_REFRESH_METHOD_INVITE ? "re-INVITE" : "UPDATE");
  2212. }
  2213. int ast_sip_session_refresh(struct ast_sip_session *session,
  2214. ast_sip_session_request_creation_cb on_request_creation,
  2215. ast_sip_session_sdp_creation_cb on_sdp_creation,
  2216. ast_sip_session_response_cb on_response,
  2217. enum ast_sip_session_refresh_method method, int generate_new_sdp,
  2218. struct ast_sip_session_media_state *media_state)
  2219. {
  2220. return sip_session_refresh(session, on_request_creation, on_sdp_creation,
  2221. on_response, method, generate_new_sdp, media_state, NULL, 0);
  2222. }
  2223. int ast_sip_session_regenerate_answer(struct ast_sip_session *session,
  2224. ast_sip_session_sdp_creation_cb on_sdp_creation)
  2225. {
  2226. pjsip_inv_session *inv_session = session->inv_session;
  2227. pjmedia_sdp_session *new_answer = NULL;
  2228. const pjmedia_sdp_session *previous_offer = NULL;
  2229. SCOPE_ENTER(1, "%s\n", ast_sip_session_get_name(session));
  2230. /* The SDP answer can only be regenerated if it is still pending to be sent */
  2231. if (!inv_session->neg || (pjmedia_sdp_neg_get_state(inv_session->neg) != PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER &&
  2232. pjmedia_sdp_neg_get_state(inv_session->neg) != PJMEDIA_SDP_NEG_STATE_WAIT_NEGO)) {
  2233. ast_log(LOG_WARNING, "Requested to regenerate local SDP answer for channel '%s' but negotiation in state '%s'\n",
  2234. ast_channel_name(session->channel), pjmedia_sdp_neg_state_str(pjmedia_sdp_neg_get_state(inv_session->neg)));
  2235. SCOPE_EXIT_RTN_VALUE(-1, "Bad negotiation state\n");
  2236. }
  2237. pjmedia_sdp_neg_get_neg_remote(inv_session->neg, &previous_offer);
  2238. if (pjmedia_sdp_neg_get_state(inv_session->neg) == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO) {
  2239. /* Transition the SDP negotiator back to when it received the remote offer */
  2240. pjmedia_sdp_neg_negotiate(inv_session->pool, inv_session->neg, 0);
  2241. pjmedia_sdp_neg_set_remote_offer(inv_session->pool, inv_session->neg, previous_offer);
  2242. }
  2243. new_answer = create_local_sdp(inv_session, session, previous_offer, 0);
  2244. if (!new_answer) {
  2245. ast_log(LOG_WARNING, "Could not create a new local SDP answer for channel '%s'\n",
  2246. ast_channel_name(session->channel));
  2247. SCOPE_EXIT_RTN_VALUE(-1, "Couldn't create new SDP\n");
  2248. }
  2249. if (on_sdp_creation) {
  2250. if (on_sdp_creation(session, new_answer)) {
  2251. SCOPE_EXIT_RTN_VALUE(-1, "Callback failed\n");
  2252. }
  2253. }
  2254. pjsip_inv_set_sdp_answer(inv_session, new_answer);
  2255. SCOPE_EXIT_RTN_VALUE(0);
  2256. }
  2257. void ast_sip_session_send_response(struct ast_sip_session *session, pjsip_tx_data *tdata)
  2258. {
  2259. pjsip_dialog *dlg = pjsip_tdata_get_dlg(tdata);
  2260. RAII_VAR(struct ast_sip_session *, dlg_session, dlg ? ast_sip_dialog_get_session(dlg) : NULL, ao2_cleanup);
  2261. if (!dlg_session) {
  2262. /* If the dialog has a session, handle_outgoing_response will be called
  2263. from session_on_tx_response. If it does not, call it from here. */
  2264. handle_outgoing_response(session, tdata);
  2265. }
  2266. pjsip_inv_send_msg(session->inv_session, tdata);
  2267. return;
  2268. }
  2269. static pj_bool_t session_on_rx_request(pjsip_rx_data *rdata);
  2270. static pj_bool_t session_on_rx_response(pjsip_rx_data *rdata);
  2271. static pj_status_t session_on_tx_response(pjsip_tx_data *tdata);
  2272. static void session_on_tsx_state(pjsip_transaction *tsx, pjsip_event *e);
  2273. static pjsip_module session_module = {
  2274. .name = {"Session Module", 14},
  2275. .priority = PJSIP_MOD_PRIORITY_APPLICATION,
  2276. .on_rx_request = session_on_rx_request,
  2277. .on_rx_response = session_on_rx_response,
  2278. .on_tsx_state = session_on_tsx_state,
  2279. .on_tx_response = session_on_tx_response,
  2280. };
  2281. /*! \brief Determine whether the SDP provided requires deferral of negotiating or not
  2282. *
  2283. * \retval 1 re-invite should be deferred and resumed later
  2284. * \retval 0 re-invite should not be deferred
  2285. */
  2286. static int sdp_requires_deferral(struct ast_sip_session *session, const pjmedia_sdp_session *sdp)
  2287. {
  2288. int i;
  2289. if (!session->pending_media_state->topology) {
  2290. session->pending_media_state->topology = ast_stream_topology_alloc();
  2291. if (!session->pending_media_state->topology) {
  2292. return -1;
  2293. }
  2294. }
  2295. for (i = 0; i < sdp->media_count; ++i) {
  2296. /* See if there are registered handlers for this media stream type */
  2297. char media[20];
  2298. struct ast_sip_session_sdp_handler *handler;
  2299. RAII_VAR(struct sdp_handler_list *, handler_list, NULL, ao2_cleanup);
  2300. struct ast_stream *existing_stream = NULL;
  2301. struct ast_stream *stream;
  2302. enum ast_media_type type;
  2303. struct ast_sip_session_media *session_media = NULL;
  2304. enum ast_sip_session_sdp_stream_defer res;
  2305. pjmedia_sdp_media *remote_stream = sdp->media[i];
  2306. /* We need a null-terminated version of the media string */
  2307. ast_copy_pj_str(media, &sdp->media[i]->desc.media, sizeof(media));
  2308. if (session->active_media_state->topology &&
  2309. (i < ast_stream_topology_get_count(session->active_media_state->topology))) {
  2310. existing_stream = ast_stream_topology_get_stream(session->active_media_state->topology, i);
  2311. }
  2312. type = ast_media_type_from_str(media);
  2313. stream = ast_stream_alloc(existing_stream ? ast_stream_get_name(existing_stream) : ast_codec_media_type2str(type), type);
  2314. if (!stream) {
  2315. return -1;
  2316. }
  2317. /* As this is only called on an incoming SDP offer before processing it is not possible
  2318. * for streams and their media sessions to exist.
  2319. */
  2320. if (ast_stream_topology_set_stream(session->pending_media_state->topology, i, stream)) {
  2321. ast_stream_free(stream);
  2322. return -1;
  2323. }
  2324. if (existing_stream) {
  2325. const char *stream_label = ast_stream_get_metadata(existing_stream, "SDP:LABEL");
  2326. if (!ast_strlen_zero(stream_label)) {
  2327. ast_stream_set_metadata(stream, "SDP:LABEL", stream_label);
  2328. }
  2329. }
  2330. session_media = ast_sip_session_media_state_add(session, session->pending_media_state, ast_media_type_from_str(media), i);
  2331. if (!session_media) {
  2332. return -1;
  2333. }
  2334. /* For backwards compatibility with the core the default audio stream is always sendrecv */
  2335. if (!ast_sip_session_is_pending_stream_default(session, stream) || strcmp(media, "audio")) {
  2336. if (pjmedia_sdp_media_find_attr2(remote_stream, "sendonly", NULL)) {
  2337. /* Stream state reflects our state of a stream, so in the case of
  2338. * sendonly and recvonly we store the opposite since that is what ours
  2339. * is.
  2340. */
  2341. ast_stream_set_state(stream, AST_STREAM_STATE_RECVONLY);
  2342. } else if (pjmedia_sdp_media_find_attr2(remote_stream, "recvonly", NULL)) {
  2343. ast_stream_set_state(stream, AST_STREAM_STATE_SENDONLY);
  2344. } else if (pjmedia_sdp_media_find_attr2(remote_stream, "inactive", NULL)) {
  2345. ast_stream_set_state(stream, AST_STREAM_STATE_INACTIVE);
  2346. } else {
  2347. ast_stream_set_state(stream, AST_STREAM_STATE_SENDRECV);
  2348. }
  2349. } else {
  2350. ast_stream_set_state(stream, AST_STREAM_STATE_SENDRECV);
  2351. }
  2352. if (session_media->handler) {
  2353. handler = session_media->handler;
  2354. if (handler->defer_incoming_sdp_stream) {
  2355. res = handler->defer_incoming_sdp_stream(session, session_media, sdp,
  2356. sdp->media[i]);
  2357. switch (res) {
  2358. case AST_SIP_SESSION_SDP_DEFER_NOT_HANDLED:
  2359. break;
  2360. case AST_SIP_SESSION_SDP_DEFER_ERROR:
  2361. return 0;
  2362. case AST_SIP_SESSION_SDP_DEFER_NOT_NEEDED:
  2363. break;
  2364. case AST_SIP_SESSION_SDP_DEFER_NEEDED:
  2365. return 1;
  2366. }
  2367. }
  2368. /* Handled by this handler. Move to the next stream */
  2369. continue;
  2370. }
  2371. handler_list = ao2_find(sdp_handlers, media, OBJ_KEY);
  2372. if (!handler_list) {
  2373. ast_debug(3, "%s: No registered SDP handlers for media type '%s'\n", ast_sip_session_get_name(session), media);
  2374. continue;
  2375. }
  2376. AST_LIST_TRAVERSE(&handler_list->list, handler, next) {
  2377. if (handler == session_media->handler) {
  2378. continue;
  2379. }
  2380. if (!handler->defer_incoming_sdp_stream) {
  2381. continue;
  2382. }
  2383. res = handler->defer_incoming_sdp_stream(session, session_media, sdp,
  2384. sdp->media[i]);
  2385. switch (res) {
  2386. case AST_SIP_SESSION_SDP_DEFER_NOT_HANDLED:
  2387. continue;
  2388. case AST_SIP_SESSION_SDP_DEFER_ERROR:
  2389. session_media_set_handler(session_media, handler);
  2390. return 0;
  2391. case AST_SIP_SESSION_SDP_DEFER_NOT_NEEDED:
  2392. /* Handled by this handler. */
  2393. session_media_set_handler(session_media, handler);
  2394. break;
  2395. case AST_SIP_SESSION_SDP_DEFER_NEEDED:
  2396. /* Handled by this handler. */
  2397. session_media_set_handler(session_media, handler);
  2398. return 1;
  2399. }
  2400. /* Move to the next stream */
  2401. break;
  2402. }
  2403. }
  2404. return 0;
  2405. }
  2406. static pj_bool_t session_reinvite_on_rx_request(pjsip_rx_data *rdata)
  2407. {
  2408. pjsip_dialog *dlg;
  2409. RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
  2410. pjsip_rdata_sdp_info *sdp_info;
  2411. int deferred;
  2412. if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD ||
  2413. !(dlg = pjsip_ua_find_dialog(&rdata->msg_info.cid->id, &rdata->msg_info.to->tag, &rdata->msg_info.from->tag, PJ_FALSE)) ||
  2414. !(session = ast_sip_dialog_get_session(dlg)) ||
  2415. !session->channel) {
  2416. return PJ_FALSE;
  2417. }
  2418. if (session->inv_session->invite_tsx) {
  2419. /* There's a transaction in progress so bail now and let pjproject send 491 */
  2420. return PJ_FALSE;
  2421. }
  2422. if (session->deferred_reinvite) {
  2423. pj_str_t key, deferred_key;
  2424. pjsip_tx_data *tdata;
  2425. /* We use memory from the new request on purpose so the deferred reinvite pool does not grow uncontrollably */
  2426. pjsip_tsx_create_key(rdata->tp_info.pool, &key, PJSIP_ROLE_UAS, &rdata->msg_info.cseq->method, rdata);
  2427. pjsip_tsx_create_key(rdata->tp_info.pool, &deferred_key, PJSIP_ROLE_UAS, &session->deferred_reinvite->msg_info.cseq->method,
  2428. session->deferred_reinvite);
  2429. /* If this is a retransmission ignore it */
  2430. if (!pj_strcmp(&key, &deferred_key)) {
  2431. return PJ_TRUE;
  2432. }
  2433. /* Otherwise this is a new re-invite, so reject it */
  2434. if (pjsip_dlg_create_response(dlg, rdata, 491, NULL, &tdata) == PJ_SUCCESS) {
  2435. if (pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL) != PJ_SUCCESS) {
  2436. pjsip_tx_data_dec_ref(tdata);
  2437. }
  2438. }
  2439. return PJ_TRUE;
  2440. }
  2441. if (!(sdp_info = pjsip_rdata_get_sdp_info(rdata)) ||
  2442. (sdp_info->sdp_err != PJ_SUCCESS)) {
  2443. return PJ_FALSE;
  2444. }
  2445. if (!sdp_info->sdp) {
  2446. return PJ_FALSE;
  2447. }
  2448. deferred = sdp_requires_deferral(session, sdp_info->sdp);
  2449. if (deferred == -1) {
  2450. ast_sip_session_media_state_reset(session->pending_media_state);
  2451. return PJ_FALSE;
  2452. } else if (!deferred) {
  2453. return PJ_FALSE;
  2454. }
  2455. pjsip_rx_data_clone(rdata, 0, &session->deferred_reinvite);
  2456. return PJ_TRUE;
  2457. }
  2458. void ast_sip_session_resume_reinvite(struct ast_sip_session *session)
  2459. {
  2460. if (!session->deferred_reinvite) {
  2461. return;
  2462. }
  2463. if (session->channel) {
  2464. pjsip_endpt_process_rx_data(ast_sip_get_pjsip_endpoint(),
  2465. session->deferred_reinvite, NULL, NULL);
  2466. }
  2467. pjsip_rx_data_free_cloned(session->deferred_reinvite);
  2468. session->deferred_reinvite = NULL;
  2469. }
  2470. static pjsip_module session_reinvite_module = {
  2471. .name = { "Session Re-Invite Module", 24 },
  2472. .priority = PJSIP_MOD_PRIORITY_UA_PROXY_LAYER - 1,
  2473. .on_rx_request = session_reinvite_on_rx_request,
  2474. };
  2475. void ast_sip_session_send_request_with_cb(struct ast_sip_session *session, pjsip_tx_data *tdata,
  2476. ast_sip_session_response_cb on_response)
  2477. {
  2478. pjsip_inv_session *inv_session = session->inv_session;
  2479. /* For every request except BYE we disallow sending of the message when
  2480. * the session has been disconnected. A BYE request is special though
  2481. * because it can be sent again after the session is disconnected except
  2482. * with credentials.
  2483. */
  2484. if (inv_session->state == PJSIP_INV_STATE_DISCONNECTED &&
  2485. tdata->msg->line.req.method.id != PJSIP_BYE_METHOD) {
  2486. return;
  2487. }
  2488. ast_sip_mod_data_set(tdata->pool, tdata->mod_data, session_module.id,
  2489. MOD_DATA_ON_RESPONSE, on_response);
  2490. handle_outgoing_request(session, tdata);
  2491. pjsip_inv_send_msg(session->inv_session, tdata);
  2492. return;
  2493. }
  2494. void ast_sip_session_send_request(struct ast_sip_session *session, pjsip_tx_data *tdata)
  2495. {
  2496. ast_sip_session_send_request_with_cb(session, tdata, NULL);
  2497. }
  2498. int ast_sip_session_create_invite(struct ast_sip_session *session, pjsip_tx_data **tdata)
  2499. {
  2500. pjmedia_sdp_session *offer;
  2501. SCOPE_ENTER(1, "%s\n", ast_sip_session_get_name(session));
  2502. if (!(offer = create_local_sdp(session->inv_session, session, NULL, 0))) {
  2503. pjsip_inv_terminate(session->inv_session, 500, PJ_FALSE);
  2504. SCOPE_EXIT_RTN_VALUE(-1, "Couldn't create offer\n");
  2505. }
  2506. pjsip_inv_set_local_sdp(session->inv_session, offer);
  2507. pjmedia_sdp_neg_set_prefer_remote_codec_order(session->inv_session->neg, PJ_FALSE);
  2508. #ifdef PJMEDIA_SDP_NEG_ANSWER_MULTIPLE_CODECS
  2509. if (!session->endpoint->preferred_codec_only) {
  2510. pjmedia_sdp_neg_set_answer_multiple_codecs(session->inv_session->neg, PJ_TRUE);
  2511. }
  2512. #endif
  2513. /*
  2514. * We MUST call set_from_header() before pjsip_inv_invite. If we don't, the
  2515. * From in the initial INVITE will be wrong but the rest of the messages will be OK.
  2516. */
  2517. set_from_header(session);
  2518. if (pjsip_inv_invite(session->inv_session, tdata) != PJ_SUCCESS) {
  2519. SCOPE_EXIT_RTN_VALUE(-1, "pjsip_inv_invite failed\n");
  2520. }
  2521. SCOPE_EXIT_RTN_VALUE(0);
  2522. }
  2523. static int datastore_hash(const void *obj, int flags)
  2524. {
  2525. const struct ast_datastore *datastore = obj;
  2526. const char *uid = flags & OBJ_KEY ? obj : datastore->uid;
  2527. ast_assert(uid != NULL);
  2528. return ast_str_hash(uid);
  2529. }
  2530. static int datastore_cmp(void *obj, void *arg, int flags)
  2531. {
  2532. const struct ast_datastore *datastore1 = obj;
  2533. const struct ast_datastore *datastore2 = arg;
  2534. const char *uid2 = flags & OBJ_KEY ? arg : datastore2->uid;
  2535. ast_assert(datastore1->uid != NULL);
  2536. ast_assert(uid2 != NULL);
  2537. return strcmp(datastore1->uid, uid2) ? 0 : CMP_MATCH | CMP_STOP;
  2538. }
  2539. static void session_destructor(void *obj)
  2540. {
  2541. struct ast_sip_session *session = obj;
  2542. struct ast_sip_session_delayed_request *delay;
  2543. #ifdef TEST_FRAMEWORK
  2544. /* We dup the endpoint ID in case the endpoint gets freed out from under us */
  2545. const char *endpoint_name = session->endpoint ?
  2546. ast_strdupa(ast_sorcery_object_get_id(session->endpoint)) : "<none>";
  2547. #endif
  2548. ast_debug(3, "%s: Destroying SIP session\n", ast_sip_session_get_name(session));
  2549. ast_test_suite_event_notify("SESSION_DESTROYING",
  2550. "Endpoint: %s\r\n"
  2551. "AOR: %s\r\n"
  2552. "Contact: %s"
  2553. , endpoint_name
  2554. , session->aor ? ast_sorcery_object_get_id(session->aor) : "<none>"
  2555. , session->contact ? ast_sorcery_object_get_id(session->contact) : "<none>"
  2556. );
  2557. /* fire session destroy handler */
  2558. handle_session_destroy(session);
  2559. /* remove all registered supplements */
  2560. ast_sip_session_remove_supplements(session);
  2561. AST_LIST_HEAD_DESTROY(&session->supplements);
  2562. /* remove all saved media stats */
  2563. AST_VECTOR_RESET(&session->media_stats, ast_free);
  2564. AST_VECTOR_FREE(&session->media_stats);
  2565. ast_taskprocessor_unreference(session->serializer);
  2566. ao2_cleanup(session->datastores);
  2567. ast_sip_session_media_state_free(session->active_media_state);
  2568. ast_sip_session_media_state_free(session->pending_media_state);
  2569. while ((delay = AST_LIST_REMOVE_HEAD(&session->delayed_requests, next))) {
  2570. delayed_request_free(delay);
  2571. }
  2572. ast_party_id_free(&session->id);
  2573. ao2_cleanup(session->endpoint);
  2574. ao2_cleanup(session->aor);
  2575. ao2_cleanup(session->contact);
  2576. ao2_cleanup(session->direct_media_cap);
  2577. ast_dsp_free(session->dsp);
  2578. if (session->inv_session) {
  2579. struct pjsip_dialog *dlg = session->inv_session->dlg;
  2580. /* The INVITE session uses the dialog pool for memory, so we need to
  2581. * decrement its reference first before that of the dialog.
  2582. */
  2583. #ifdef HAVE_PJSIP_INV_SESSION_REF
  2584. pjsip_inv_dec_ref(session->inv_session);
  2585. #endif
  2586. pjsip_dlg_dec_session(dlg, &session_module);
  2587. }
  2588. ast_test_suite_event_notify("SESSION_DESTROYED", "Endpoint: %s", endpoint_name);
  2589. }
  2590. /*! \brief Destructor for SIP channel */
  2591. static void sip_channel_destroy(void *obj)
  2592. {
  2593. struct ast_sip_channel_pvt *channel = obj;
  2594. ao2_cleanup(channel->pvt);
  2595. ao2_cleanup(channel->session);
  2596. }
  2597. struct ast_sip_channel_pvt *ast_sip_channel_pvt_alloc(void *pvt, struct ast_sip_session *session)
  2598. {
  2599. struct ast_sip_channel_pvt *channel = ao2_alloc(sizeof(*channel), sip_channel_destroy);
  2600. if (!channel) {
  2601. return NULL;
  2602. }
  2603. ao2_ref(pvt, +1);
  2604. channel->pvt = pvt;
  2605. ao2_ref(session, +1);
  2606. channel->session = session;
  2607. return channel;
  2608. }
  2609. struct ast_sip_session *ast_sip_session_alloc(struct ast_sip_endpoint *endpoint,
  2610. struct ast_sip_contact *contact, pjsip_inv_session *inv_session, pjsip_rx_data *rdata)
  2611. {
  2612. RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
  2613. struct ast_sip_session *ret_session;
  2614. int dsp_features = 0;
  2615. session = ao2_alloc(sizeof(*session), session_destructor);
  2616. if (!session) {
  2617. return NULL;
  2618. }
  2619. AST_LIST_HEAD_INIT(&session->supplements);
  2620. AST_LIST_HEAD_INIT_NOLOCK(&session->delayed_requests);
  2621. ast_party_id_init(&session->id);
  2622. session->direct_media_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  2623. if (!session->direct_media_cap) {
  2624. return NULL;
  2625. }
  2626. session->datastores = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  2627. DATASTORE_BUCKETS, datastore_hash, NULL, datastore_cmp);
  2628. if (!session->datastores) {
  2629. return NULL;
  2630. }
  2631. session->active_media_state = ast_sip_session_media_state_alloc();
  2632. if (!session->active_media_state) {
  2633. return NULL;
  2634. }
  2635. session->pending_media_state = ast_sip_session_media_state_alloc();
  2636. if (!session->pending_media_state) {
  2637. return NULL;
  2638. }
  2639. if (AST_VECTOR_INIT(&session->media_stats, 1) < 0) {
  2640. return NULL;
  2641. }
  2642. if (endpoint->dtmf == AST_SIP_DTMF_INBAND || endpoint->dtmf == AST_SIP_DTMF_AUTO) {
  2643. dsp_features |= DSP_FEATURE_DIGIT_DETECT;
  2644. }
  2645. if (endpoint->faxdetect) {
  2646. dsp_features |= DSP_FEATURE_FAX_DETECT;
  2647. }
  2648. if (dsp_features) {
  2649. session->dsp = ast_dsp_new();
  2650. if (!session->dsp) {
  2651. return NULL;
  2652. }
  2653. ast_dsp_set_features(session->dsp, dsp_features);
  2654. }
  2655. session->endpoint = ao2_bump(endpoint);
  2656. if (rdata) {
  2657. /*
  2658. * We must continue using the serializer that the original
  2659. * INVITE came in on for the dialog. There may be
  2660. * retransmissions already enqueued in the original
  2661. * serializer that can result in reentrancy and message
  2662. * sequencing problems.
  2663. */
  2664. session->serializer = ast_sip_get_distributor_serializer(rdata);
  2665. } else {
  2666. char tps_name[AST_TASKPROCESSOR_MAX_NAME + 1];
  2667. /* Create name with seq number appended. */
  2668. ast_taskprocessor_build_name(tps_name, sizeof(tps_name), "pjsip/outsess/%s",
  2669. ast_sorcery_object_get_id(endpoint));
  2670. session->serializer = ast_sip_create_serializer(tps_name);
  2671. }
  2672. if (!session->serializer) {
  2673. return NULL;
  2674. }
  2675. ast_sip_dialog_set_serializer(inv_session->dlg, session->serializer);
  2676. ast_sip_dialog_set_endpoint(inv_session->dlg, endpoint);
  2677. /* When a PJSIP INVITE session is created it is created with a reference
  2678. * count of 1, with that reference being managed by the underlying state
  2679. * of the INVITE session itself. When the INVITE session transitions to
  2680. * a DISCONNECTED state that reference is released. This means we can not
  2681. * rely on that reference to ensure the INVITE session remains for the
  2682. * lifetime of our session. To ensure it does we add our own reference
  2683. * and release it when our own session goes away, ensuring that the INVITE
  2684. * session remains for the lifetime of session.
  2685. */
  2686. #ifdef HAVE_PJSIP_INV_SESSION_REF
  2687. if (pjsip_inv_add_ref(inv_session) != PJ_SUCCESS) {
  2688. ast_log(LOG_ERROR, "Can't increase the session reference counter\n");
  2689. return NULL;
  2690. }
  2691. #endif
  2692. pjsip_dlg_inc_session(inv_session->dlg, &session_module);
  2693. inv_session->mod_data[session_module.id] = ao2_bump(session);
  2694. session->contact = ao2_bump(contact);
  2695. session->inv_session = inv_session;
  2696. session->dtmf = endpoint->dtmf;
  2697. session->moh_passthrough = endpoint->moh_passthrough;
  2698. if (ast_sip_session_add_supplements(session)) {
  2699. /* Release the ref held by session->inv_session */
  2700. ao2_ref(session, -1);
  2701. return NULL;
  2702. }
  2703. session->authentication_challenge_count = 0;
  2704. /* Fire session begin handlers */
  2705. handle_session_begin(session);
  2706. /* Avoid unnecessary ref manipulation to return a session */
  2707. ret_session = session;
  2708. session = NULL;
  2709. return ret_session;
  2710. }
  2711. /*! \brief struct controlling the suspension of the session's serializer. */
  2712. struct ast_sip_session_suspender {
  2713. ast_cond_t cond_suspended;
  2714. ast_cond_t cond_complete;
  2715. int suspended;
  2716. int complete;
  2717. };
  2718. static void sip_session_suspender_dtor(void *vdoomed)
  2719. {
  2720. struct ast_sip_session_suspender *doomed = vdoomed;
  2721. ast_cond_destroy(&doomed->cond_suspended);
  2722. ast_cond_destroy(&doomed->cond_complete);
  2723. }
  2724. /*!
  2725. * \internal
  2726. * \brief Block the session serializer thread task.
  2727. *
  2728. * \param data Pushed serializer task data for suspension.
  2729. *
  2730. * \retval 0
  2731. */
  2732. static int sip_session_suspend_task(void *data)
  2733. {
  2734. struct ast_sip_session_suspender *suspender = data;
  2735. ao2_lock(suspender);
  2736. /* Signal that the serializer task is now suspended. */
  2737. suspender->suspended = 1;
  2738. ast_cond_signal(&suspender->cond_suspended);
  2739. /* Wait for the serializer suspension to be completed. */
  2740. while (!suspender->complete) {
  2741. ast_cond_wait(&suspender->cond_complete, ao2_object_get_lockaddr(suspender));
  2742. }
  2743. ao2_unlock(suspender);
  2744. ao2_ref(suspender, -1);
  2745. return 0;
  2746. }
  2747. void ast_sip_session_suspend(struct ast_sip_session *session)
  2748. {
  2749. struct ast_sip_session_suspender *suspender;
  2750. int res;
  2751. ast_assert(session->suspended == NULL);
  2752. if (ast_taskprocessor_is_task(session->serializer)) {
  2753. /* I am the session's serializer thread so I cannot suspend. */
  2754. return;
  2755. }
  2756. if (ast_taskprocessor_is_suspended(session->serializer)) {
  2757. /* The serializer already suspended. */
  2758. return;
  2759. }
  2760. suspender = ao2_alloc(sizeof(*suspender), sip_session_suspender_dtor);
  2761. if (!suspender) {
  2762. /* We will just have to hope that the system does not deadlock */
  2763. return;
  2764. }
  2765. ast_cond_init(&suspender->cond_suspended, NULL);
  2766. ast_cond_init(&suspender->cond_complete, NULL);
  2767. ao2_ref(suspender, +1);
  2768. res = ast_sip_push_task(session->serializer, sip_session_suspend_task, suspender);
  2769. if (res) {
  2770. /* We will just have to hope that the system does not deadlock */
  2771. ao2_ref(suspender, -2);
  2772. return;
  2773. }
  2774. session->suspended = suspender;
  2775. /* Wait for the serializer to get suspended. */
  2776. ao2_lock(suspender);
  2777. while (!suspender->suspended) {
  2778. ast_cond_wait(&suspender->cond_suspended, ao2_object_get_lockaddr(suspender));
  2779. }
  2780. ao2_unlock(suspender);
  2781. ast_taskprocessor_suspend(session->serializer);
  2782. }
  2783. void ast_sip_session_unsuspend(struct ast_sip_session *session)
  2784. {
  2785. struct ast_sip_session_suspender *suspender = session->suspended;
  2786. if (!suspender) {
  2787. /* Nothing to do */
  2788. return;
  2789. }
  2790. session->suspended = NULL;
  2791. /* Signal that the serializer task suspension is now complete. */
  2792. ao2_lock(suspender);
  2793. suspender->complete = 1;
  2794. ast_cond_signal(&suspender->cond_complete);
  2795. ao2_unlock(suspender);
  2796. ao2_ref(suspender, -1);
  2797. ast_taskprocessor_unsuspend(session->serializer);
  2798. }
  2799. /*!
  2800. * \internal
  2801. * \brief Handle initial INVITE challenge response message.
  2802. * \since 13.5.0
  2803. *
  2804. * \param rdata PJSIP receive response message data.
  2805. *
  2806. * \retval PJ_FALSE Did not handle message.
  2807. * \retval PJ_TRUE Handled message.
  2808. */
  2809. static pj_bool_t outbound_invite_auth(pjsip_rx_data *rdata)
  2810. {
  2811. pjsip_transaction *tsx;
  2812. pjsip_dialog *dlg;
  2813. pjsip_inv_session *inv;
  2814. pjsip_tx_data *tdata;
  2815. struct ast_sip_session *session;
  2816. if (rdata->msg_info.msg->line.status.code != 401
  2817. && rdata->msg_info.msg->line.status.code != 407) {
  2818. /* Doesn't pertain to us. Move on */
  2819. return PJ_FALSE;
  2820. }
  2821. tsx = pjsip_rdata_get_tsx(rdata);
  2822. dlg = pjsip_rdata_get_dlg(rdata);
  2823. if (!dlg || !tsx) {
  2824. return PJ_FALSE;
  2825. }
  2826. if (tsx->method.id != PJSIP_INVITE_METHOD) {
  2827. /* Not an INVITE that needs authentication */
  2828. return PJ_FALSE;
  2829. }
  2830. inv = pjsip_dlg_get_inv_session(dlg);
  2831. session = inv->mod_data[session_module.id];
  2832. if (PJSIP_INV_STATE_CONFIRMED <= inv->state) {
  2833. /*
  2834. * We cannot handle reINVITE authentication at this
  2835. * time because the reINVITE transaction is still in
  2836. * progress.
  2837. */
  2838. ast_debug(3, "%s: A reINVITE is being challenged\n", ast_sip_session_get_name(session));
  2839. return PJ_FALSE;
  2840. }
  2841. ast_debug(3, "%s: Initial INVITE is being challenged.\n", ast_sip_session_get_name(session));
  2842. if (++session->authentication_challenge_count > MAX_RX_CHALLENGES) {
  2843. ast_debug(3, "%s: Initial INVITE reached maximum number of auth attempts.\n", ast_sip_session_get_name(session));
  2844. return PJ_FALSE;
  2845. }
  2846. if (ast_sip_create_request_with_auth(&session->endpoint->outbound_auths, rdata,
  2847. tsx->last_tx, &tdata)) {
  2848. return PJ_FALSE;
  2849. }
  2850. /*
  2851. * Restart the outgoing initial INVITE transaction to deal
  2852. * with authentication.
  2853. */
  2854. pjsip_inv_uac_restart(inv, PJ_FALSE);
  2855. ast_sip_session_send_request(session, tdata);
  2856. return PJ_TRUE;
  2857. }
  2858. static pjsip_module outbound_invite_auth_module = {
  2859. .name = {"Outbound INVITE Auth", 20},
  2860. .priority = PJSIP_MOD_PRIORITY_DIALOG_USAGE,
  2861. .on_rx_response = outbound_invite_auth,
  2862. };
  2863. /*!
  2864. * \internal
  2865. * \brief Setup outbound initial INVITE authentication.
  2866. * \since 13.5.0
  2867. *
  2868. * \param dlg PJSIP dialog to attach outbound authentication.
  2869. *
  2870. * \retval 0 on success.
  2871. * \retval -1 on error.
  2872. */
  2873. static int setup_outbound_invite_auth(pjsip_dialog *dlg)
  2874. {
  2875. pj_status_t status;
  2876. ++dlg->sess_count;
  2877. status = pjsip_dlg_add_usage(dlg, &outbound_invite_auth_module, NULL);
  2878. --dlg->sess_count;
  2879. return status != PJ_SUCCESS ? -1 : 0;
  2880. }
  2881. struct ast_sip_session *ast_sip_session_create_outgoing(struct ast_sip_endpoint *endpoint,
  2882. struct ast_sip_contact *contact, const char *location, const char *request_user,
  2883. struct ast_stream_topology *req_topology)
  2884. {
  2885. const char *uri = NULL;
  2886. RAII_VAR(struct ast_sip_aor *, found_aor, NULL, ao2_cleanup);
  2887. RAII_VAR(struct ast_sip_contact *, found_contact, NULL, ao2_cleanup);
  2888. pjsip_timer_setting timer;
  2889. pjsip_dialog *dlg;
  2890. struct pjsip_inv_session *inv_session;
  2891. RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
  2892. struct ast_sip_session *ret_session;
  2893. SCOPE_ENTER(1, "%s %s Topology: %s\n", ast_sorcery_object_get_id(endpoint), request_user,
  2894. ast_str_tmp(256, ast_stream_topology_to_str(req_topology, &STR_TMP)));
  2895. /* If no location has been provided use the AOR list from the endpoint itself */
  2896. if (location || !contact) {
  2897. location = S_OR(location, endpoint->aors);
  2898. ast_sip_location_retrieve_contact_and_aor_from_list_filtered(location, AST_SIP_CONTACT_FILTER_REACHABLE,
  2899. &found_aor, &found_contact);
  2900. if (!found_contact || ast_strlen_zero(found_contact->uri)) {
  2901. uri = location;
  2902. } else {
  2903. uri = found_contact->uri;
  2904. }
  2905. } else {
  2906. uri = contact->uri;
  2907. }
  2908. /* If we still have no URI to dial fail to create the session */
  2909. if (ast_strlen_zero(uri)) {
  2910. ast_log(LOG_ERROR, "Endpoint '%s': No URI available. Is endpoint registered?\n",
  2911. ast_sorcery_object_get_id(endpoint));
  2912. SCOPE_EXIT_RTN_VALUE(NULL, "No URI\n");
  2913. }
  2914. if (!(dlg = ast_sip_create_dialog_uac(endpoint, uri, request_user))) {
  2915. SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't create dialog\n");
  2916. }
  2917. if (setup_outbound_invite_auth(dlg)) {
  2918. pjsip_dlg_terminate(dlg);
  2919. SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't setup auth\n");
  2920. }
  2921. if (pjsip_inv_create_uac(dlg, NULL, endpoint->extensions.flags, &inv_session) != PJ_SUCCESS) {
  2922. pjsip_dlg_terminate(dlg);
  2923. SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't create uac\n");
  2924. }
  2925. #if defined(HAVE_PJSIP_REPLACE_MEDIA_STREAM) || defined(PJMEDIA_SDP_NEG_ALLOW_MEDIA_CHANGE)
  2926. inv_session->sdp_neg_flags = PJMEDIA_SDP_NEG_ALLOW_MEDIA_CHANGE;
  2927. #endif
  2928. pjsip_timer_setting_default(&timer);
  2929. timer.min_se = endpoint->extensions.timer.min_se;
  2930. timer.sess_expires = endpoint->extensions.timer.sess_expires;
  2931. pjsip_timer_init_session(inv_session, &timer);
  2932. session = ast_sip_session_alloc(endpoint, found_contact ? found_contact : contact,
  2933. inv_session, NULL);
  2934. if (!session) {
  2935. pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
  2936. return NULL;
  2937. }
  2938. session->aor = ao2_bump(found_aor);
  2939. session->call_direction = AST_SIP_SESSION_OUTGOING_CALL;
  2940. ast_party_id_copy(&session->id, &endpoint->id.self);
  2941. if (ast_stream_topology_get_count(req_topology) > 0) {
  2942. /* get joint caps between req_topology and endpoint topology */
  2943. int i;
  2944. for (i = 0; i < ast_stream_topology_get_count(req_topology); ++i) {
  2945. struct ast_stream *req_stream;
  2946. struct ast_stream *clone_stream;
  2947. req_stream = ast_stream_topology_get_stream(req_topology, i);
  2948. if (ast_stream_get_state(req_stream) == AST_STREAM_STATE_REMOVED) {
  2949. continue;
  2950. }
  2951. clone_stream = ast_sip_session_create_joint_call_stream(session, req_stream);
  2952. if (!clone_stream || ast_stream_get_format_count(clone_stream) == 0) {
  2953. ast_stream_free(clone_stream);
  2954. continue;
  2955. }
  2956. if (!session->pending_media_state->topology) {
  2957. session->pending_media_state->topology = ast_stream_topology_alloc();
  2958. if (!session->pending_media_state->topology) {
  2959. pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
  2960. ao2_ref(session, -1);
  2961. SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't create topology\n");
  2962. }
  2963. }
  2964. if (ast_stream_topology_append_stream(session->pending_media_state->topology, clone_stream) < 0) {
  2965. ast_stream_free(clone_stream);
  2966. continue;
  2967. }
  2968. }
  2969. }
  2970. if (!session->pending_media_state->topology) {
  2971. /* Use the configured topology on the endpoint as the pending one */
  2972. session->pending_media_state->topology = ast_stream_topology_clone(endpoint->media.topology);
  2973. if (!session->pending_media_state->topology) {
  2974. pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
  2975. ao2_ref(session, -1);
  2976. SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't clone topology\n");
  2977. }
  2978. }
  2979. if (pjsip_dlg_add_usage(dlg, &session_module, NULL) != PJ_SUCCESS) {
  2980. pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
  2981. /* Since we are not notifying ourselves that the INVITE session is being terminated
  2982. * we need to manually drop its reference to session
  2983. */
  2984. ao2_ref(session, -1);
  2985. SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't add usage\n");
  2986. }
  2987. /* Avoid unnecessary ref manipulation to return a session */
  2988. ret_session = session;
  2989. session = NULL;
  2990. SCOPE_EXIT_RTN_VALUE(ret_session);
  2991. }
  2992. static int session_end(void *vsession);
  2993. static int session_end_completion(void *vsession);
  2994. void ast_sip_session_terminate(struct ast_sip_session *session, int response)
  2995. {
  2996. pj_status_t status;
  2997. pjsip_tx_data *packet = NULL;
  2998. SCOPE_ENTER(1, "%s Response %d\n", ast_sip_session_get_name(session), response);
  2999. if (session->defer_terminate) {
  3000. session->terminate_while_deferred = 1;
  3001. SCOPE_EXIT_RTN("Deferred\n");
  3002. }
  3003. if (!response) {
  3004. response = 603;
  3005. }
  3006. /* The media sessions need to exist for the lifetime of the underlying channel
  3007. * to ensure that anything (such as bridge_native_rtp) has access to them as
  3008. * appropriate. Since ast_sip_session_terminate is called by chan_pjsip and other
  3009. * places when the session is to be terminated we terminate any existing
  3010. * media sessions here.
  3011. */
  3012. ast_sip_session_media_stats_save(session, session->active_media_state);
  3013. SWAP(session->active_media_state, session->pending_media_state);
  3014. ast_sip_session_media_state_reset(session->pending_media_state);
  3015. switch (session->inv_session->state) {
  3016. case PJSIP_INV_STATE_NULL:
  3017. if (!session->inv_session->invite_tsx) {
  3018. /*
  3019. * Normally, it's pjproject's transaction cleanup that ultimately causes the
  3020. * final session reference to be released but if both STATE and invite_tsx are NULL,
  3021. * we never created a transaction in the first place. In this case, we need to
  3022. * do the cleanup ourselves.
  3023. */
  3024. /* Transfer the inv_session session reference to the session_end_task */
  3025. session->inv_session->mod_data[session_module.id] = NULL;
  3026. pjsip_inv_terminate(session->inv_session, response, PJ_TRUE);
  3027. session_end(session);
  3028. /*
  3029. * session_end_completion will cleanup the final session reference unless
  3030. * ast_sip_session_terminate's caller is holding one.
  3031. */
  3032. session_end_completion(session);
  3033. } else {
  3034. pjsip_inv_terminate(session->inv_session, response, PJ_TRUE);
  3035. }
  3036. break;
  3037. case PJSIP_INV_STATE_CONFIRMED:
  3038. if (session->inv_session->invite_tsx) {
  3039. ast_debug(3, "%s: Delay sending BYE because of outstanding transaction...\n",
  3040. ast_sip_session_get_name(session));
  3041. /* If this is delayed the only thing that will happen is a BYE request so we don't
  3042. * actually need to store the response code for when it happens.
  3043. */
  3044. delay_request(session, NULL, NULL, NULL, 0, DELAYED_METHOD_BYE, NULL, NULL, 1);
  3045. break;
  3046. }
  3047. /* Fall through */
  3048. default:
  3049. status = pjsip_inv_end_session(session->inv_session, response, NULL, &packet);
  3050. if (status == PJ_SUCCESS && packet) {
  3051. struct ast_sip_session_delayed_request *delay;
  3052. /* Flush any delayed requests so they cannot overlap this transaction. */
  3053. while ((delay = AST_LIST_REMOVE_HEAD(&session->delayed_requests, next))) {
  3054. delayed_request_free(delay);
  3055. }
  3056. if (packet->msg->type == PJSIP_RESPONSE_MSG) {
  3057. ast_sip_session_send_response(session, packet);
  3058. } else {
  3059. ast_sip_session_send_request(session, packet);
  3060. }
  3061. }
  3062. break;
  3063. }
  3064. SCOPE_EXIT_RTN();
  3065. }
  3066. static int session_termination_task(void *data)
  3067. {
  3068. struct ast_sip_session *session = data;
  3069. if (session->defer_terminate) {
  3070. session->defer_terminate = 0;
  3071. if (session->inv_session) {
  3072. ast_sip_session_terminate(session, 0);
  3073. }
  3074. }
  3075. ao2_ref(session, -1);
  3076. return 0;
  3077. }
  3078. static void session_termination_cb(pj_timer_heap_t *timer_heap, struct pj_timer_entry *entry)
  3079. {
  3080. struct ast_sip_session *session = entry->user_data;
  3081. if (ast_sip_push_task(session->serializer, session_termination_task, session)) {
  3082. ao2_cleanup(session);
  3083. }
  3084. }
  3085. int ast_sip_session_defer_termination(struct ast_sip_session *session)
  3086. {
  3087. pj_time_val delay = { .sec = 60, };
  3088. int res;
  3089. /* The session should not have an active deferred termination request. */
  3090. ast_assert(!session->defer_terminate);
  3091. session->defer_terminate = 1;
  3092. session->defer_end = 1;
  3093. session->ended_while_deferred = 0;
  3094. ao2_ref(session, +1);
  3095. pj_timer_entry_init(&session->scheduled_termination, 0, session, session_termination_cb);
  3096. res = (pjsip_endpt_schedule_timer(ast_sip_get_pjsip_endpoint(),
  3097. &session->scheduled_termination, &delay) != PJ_SUCCESS) ? -1 : 0;
  3098. if (res) {
  3099. session->defer_terminate = 0;
  3100. ao2_ref(session, -1);
  3101. }
  3102. return res;
  3103. }
  3104. /*!
  3105. * \internal
  3106. * \brief Stop the defer termination timer if it is still running.
  3107. * \since 13.5.0
  3108. *
  3109. * \param session Which session to stop the timer.
  3110. */
  3111. static void sip_session_defer_termination_stop_timer(struct ast_sip_session *session)
  3112. {
  3113. if (pj_timer_heap_cancel_if_active(pjsip_endpt_get_timer_heap(ast_sip_get_pjsip_endpoint()),
  3114. &session->scheduled_termination, session->scheduled_termination.id)) {
  3115. ao2_ref(session, -1);
  3116. }
  3117. }
  3118. void ast_sip_session_defer_termination_cancel(struct ast_sip_session *session)
  3119. {
  3120. if (!session->defer_terminate) {
  3121. /* Already canceled or timer fired. */
  3122. return;
  3123. }
  3124. session->defer_terminate = 0;
  3125. if (session->terminate_while_deferred) {
  3126. /* Complete the termination started by the upper layer. */
  3127. ast_sip_session_terminate(session, 0);
  3128. }
  3129. /* Stop the termination timer if it is still running. */
  3130. sip_session_defer_termination_stop_timer(session);
  3131. }
  3132. void ast_sip_session_end_if_deferred(struct ast_sip_session *session)
  3133. {
  3134. if (!session->defer_end) {
  3135. return;
  3136. }
  3137. session->defer_end = 0;
  3138. if (session->ended_while_deferred) {
  3139. /* Complete the session end started by the remote hangup. */
  3140. ast_debug(3, "%s: Ending session after being deferred\n", ast_sip_session_get_name(session));
  3141. session->ended_while_deferred = 0;
  3142. session_end(session);
  3143. }
  3144. }
  3145. struct ast_sip_session *ast_sip_dialog_get_session(pjsip_dialog *dlg)
  3146. {
  3147. pjsip_inv_session *inv_session = pjsip_dlg_get_inv_session(dlg);
  3148. struct ast_sip_session *session;
  3149. if (!inv_session ||
  3150. !(session = inv_session->mod_data[session_module.id])) {
  3151. return NULL;
  3152. }
  3153. ao2_ref(session, +1);
  3154. return session;
  3155. }
  3156. pjsip_dialog *ast_sip_session_get_dialog(const struct ast_sip_session *session)
  3157. {
  3158. pjsip_inv_session *inv_session = session->inv_session;
  3159. if (!inv_session) {
  3160. return NULL;
  3161. }
  3162. return inv_session->dlg;
  3163. }
  3164. pjsip_inv_state ast_sip_session_get_pjsip_inv_state(const struct ast_sip_session *session)
  3165. {
  3166. pjsip_inv_session *inv_session = session->inv_session;
  3167. if (!inv_session) {
  3168. return PJSIP_INV_STATE_NULL;
  3169. }
  3170. return inv_session->state;
  3171. }
  3172. /*! \brief Fetch just the Caller ID number in order of PAI, RPID, From */
  3173. static int fetch_callerid_num(struct ast_sip_session *session, pjsip_rx_data *rdata, char *buf, size_t len)
  3174. {
  3175. int res = -1;
  3176. struct ast_party_id id;
  3177. ast_party_id_init(&id);
  3178. if (!ast_sip_set_id_from_invite(rdata, &id, &session->endpoint->id.self, session->endpoint->id.trust_inbound)) {
  3179. ast_copy_string(buf, id.number.str, len);
  3180. res = 0;
  3181. }
  3182. ast_party_id_free(&id);
  3183. return res;
  3184. }
  3185. enum sip_get_destination_result {
  3186. /*! The extension was successfully found */
  3187. SIP_GET_DEST_EXTEN_FOUND,
  3188. /*! The extension specified in the RURI was not found */
  3189. SIP_GET_DEST_EXTEN_NOT_FOUND,
  3190. /*! The extension specified in the RURI was a partial match */
  3191. SIP_GET_DEST_EXTEN_PARTIAL,
  3192. /*! The RURI is of an unsupported scheme */
  3193. SIP_GET_DEST_UNSUPPORTED_URI,
  3194. };
  3195. /*!
  3196. * \brief Determine where in the dialplan a call should go
  3197. *
  3198. * This uses the username in the request URI to try to match
  3199. * an extension in the endpoint's configured context in order
  3200. * to route the call.
  3201. *
  3202. * \param session The inbound SIP session
  3203. * \param rdata The SIP INVITE
  3204. */
  3205. static enum sip_get_destination_result get_destination(struct ast_sip_session *session, pjsip_rx_data *rdata)
  3206. {
  3207. char cid_num[AST_CHANNEL_NAME];
  3208. pjsip_uri *ruri = rdata->msg_info.msg->line.req.uri;
  3209. struct ast_features_pickup_config *pickup_cfg;
  3210. const char *pickupexten;
  3211. if (!ast_sip_is_allowed_uri(ruri)) {
  3212. return SIP_GET_DEST_UNSUPPORTED_URI;
  3213. }
  3214. ast_copy_pj_str(session->exten, ast_sip_pjsip_uri_get_username(ruri), sizeof(session->exten));
  3215. if (ast_strlen_zero(session->exten)) {
  3216. /* Some SIP devices send an empty extension for PLAR: this should map to s */
  3217. ast_debug(1, "RURI contains no user portion: defaulting to extension 's'\n");
  3218. ast_copy_string(session->exten, "s", sizeof(session->exten));
  3219. }
  3220. /*
  3221. * We may want to match in the dialplan without any user
  3222. * options getting in the way.
  3223. */
  3224. AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(session->exten);
  3225. pickup_cfg = ast_get_chan_features_pickup_config(NULL); /* session->channel doesn't exist yet, using NULL */
  3226. if (!pickup_cfg) {
  3227. ast_log(LOG_ERROR, "%s: Unable to retrieve pickup configuration options. Unable to detect call pickup extension\n",
  3228. ast_sip_session_get_name(session));
  3229. pickupexten = "";
  3230. } else {
  3231. pickupexten = ast_strdupa(pickup_cfg->pickupexten);
  3232. ao2_ref(pickup_cfg, -1);
  3233. }
  3234. fetch_callerid_num(session, rdata, cid_num, sizeof(cid_num));
  3235. /* If there's an overlap_context override specified, use that; otherwise, just use the endpoint's context */
  3236. if (!strcmp(session->exten, pickupexten) ||
  3237. ast_exists_extension(NULL, S_OR(session->endpoint->overlap_context, session->endpoint->context), session->exten, 1, S_OR(cid_num, NULL))) {
  3238. /*
  3239. * Save off the INVITE Request-URI in case it is
  3240. * needed: CHANNEL(pjsip,request_uri)
  3241. */
  3242. session->request_uri = pjsip_uri_clone(session->inv_session->pool, ruri);
  3243. return SIP_GET_DEST_EXTEN_FOUND;
  3244. }
  3245. /*
  3246. * Check for partial match via overlap dialling (if enabled)
  3247. */
  3248. if (session->endpoint->allow_overlap && (
  3249. !strncmp(session->exten, pickupexten, strlen(session->exten)) ||
  3250. ast_canmatch_extension(NULL, S_OR(session->endpoint->overlap_context, session->endpoint->context), session->exten, 1, S_OR(cid_num, NULL)))) {
  3251. /* Overlap partial match */
  3252. return SIP_GET_DEST_EXTEN_PARTIAL;
  3253. }
  3254. return SIP_GET_DEST_EXTEN_NOT_FOUND;
  3255. }
  3256. /*!
  3257. * \internal
  3258. * \brief Process initial answer for an incoming invite
  3259. *
  3260. * This function should only be called during the setup, and handling of a
  3261. * new incoming invite. Most, if not all of the time, this will be called
  3262. * when an error occurs and we need to respond as such.
  3263. *
  3264. * When a SIP session termination code is given for the answer it's assumed
  3265. * this call then will be the final bit of processing before ending session
  3266. * setup. As such, we've been holding a lock, and a reference on the invite
  3267. * session's dialog. So before returning this function removes that reference,
  3268. * and unlocks the dialog.
  3269. *
  3270. * \param inv_session The session on which to answer
  3271. * \param rdata The original request
  3272. * \param answer_code The answer's numeric code
  3273. * \param terminate_code The termination code if the answer fails
  3274. * \param notify Whether or not to call on_state_changed
  3275. *
  3276. * \retval 0 if invite successfully answered, -1 if an error occurred
  3277. */
  3278. static int new_invite_initial_answer(pjsip_inv_session *inv_session, pjsip_rx_data *rdata,
  3279. int answer_code, int terminate_code, pj_bool_t notify)
  3280. {
  3281. pjsip_tx_data *tdata = NULL;
  3282. int res = 0;
  3283. if (inv_session->state != PJSIP_INV_STATE_DISCONNECTED) {
  3284. if (pjsip_inv_initial_answer(
  3285. inv_session, rdata, answer_code, NULL, NULL, &tdata) != PJ_SUCCESS) {
  3286. pjsip_inv_terminate(inv_session, terminate_code ? terminate_code : answer_code, notify);
  3287. res = -1;
  3288. } else {
  3289. pjsip_inv_send_msg(inv_session, tdata);
  3290. }
  3291. }
  3292. if (answer_code >= 300) {
  3293. /*
  3294. * A session is ending. The dialog has a reference that needs to be
  3295. * removed and holds a lock that needs to be unlocked before returning.
  3296. */
  3297. pjsip_dlg_dec_lock(inv_session->dlg);
  3298. }
  3299. return res;
  3300. }
  3301. /*!
  3302. * \internal
  3303. * \brief Create and initialize a pjsip invite session
  3304. *
  3305. * pjsip_inv_session adds, and maintains a reference to the dialog upon a successful
  3306. * invite session creation until the session is destroyed. However, we'll wait to
  3307. * remove the reference that was added for the dialog when it gets created since we're
  3308. * not ready to unlock the dialog in this function.
  3309. *
  3310. * So, if this function successfully returns that means it returns with its newly
  3311. * created, and associated dialog locked and with two references (i.e. dialog's
  3312. * reference count should be 2).
  3313. *
  3314. * \param rdata The request that is starting the dialog
  3315. * \param endpoint A pointer to the endpoint
  3316. *
  3317. * \return A pjsip invite session object
  3318. * \retval NULL on error
  3319. */
  3320. static pjsip_inv_session *pre_session_setup(pjsip_rx_data *rdata, const struct ast_sip_endpoint *endpoint)
  3321. {
  3322. pjsip_tx_data *tdata;
  3323. pjsip_dialog *dlg;
  3324. pjsip_inv_session *inv_session;
  3325. unsigned int options = endpoint->extensions.flags;
  3326. const pj_str_t STR_100REL = { "100rel", 6};
  3327. unsigned int i;
  3328. pj_status_t dlg_status = PJ_EUNKNOWN;
  3329. /*
  3330. * If 100rel is set to "peer_supported" on the endpoint and the peer indicated support for 100rel
  3331. * in the Supported header, send 1xx responses reliably by adding PJSIP_INV_REQUIRE_100REL to pjsip_inv_options flags.
  3332. */
  3333. if (endpoint->rel100 == AST_SIP_100REL_PEER_SUPPORTED && rdata->msg_info.supported != NULL) {
  3334. for (i = 0; i < rdata->msg_info.supported->count; ++i) {
  3335. if (pj_stricmp(&rdata->msg_info.supported->values[i], &STR_100REL) == 0) {
  3336. options |= PJSIP_INV_REQUIRE_100REL;
  3337. break;
  3338. }
  3339. }
  3340. }
  3341. if (pjsip_inv_verify_request(rdata, &options, NULL, NULL, ast_sip_get_pjsip_endpoint(), &tdata) != PJ_SUCCESS) {
  3342. if (tdata) {
  3343. if (pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL) != PJ_SUCCESS) {
  3344. pjsip_tx_data_dec_ref(tdata);
  3345. }
  3346. } else {
  3347. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
  3348. }
  3349. return NULL;
  3350. }
  3351. dlg = ast_sip_create_dialog_uas_locked(endpoint, rdata, &dlg_status);
  3352. if (!dlg) {
  3353. if (dlg_status != PJ_EEXISTS) {
  3354. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
  3355. }
  3356. return NULL;
  3357. }
  3358. /*
  3359. * The returned dialog holds a lock and has a reference added. Any paths where the
  3360. * dialog invite session is not returned must unlock the dialog and remove its reference.
  3361. */
  3362. if (pjsip_inv_create_uas(dlg, rdata, NULL, options, &inv_session) != PJ_SUCCESS) {
  3363. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
  3364. /*
  3365. * The acquired dialog holds a lock, and a reference. Since the dialog is not
  3366. * going to be returned here it must first be unlocked and de-referenced. This
  3367. * must be done prior to calling dialog termination.
  3368. */
  3369. pjsip_dlg_dec_lock(dlg);
  3370. pjsip_dlg_terminate(dlg);
  3371. return NULL;
  3372. }
  3373. #if defined(HAVE_PJSIP_REPLACE_MEDIA_STREAM) || defined(PJMEDIA_SDP_NEG_ALLOW_MEDIA_CHANGE)
  3374. inv_session->sdp_neg_flags = PJMEDIA_SDP_NEG_ALLOW_MEDIA_CHANGE;
  3375. #endif
  3376. if (pjsip_dlg_add_usage(dlg, &session_module, NULL) != PJ_SUCCESS) {
  3377. /* Dialog's lock and a reference are removed in new_invite_initial_answer */
  3378. new_invite_initial_answer(inv_session, rdata, 500, 500, PJ_FALSE);
  3379. /* Remove 2nd reference added at inv_session creation */
  3380. pjsip_dlg_dec_session(inv_session->dlg, &session_module);
  3381. return NULL;
  3382. }
  3383. return inv_session;
  3384. }
  3385. struct new_invite {
  3386. /*! \brief Session created for the new INVITE */
  3387. struct ast_sip_session *session;
  3388. /*! \brief INVITE request itself */
  3389. pjsip_rx_data *rdata;
  3390. };
  3391. static int check_sdp_content_type_supported(pjsip_media_type *content_type)
  3392. {
  3393. pjsip_media_type app_sdp;
  3394. pjsip_media_type_init2(&app_sdp, "application", "sdp");
  3395. if (!pjsip_media_type_cmp(content_type, &app_sdp, 0)) {
  3396. return 1;
  3397. }
  3398. return 0;
  3399. }
  3400. static int check_content_disposition_in_multipart(pjsip_multipart_part *part)
  3401. {
  3402. pjsip_hdr *hdr = part->hdr.next;
  3403. static const pj_str_t str_handling_required = {"handling=required", 16};
  3404. while (hdr != &part->hdr) {
  3405. if (hdr->type == PJSIP_H_OTHER) {
  3406. pjsip_generic_string_hdr *generic_hdr = (pjsip_generic_string_hdr*)hdr;
  3407. if (!pj_stricmp2(&hdr->name, "Content-Disposition") &&
  3408. pj_stristr(&generic_hdr->hvalue, &str_handling_required) &&
  3409. !check_sdp_content_type_supported(&part->body->content_type)) {
  3410. return 1;
  3411. }
  3412. }
  3413. hdr = hdr->next;
  3414. }
  3415. return 0;
  3416. }
  3417. /**
  3418. * if there is required media we don't understand, return 1
  3419. */
  3420. static int check_content_disposition(pjsip_rx_data *rdata)
  3421. {
  3422. pjsip_msg_body *body = rdata->msg_info.msg->body;
  3423. pjsip_ctype_hdr *ctype_hdr = rdata->msg_info.ctype;
  3424. if (body && ctype_hdr &&
  3425. ast_sip_is_media_type_in(&ctype_hdr->media, &pjsip_media_type_multipart_mixed,
  3426. &pjsip_media_type_multipart_alternative, SENTINEL)) {
  3427. pjsip_multipart_part *part = pjsip_multipart_get_first_part(body);
  3428. while (part != NULL) {
  3429. if (check_content_disposition_in_multipart(part)) {
  3430. return 1;
  3431. }
  3432. part = pjsip_multipart_get_next_part(body, part);
  3433. }
  3434. }
  3435. return 0;
  3436. }
  3437. static int new_invite(struct new_invite *invite)
  3438. {
  3439. pjsip_tx_data *tdata = NULL;
  3440. pjsip_timer_setting timer;
  3441. pjsip_rdata_sdp_info *sdp_info;
  3442. pjmedia_sdp_session *local = NULL;
  3443. char buffer[AST_SOCKADDR_BUFLEN];
  3444. SCOPE_ENTER(3, "%s\n", ast_sip_session_get_name(invite->session));
  3445. /* From this point on, any calls to pjsip_inv_terminate have the last argument as PJ_TRUE
  3446. * so that we will be notified so we can destroy the session properly
  3447. */
  3448. if (invite->session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
  3449. ast_trace_log(-1, LOG_ERROR, "%s: Session already DISCONNECTED [reason=%d (%s)]\n",
  3450. ast_sip_session_get_name(invite->session),
  3451. invite->session->inv_session->cause,
  3452. pjsip_get_status_text(invite->session->inv_session->cause)->ptr);
  3453. SCOPE_EXIT_RTN_VALUE(-1);
  3454. }
  3455. switch (get_destination(invite->session, invite->rdata)) {
  3456. case SIP_GET_DEST_EXTEN_FOUND:
  3457. /* Things worked. Keep going */
  3458. break;
  3459. case SIP_GET_DEST_UNSUPPORTED_URI:
  3460. ast_trace(-1, "%s: Call (%s:%s) to extension '%s' - unsupported uri\n",
  3461. ast_sip_session_get_name(invite->session),
  3462. invite->rdata->tp_info.transport->type_name,
  3463. pj_sockaddr_print(&invite->rdata->pkt_info.src_addr, buffer, sizeof(buffer), 3),
  3464. invite->session->exten);
  3465. if (pjsip_inv_initial_answer(invite->session->inv_session, invite->rdata, 416, NULL, NULL, &tdata) == PJ_SUCCESS) {
  3466. ast_sip_session_send_response(invite->session, tdata);
  3467. } else {
  3468. pjsip_inv_terminate(invite->session->inv_session, 416, PJ_TRUE);
  3469. }
  3470. goto end;
  3471. case SIP_GET_DEST_EXTEN_PARTIAL:
  3472. ast_trace(-1, "%s: Call (%s:%s) to extension '%s' - partial match\n",
  3473. ast_sip_session_get_name(invite->session),
  3474. invite->rdata->tp_info.transport->type_name,
  3475. pj_sockaddr_print(&invite->rdata->pkt_info.src_addr, buffer, sizeof(buffer), 3),
  3476. invite->session->exten);
  3477. if (pjsip_inv_initial_answer(invite->session->inv_session, invite->rdata, 484, NULL, NULL, &tdata) == PJ_SUCCESS) {
  3478. ast_sip_session_send_response(invite->session, tdata);
  3479. } else {
  3480. pjsip_inv_terminate(invite->session->inv_session, 484, PJ_TRUE);
  3481. }
  3482. goto end;
  3483. case SIP_GET_DEST_EXTEN_NOT_FOUND:
  3484. default:
  3485. ast_trace_log(-1, LOG_NOTICE, "%s: Call (%s:%s) to extension '%s' rejected because extension not found in context '%s'.\n",
  3486. ast_sip_session_get_name(invite->session),
  3487. invite->rdata->tp_info.transport->type_name,
  3488. pj_sockaddr_print(&invite->rdata->pkt_info.src_addr, buffer, sizeof(buffer), 3),
  3489. invite->session->exten,
  3490. invite->session->endpoint->context);
  3491. if (pjsip_inv_initial_answer(invite->session->inv_session, invite->rdata, 404, NULL, NULL, &tdata) == PJ_SUCCESS) {
  3492. ast_sip_session_send_response(invite->session, tdata);
  3493. } else {
  3494. pjsip_inv_terminate(invite->session->inv_session, 404, PJ_TRUE);
  3495. }
  3496. goto end;
  3497. };
  3498. if (check_content_disposition(invite->rdata)) {
  3499. if (pjsip_inv_initial_answer(invite->session->inv_session, invite->rdata, 415, NULL, NULL, &tdata) == PJ_SUCCESS) {
  3500. ast_sip_session_send_response(invite->session, tdata);
  3501. } else {
  3502. pjsip_inv_terminate(invite->session->inv_session, 415, PJ_TRUE);
  3503. }
  3504. goto end;
  3505. }
  3506. pjsip_timer_setting_default(&timer);
  3507. timer.min_se = invite->session->endpoint->extensions.timer.min_se;
  3508. timer.sess_expires = invite->session->endpoint->extensions.timer.sess_expires;
  3509. pjsip_timer_init_session(invite->session->inv_session, &timer);
  3510. /*
  3511. * At this point, we've verified what we can that won't take awhile,
  3512. * so let's go ahead and send a 100 Trying out to stop any
  3513. * retransmissions.
  3514. */
  3515. if (pjsip_inv_initial_answer(invite->session->inv_session, invite->rdata, 100, NULL, NULL, &tdata) != PJ_SUCCESS) {
  3516. if (tdata) {
  3517. pjsip_inv_send_msg(invite->session->inv_session, tdata);
  3518. } else {
  3519. pjsip_inv_terminate(invite->session->inv_session, 500, PJ_TRUE);
  3520. }
  3521. goto end;
  3522. }
  3523. ast_trace(-1, "%s: Call (%s:%s) to extension '%s' sending 100 Trying\n",
  3524. ast_sip_session_get_name(invite->session),
  3525. invite->rdata->tp_info.transport->type_name,
  3526. pj_sockaddr_print(&invite->rdata->pkt_info.src_addr, buffer, sizeof(buffer), 3),
  3527. invite->session->exten);
  3528. ast_sip_session_send_response(invite->session, tdata);
  3529. sdp_info = pjsip_rdata_get_sdp_info(invite->rdata);
  3530. if (sdp_info && (sdp_info->sdp_err == PJ_SUCCESS) && sdp_info->sdp) {
  3531. if (handle_incoming_sdp(invite->session, sdp_info->sdp)) {
  3532. tdata = NULL;
  3533. if (pjsip_inv_end_session(invite->session->inv_session, 488, NULL, &tdata) == PJ_SUCCESS
  3534. && tdata) {
  3535. ast_sip_session_send_response(invite->session, tdata);
  3536. }
  3537. goto end;
  3538. }
  3539. /* We are creating a local SDP which is an answer to their offer */
  3540. local = create_local_sdp(invite->session->inv_session, invite->session, sdp_info->sdp, 0);
  3541. } else {
  3542. /* We are creating a local SDP which is an offer */
  3543. local = create_local_sdp(invite->session->inv_session, invite->session, NULL, 0);
  3544. }
  3545. /* If we were unable to create a local SDP terminate the session early, it won't go anywhere */
  3546. if (!local) {
  3547. tdata = NULL;
  3548. if (pjsip_inv_end_session(invite->session->inv_session, 500, NULL, &tdata) == PJ_SUCCESS
  3549. && tdata) {
  3550. ast_sip_session_send_response(invite->session, tdata);
  3551. }
  3552. goto end;
  3553. }
  3554. pjsip_inv_set_local_sdp(invite->session->inv_session, local);
  3555. pjmedia_sdp_neg_set_prefer_remote_codec_order(invite->session->inv_session->neg, PJ_FALSE);
  3556. #ifdef PJMEDIA_SDP_NEG_ANSWER_MULTIPLE_CODECS
  3557. if (!invite->session->endpoint->preferred_codec_only) {
  3558. pjmedia_sdp_neg_set_answer_multiple_codecs(invite->session->inv_session->neg, PJ_TRUE);
  3559. }
  3560. #endif
  3561. handle_incoming_request(invite->session, invite->rdata);
  3562. end:
  3563. SCOPE_EXIT_RTN_VALUE(0, "%s\n", ast_sip_session_get_name(invite->session));
  3564. }
  3565. static void handle_new_invite_request(pjsip_rx_data *rdata)
  3566. {
  3567. RAII_VAR(struct ast_sip_endpoint *, endpoint,
  3568. ast_pjsip_rdata_get_endpoint(rdata), ao2_cleanup);
  3569. pjsip_inv_session *inv_session = NULL;
  3570. struct ast_sip_session *session;
  3571. struct new_invite invite;
  3572. char *req_uri = TRACE_ATLEAST(1) ? ast_alloca(256) : "";
  3573. int res = TRACE_ATLEAST(1) ? pjsip_uri_print(PJSIP_URI_IN_REQ_URI, rdata->msg_info.msg->line.req.uri, req_uri, 256) : 0;
  3574. SCOPE_ENTER(1, "Request: %s\n", res ? req_uri : "");
  3575. ast_assert(endpoint != NULL);
  3576. inv_session = pre_session_setup(rdata, endpoint);
  3577. if (!inv_session) {
  3578. /* pre_session_setup() returns a response on failure */
  3579. SCOPE_EXIT_RTN("Failure in pre session setup\n");
  3580. }
  3581. /*
  3582. * Upon a successful pre_session_setup the associated dialog is returned locked
  3583. * and with an added reference. Well actually two references. One added when the
  3584. * dialog itself was created, and another added when the pjsip invite session was
  3585. * created and the dialog was added to it.
  3586. *
  3587. * In order to ensure the dialog's, and any of its internal attributes, lifetimes
  3588. * we'll hold the lock and maintain the reference throughout the entire new invite
  3589. * handling process. See ast_sip_create_dialog_uas_locked for more details but,
  3590. * basically we do this to make sure a transport failure does not destroy the dialog
  3591. * and/or transaction out from underneath us between pjsip calls. Alternatively, we
  3592. * could probably release the lock if we needed to, but then we'd have to re-lock and
  3593. * check the dialog and transaction prior to every pjsip call.
  3594. *
  3595. * That means any off nominal/failure paths in this function must remove the associated
  3596. * dialog reference added at dialog creation, and remove the lock. As well the
  3597. * referenced pjsip invite session must be "cleaned up", which should also then
  3598. * remove its reference to the dialog at that time.
  3599. *
  3600. * Nominally we'll unlock the dialog, and release the reference when all new invite
  3601. * process handling has successfully completed.
  3602. */
  3603. session = ast_sip_session_alloc(endpoint, NULL, inv_session, rdata);
  3604. if (!session) {
  3605. /* Dialog's lock and reference are removed in new_invite_initial_answer */
  3606. if (!new_invite_initial_answer(inv_session, rdata, 500, 500, PJ_FALSE)) {
  3607. /* Terminate the session if it wasn't done in the answer */
  3608. pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
  3609. }
  3610. SCOPE_EXIT_RTN("Couldn't create session\n");
  3611. }
  3612. session->call_direction = AST_SIP_SESSION_INCOMING_CALL;
  3613. /*
  3614. * The current thread is supposed be the session serializer to prevent
  3615. * any initial INVITE retransmissions from trying to setup the same
  3616. * call again.
  3617. */
  3618. ast_assert(ast_taskprocessor_is_task(session->serializer));
  3619. invite.session = session;
  3620. invite.rdata = rdata;
  3621. new_invite(&invite);
  3622. /*
  3623. * The dialog lock and reference added at dialog creation time must be
  3624. * maintained throughout the new invite process. Since we're pretty much
  3625. * done at this point with things it's safe to go ahead and remove the lock
  3626. * and the reference here. See ast_sip_create_dialog_uas_locked for more info.
  3627. *
  3628. * Note, any future functionality added that does work using the dialog must
  3629. * be done before this.
  3630. */
  3631. pjsip_dlg_dec_lock(inv_session->dlg);
  3632. SCOPE_EXIT("Request: %s Session: %s\n", req_uri, ast_sip_session_get_name(session));
  3633. ao2_ref(session, -1);
  3634. }
  3635. static pj_bool_t does_method_match(const pj_str_t *message_method, const char *supplement_method)
  3636. {
  3637. pj_str_t method;
  3638. if (ast_strlen_zero(supplement_method)) {
  3639. return PJ_TRUE;
  3640. }
  3641. pj_cstr(&method, supplement_method);
  3642. return pj_stristr(&method, message_method) ? PJ_TRUE : PJ_FALSE;
  3643. }
  3644. static pj_bool_t has_supplement(const struct ast_sip_session *session, const pjsip_rx_data *rdata)
  3645. {
  3646. struct ast_sip_session_supplement *supplement;
  3647. struct pjsip_method *method = &rdata->msg_info.msg->line.req.method;
  3648. if (!session) {
  3649. return PJ_FALSE;
  3650. }
  3651. AST_LIST_TRAVERSE(&session->supplements, supplement, next) {
  3652. if (does_method_match(&method->name, supplement->method)) {
  3653. return PJ_TRUE;
  3654. }
  3655. }
  3656. return PJ_FALSE;
  3657. }
  3658. /*!
  3659. * \internal
  3660. * Added for debugging purposes
  3661. */
  3662. static void session_on_tsx_state(pjsip_transaction *tsx, pjsip_event *e)
  3663. {
  3664. pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
  3665. pjsip_inv_session *inv_session = (dlg ? pjsip_dlg_get_inv_session(dlg) : NULL);
  3666. struct ast_sip_session *session = (inv_session ? inv_session->mod_data[session_module.id] : NULL);
  3667. SCOPE_ENTER(1, "%s TSX State: %s Inv State: %s\n", ast_sip_session_get_name(session),
  3668. pjsip_tsx_state_str(tsx->state), inv_session ? pjsip_inv_state_name(inv_session->state) : "unknown");
  3669. if (session) {
  3670. ast_trace(2, "Topology: Pending: %s Active: %s\n",
  3671. ast_str_tmp(256, ast_stream_topology_to_str(session->pending_media_state->topology, &STR_TMP)),
  3672. ast_str_tmp(256, ast_stream_topology_to_str(session->active_media_state->topology, &STR_TMP)));
  3673. }
  3674. SCOPE_EXIT_RTN();
  3675. }
  3676. /*!
  3677. * \internal
  3678. * Added for debugging purposes
  3679. */
  3680. static pj_bool_t session_on_rx_response(pjsip_rx_data *rdata)
  3681. {
  3682. struct pjsip_status_line status = rdata->msg_info.msg->line.status;
  3683. pjsip_dialog *dlg = pjsip_rdata_get_dlg(rdata);
  3684. pjsip_inv_session *inv_session = dlg ? pjsip_dlg_get_inv_session(dlg) : NULL;
  3685. struct ast_sip_session *session = (inv_session ? inv_session->mod_data[session_module.id] : NULL);
  3686. SCOPE_ENTER(1, "%s Method: %.*s Status: %d\n", ast_sip_session_get_name(session),
  3687. (int)rdata->msg_info.cseq->method.name.slen, rdata->msg_info.cseq->method.name.ptr, status.code);
  3688. SCOPE_EXIT_RTN_VALUE(PJ_FALSE);
  3689. }
  3690. /*!
  3691. * \brief Called when a new SIP request comes into PJSIP
  3692. *
  3693. * This function is called under two circumstances
  3694. * 1) An out-of-dialog request is received by PJSIP
  3695. * 2) An in-dialog request that the inv_session layer does not
  3696. * handle is received (such as an in-dialog INFO)
  3697. *
  3698. * Except for INVITEs, there is very little we actually do in this function
  3699. * 1) For requests we don't handle, we return PJ_FALSE
  3700. * 2) For new INVITEs, handle them now to prevent retransmissions from
  3701. * trying to setup the same call again.
  3702. * 3) For in-dialog requests we handle, we process them in the
  3703. * .on_state_changed = session_inv_on_state_changed or
  3704. * .on_tsx_state_changed = session_inv_on_tsx_state_changed
  3705. * callbacks instead.
  3706. */
  3707. static pj_bool_t session_on_rx_request(pjsip_rx_data *rdata)
  3708. {
  3709. pj_status_t handled = PJ_FALSE;
  3710. struct pjsip_request_line req = rdata->msg_info.msg->line.req;
  3711. pjsip_dialog *dlg = pjsip_rdata_get_dlg(rdata);
  3712. pjsip_inv_session *inv_session = (dlg ? pjsip_dlg_get_inv_session(dlg) : NULL);
  3713. struct ast_sip_session *session = (inv_session ? inv_session->mod_data[session_module.id] : NULL);
  3714. char *req_uri = TRACE_ATLEAST(1) ? ast_alloca(256) : "";
  3715. int res = TRACE_ATLEAST(1) ? pjsip_uri_print(PJSIP_URI_IN_REQ_URI, rdata->msg_info.msg->line.req.uri, req_uri, 256) : 0;
  3716. SCOPE_ENTER(1, "%s Request: %.*s %s\n", ast_sip_session_get_name(session),
  3717. (int) pj_strlen(&req.method.name), pj_strbuf(&req.method.name), res ? req_uri : "");
  3718. switch (req.method.id) {
  3719. case PJSIP_INVITE_METHOD:
  3720. if (dlg) {
  3721. ast_log(LOG_WARNING, "on_rx_request called for INVITE in mid-dialog?\n");
  3722. break;
  3723. }
  3724. handled = PJ_TRUE;
  3725. handle_new_invite_request(rdata);
  3726. break;
  3727. default:
  3728. /* Handle other in-dialog methods if their supplements have been registered */
  3729. handled = dlg && (inv_session = pjsip_dlg_get_inv_session(dlg)) &&
  3730. has_supplement(inv_session->mod_data[session_module.id], rdata);
  3731. break;
  3732. }
  3733. SCOPE_EXIT_RTN_VALUE(handled, "%s Handled request %.*s %s ? %s\n", ast_sip_session_get_name(session),
  3734. (int) pj_strlen(&req.method.name), pj_strbuf(&req.method.name), req_uri,
  3735. handled == PJ_TRUE ? "yes" : "no");
  3736. }
  3737. static pj_bool_t session_on_tx_response(pjsip_tx_data *tdata)
  3738. {
  3739. pjsip_dialog *dlg = pjsip_tdata_get_dlg(tdata);
  3740. RAII_VAR(struct ast_sip_session *, session, dlg ? ast_sip_dialog_get_session(dlg) : NULL, ao2_cleanup);
  3741. if (session) {
  3742. handle_outgoing_response(session, tdata);
  3743. }
  3744. return PJ_SUCCESS;
  3745. }
  3746. static void resend_reinvite(pj_timer_heap_t *timer, pj_timer_entry *entry)
  3747. {
  3748. struct ast_sip_session *session = entry->user_data;
  3749. ast_debug(3, "%s: re-INVITE collision timer expired.\n",
  3750. ast_sip_session_get_name(session));
  3751. if (AST_LIST_EMPTY(&session->delayed_requests)) {
  3752. /* No delayed request pending, so just return */
  3753. ao2_ref(session, -1);
  3754. return;
  3755. }
  3756. if (ast_sip_push_task(session->serializer, invite_collision_timeout, session)) {
  3757. /*
  3758. * Uh oh. We now have nothing in the foreseeable future
  3759. * to trigger sending the delayed requests.
  3760. */
  3761. ao2_ref(session, -1);
  3762. }
  3763. }
  3764. static void reschedule_reinvite(struct ast_sip_session *session, ast_sip_session_response_cb on_response)
  3765. {
  3766. pjsip_inv_session *inv = session->inv_session;
  3767. pj_time_val tv;
  3768. struct ast_sip_session_media_state *pending_media_state = NULL;
  3769. struct ast_sip_session_media_state *active_media_state = NULL;
  3770. const char *session_name = ast_sip_session_get_name(session);
  3771. int use_pending = 0;
  3772. int use_active = 0;
  3773. SCOPE_ENTER(3, "%s\n", session_name);
  3774. /*
  3775. * If the two media state topologies are the same this means that the session refresh request
  3776. * did not specify a desired topology, so it does not care. If that is the case we don't even
  3777. * pass one in here resulting in the current topology being used. It's possible though that
  3778. * either one of the topologies could be NULL so we have to test for that before we check for
  3779. * equality.
  3780. */
  3781. /* We only want to clone a media state if its topology is not null */
  3782. use_pending = session->pending_media_state->topology != NULL;
  3783. use_active = session->active_media_state->topology != NULL;
  3784. /*
  3785. * If both media states have topologies, we can test for equality. If they're equal we're not going to
  3786. * clone either states.
  3787. */
  3788. if (use_pending && use_active && ast_stream_topology_equal(session->active_media_state->topology, session->pending_media_state->topology)) {
  3789. use_pending = 0;
  3790. use_active = 0;
  3791. }
  3792. if (use_pending) {
  3793. pending_media_state = ast_sip_session_media_state_clone(session->pending_media_state);
  3794. if (!pending_media_state) {
  3795. SCOPE_EXIT_LOG_RTN(LOG_ERROR, "%s: Failed to clone pending media state\n", session_name);
  3796. }
  3797. }
  3798. if (use_active) {
  3799. active_media_state = ast_sip_session_media_state_clone(session->active_media_state);
  3800. if (!active_media_state) {
  3801. ast_sip_session_media_state_free(pending_media_state);
  3802. SCOPE_EXIT_LOG_RTN(LOG_ERROR, "%s: Failed to clone active media state\n", session_name);
  3803. }
  3804. }
  3805. if (delay_request(session, NULL, NULL, on_response, 1, DELAYED_METHOD_INVITE, pending_media_state,
  3806. active_media_state, 1)) {
  3807. ast_sip_session_media_state_free(pending_media_state);
  3808. ast_sip_session_media_state_free(active_media_state);
  3809. SCOPE_EXIT_LOG_RTN(LOG_ERROR, "%s: Failed to add delayed request\n", session_name);
  3810. }
  3811. if (pj_timer_entry_running(&session->rescheduled_reinvite)) {
  3812. /* Timer already running. Something weird is going on. */
  3813. SCOPE_EXIT_LOG_RTN(LOG_ERROR, "%s: re-INVITE collision while timer running!!!\n", session_name);
  3814. }
  3815. tv.sec = 0;
  3816. if (inv->role == PJSIP_ROLE_UAC) {
  3817. tv.msec = 2100 + ast_random() % 2000;
  3818. } else {
  3819. tv.msec = ast_random() % 2000;
  3820. }
  3821. pj_timer_entry_init(&session->rescheduled_reinvite, 0, session, resend_reinvite);
  3822. ao2_ref(session, +1);
  3823. if (pjsip_endpt_schedule_timer(ast_sip_get_pjsip_endpoint(),
  3824. &session->rescheduled_reinvite, &tv) != PJ_SUCCESS) {
  3825. ao2_ref(session, -1);
  3826. SCOPE_EXIT_LOG_RTN(LOG_ERROR, "%s: Couldn't schedule timer\n", session_name);
  3827. }
  3828. SCOPE_EXIT_RTN();
  3829. }
  3830. static void __print_debug_details(const char *function, pjsip_inv_session *inv, pjsip_transaction *tsx, pjsip_event *e)
  3831. {
  3832. int id = session_module.id;
  3833. struct ast_sip_session *session = NULL;
  3834. if (!DEBUG_ATLEAST(5)) {
  3835. /* Debug not spamy enough */
  3836. return;
  3837. }
  3838. ast_log(LOG_DEBUG, "Function %s called on event %s\n",
  3839. function, pjsip_event_str(e->type));
  3840. if (!inv) {
  3841. ast_log(LOG_DEBUG, "Transaction %p does not belong to an inv_session?\n", tsx);
  3842. ast_log(LOG_DEBUG, "The transaction state is %s\n",
  3843. pjsip_tsx_state_str(tsx->state));
  3844. return;
  3845. }
  3846. if (id > -1) {
  3847. session = inv->mod_data[session_module.id];
  3848. }
  3849. if (!session) {
  3850. ast_log(LOG_DEBUG, "inv_session %p has no ast session\n", inv);
  3851. } else {
  3852. ast_log(LOG_DEBUG, "The state change pertains to the endpoint '%s(%s)'\n",
  3853. ast_sorcery_object_get_id(session->endpoint),
  3854. session->channel ? ast_channel_name(session->channel) : "");
  3855. }
  3856. if (inv->invite_tsx) {
  3857. ast_log(LOG_DEBUG, "The inv session still has an invite_tsx (%p)\n",
  3858. inv->invite_tsx);
  3859. } else {
  3860. ast_log(LOG_DEBUG, "The inv session does NOT have an invite_tsx\n");
  3861. }
  3862. if (tsx) {
  3863. ast_log(LOG_DEBUG, "The %s %.*s transaction involved in this state change is %p\n",
  3864. pjsip_role_name(tsx->role),
  3865. (int) pj_strlen(&tsx->method.name), pj_strbuf(&tsx->method.name),
  3866. tsx);
  3867. ast_log(LOG_DEBUG, "The current transaction state is %s\n",
  3868. pjsip_tsx_state_str(tsx->state));
  3869. ast_log(LOG_DEBUG, "The transaction state change event is %s\n",
  3870. pjsip_event_str(e->body.tsx_state.type));
  3871. } else {
  3872. ast_log(LOG_DEBUG, "There is no transaction involved in this state change\n");
  3873. }
  3874. ast_log(LOG_DEBUG, "The current inv state is %s\n", pjsip_inv_state_name(inv->state));
  3875. }
  3876. #define print_debug_details(inv, tsx, e) __print_debug_details(__PRETTY_FUNCTION__, (inv), (tsx), (e))
  3877. static void handle_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
  3878. {
  3879. struct ast_sip_session_supplement *supplement;
  3880. struct pjsip_request_line req = rdata->msg_info.msg->line.req;
  3881. SCOPE_ENTER(3, "%s: Method is %.*s\n", ast_sip_session_get_name(session), (int) pj_strlen(&req.method.name), pj_strbuf(&req.method.name));
  3882. AST_LIST_TRAVERSE(&session->supplements, supplement, next) {
  3883. if (supplement->incoming_request && does_method_match(&req.method.name, supplement->method)) {
  3884. if (supplement->incoming_request(session, rdata)) {
  3885. break;
  3886. }
  3887. }
  3888. }
  3889. SCOPE_EXIT("%s\n", ast_sip_session_get_name(session));
  3890. }
  3891. static void handle_session_begin(struct ast_sip_session *session)
  3892. {
  3893. struct ast_sip_session_supplement *iter;
  3894. AST_LIST_TRAVERSE(&session->supplements, iter, next) {
  3895. if (iter->session_begin) {
  3896. iter->session_begin(session);
  3897. }
  3898. }
  3899. }
  3900. static void handle_session_destroy(struct ast_sip_session *session)
  3901. {
  3902. struct ast_sip_session_supplement *iter;
  3903. AST_LIST_TRAVERSE(&session->supplements, iter, next) {
  3904. if (iter->session_destroy) {
  3905. iter->session_destroy(session);
  3906. }
  3907. }
  3908. }
  3909. static void handle_session_end(struct ast_sip_session *session)
  3910. {
  3911. struct ast_sip_session_supplement *iter;
  3912. /* Session is dead. Notify the supplements. */
  3913. AST_LIST_TRAVERSE(&session->supplements, iter, next) {
  3914. if (iter->session_end) {
  3915. iter->session_end(session);
  3916. }
  3917. }
  3918. }
  3919. static void handle_incoming_response(struct ast_sip_session *session, pjsip_rx_data *rdata,
  3920. enum ast_sip_session_response_priority response_priority)
  3921. {
  3922. struct ast_sip_session_supplement *supplement;
  3923. struct pjsip_status_line status = rdata->msg_info.msg->line.status;
  3924. SCOPE_ENTER(3, "%s: Response is %d %.*s\n", ast_sip_session_get_name(session),
  3925. status.code, (int) pj_strlen(&status.reason), pj_strbuf(&status.reason));
  3926. AST_LIST_TRAVERSE(&session->supplements, supplement, next) {
  3927. if (!(supplement->response_priority & response_priority)) {
  3928. continue;
  3929. }
  3930. if (supplement->incoming_response && does_method_match(&rdata->msg_info.cseq->method.name, supplement->method)) {
  3931. supplement->incoming_response(session, rdata);
  3932. }
  3933. }
  3934. SCOPE_EXIT("%s\n", ast_sip_session_get_name(session));
  3935. }
  3936. static int handle_incoming(struct ast_sip_session *session, pjsip_rx_data *rdata,
  3937. enum ast_sip_session_response_priority response_priority)
  3938. {
  3939. if (rdata->msg_info.msg->type == PJSIP_REQUEST_MSG) {
  3940. handle_incoming_request(session, rdata);
  3941. } else {
  3942. handle_incoming_response(session, rdata, response_priority);
  3943. }
  3944. return 0;
  3945. }
  3946. static void handle_outgoing_request(struct ast_sip_session *session, pjsip_tx_data *tdata)
  3947. {
  3948. struct ast_sip_session_supplement *supplement;
  3949. struct pjsip_request_line req = tdata->msg->line.req;
  3950. SCOPE_ENTER(3, "%s: Method is %.*s\n", ast_sip_session_get_name(session),
  3951. (int) pj_strlen(&req.method.name), pj_strbuf(&req.method.name));
  3952. ast_sip_message_apply_transport(session->endpoint->transport, tdata);
  3953. AST_LIST_TRAVERSE(&session->supplements, supplement, next) {
  3954. if (supplement->outgoing_request && does_method_match(&req.method.name, supplement->method)) {
  3955. supplement->outgoing_request(session, tdata);
  3956. }
  3957. }
  3958. SCOPE_EXIT("%s\n", ast_sip_session_get_name(session));
  3959. }
  3960. static void handle_outgoing_response(struct ast_sip_session *session, pjsip_tx_data *tdata)
  3961. {
  3962. struct ast_sip_session_supplement *supplement;
  3963. struct pjsip_status_line status = tdata->msg->line.status;
  3964. pjsip_cseq_hdr *cseq = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ, NULL);
  3965. SCOPE_ENTER(3, "%s: Method is %.*s, Response is %d %.*s\n", ast_sip_session_get_name(session),
  3966. (int) pj_strlen(&cseq->method.name),
  3967. pj_strbuf(&cseq->method.name), status.code, (int) pj_strlen(&status.reason),
  3968. pj_strbuf(&status.reason));
  3969. if (!cseq) {
  3970. SCOPE_EXIT_LOG_RTN(LOG_ERROR, "%s: Cannot send response due to missing sequence header",
  3971. ast_sip_session_get_name(session));
  3972. }
  3973. ast_sip_message_apply_transport(session->endpoint->transport, tdata);
  3974. AST_LIST_TRAVERSE(&session->supplements, supplement, next) {
  3975. if (supplement->outgoing_response && does_method_match(&cseq->method.name, supplement->method)) {
  3976. supplement->outgoing_response(session, tdata);
  3977. }
  3978. }
  3979. SCOPE_EXIT("%s\n", ast_sip_session_get_name(session));
  3980. }
  3981. static int session_end(void *vsession)
  3982. {
  3983. struct ast_sip_session *session = vsession;
  3984. /* Stop the scheduled termination */
  3985. sip_session_defer_termination_stop_timer(session);
  3986. /* Session is dead. Notify the supplements. */
  3987. handle_session_end(session);
  3988. return 0;
  3989. }
  3990. /*!
  3991. * \internal
  3992. * \brief Complete ending session activities.
  3993. * \since 13.5.0
  3994. *
  3995. * \param vsession Which session to complete stopping.
  3996. *
  3997. * \retval 0 on success.
  3998. * \retval -1 on error.
  3999. */
  4000. static int session_end_completion(void *vsession)
  4001. {
  4002. struct ast_sip_session *session = vsession;
  4003. ast_sip_dialog_set_serializer(session->inv_session->dlg, NULL);
  4004. ast_sip_dialog_set_endpoint(session->inv_session->dlg, NULL);
  4005. /* Now we can release the ref that was held by session->inv_session */
  4006. ao2_cleanup(session);
  4007. return 0;
  4008. }
  4009. static int check_request_status(pjsip_inv_session *inv, pjsip_event *e)
  4010. {
  4011. struct ast_sip_session *session = inv->mod_data[session_module.id];
  4012. pjsip_transaction *tsx = e->body.tsx_state.tsx;
  4013. if (tsx->status_code != 503 && tsx->status_code != 408) {
  4014. return 0;
  4015. }
  4016. if (!ast_sip_failover_request(tsx->last_tx)) {
  4017. return 0;
  4018. }
  4019. pjsip_inv_uac_restart(inv, PJ_FALSE);
  4020. /*
  4021. * Bump the ref since it will be on a new transaction and
  4022. * we don't want it to go away along with the old transaction.
  4023. */
  4024. pjsip_tx_data_add_ref(tsx->last_tx);
  4025. ast_sip_session_send_request(session, tsx->last_tx);
  4026. return 1;
  4027. }
  4028. static void handle_incoming_before_media(pjsip_inv_session *inv,
  4029. struct ast_sip_session *session, pjsip_rx_data *rdata)
  4030. {
  4031. pjsip_msg *msg;
  4032. ast_debug(3, "%s: Received %s\n", ast_sip_session_get_name(session), rdata->msg_info.msg->type == PJSIP_REQUEST_MSG ?
  4033. "request" : "response");
  4034. handle_incoming(session, rdata, AST_SIP_SESSION_BEFORE_MEDIA);
  4035. msg = rdata->msg_info.msg;
  4036. if (msg->type == PJSIP_REQUEST_MSG
  4037. && msg->line.req.method.id == PJSIP_ACK_METHOD
  4038. && pjmedia_sdp_neg_get_state(inv->neg) != PJMEDIA_SDP_NEG_STATE_DONE) {
  4039. pjsip_tx_data *tdata;
  4040. /*
  4041. * SDP negotiation failed on an incoming call that delayed
  4042. * negotiation and then gave us an invalid SDP answer. We
  4043. * need to send a BYE to end the call because of the invalid
  4044. * SDP answer.
  4045. */
  4046. ast_debug(1,
  4047. "%s: Ending session due to incomplete SDP negotiation. %s\n",
  4048. ast_sip_session_get_name(session),
  4049. pjsip_rx_data_get_info(rdata));
  4050. if (pjsip_inv_end_session(inv, 400, NULL, &tdata) == PJ_SUCCESS
  4051. && tdata) {
  4052. ast_sip_session_send_request(session, tdata);
  4053. }
  4054. }
  4055. }
  4056. static void session_inv_on_state_changed(pjsip_inv_session *inv, pjsip_event *e)
  4057. {
  4058. pjsip_event_id_e type;
  4059. struct ast_sip_session *session = inv->mod_data[session_module.id];
  4060. SCOPE_ENTER(1, "%s Event: %s Inv State: %s\n", ast_sip_session_get_name(session),
  4061. pjsip_event_str(e->type), pjsip_inv_state_name(inv->state));
  4062. if (ast_shutdown_final()) {
  4063. SCOPE_EXIT_RTN("Shutting down\n");
  4064. }
  4065. if (e) {
  4066. print_debug_details(inv, NULL, e);
  4067. type = e->type;
  4068. } else {
  4069. type = PJSIP_EVENT_UNKNOWN;
  4070. }
  4071. session = inv->mod_data[session_module.id];
  4072. if (!session) {
  4073. SCOPE_EXIT_RTN("No session\n");
  4074. }
  4075. switch(type) {
  4076. case PJSIP_EVENT_TX_MSG:
  4077. break;
  4078. case PJSIP_EVENT_RX_MSG:
  4079. handle_incoming_before_media(inv, session, e->body.rx_msg.rdata);
  4080. break;
  4081. case PJSIP_EVENT_TSX_STATE:
  4082. ast_debug(3, "%s: Source of transaction state change is %s\n", ast_sip_session_get_name(session),
  4083. pjsip_event_str(e->body.tsx_state.type));
  4084. /* Transaction state changes are prompted by some other underlying event. */
  4085. switch(e->body.tsx_state.type) {
  4086. case PJSIP_EVENT_TX_MSG:
  4087. break;
  4088. case PJSIP_EVENT_RX_MSG:
  4089. if (!check_request_status(inv, e)) {
  4090. handle_incoming_before_media(inv, session, e->body.tsx_state.src.rdata);
  4091. }
  4092. break;
  4093. case PJSIP_EVENT_TRANSPORT_ERROR:
  4094. case PJSIP_EVENT_TIMER:
  4095. /*
  4096. * Check the request status on transport error or timeout. A transport
  4097. * error can occur when a TCP socket closes and that can be the result
  4098. * of a 503. Also we may need to failover on a timeout (408).
  4099. */
  4100. check_request_status(inv, e);
  4101. break;
  4102. case PJSIP_EVENT_USER:
  4103. case PJSIP_EVENT_UNKNOWN:
  4104. case PJSIP_EVENT_TSX_STATE:
  4105. /* Inception? */
  4106. break;
  4107. }
  4108. break;
  4109. case PJSIP_EVENT_TRANSPORT_ERROR:
  4110. case PJSIP_EVENT_TIMER:
  4111. case PJSIP_EVENT_UNKNOWN:
  4112. case PJSIP_EVENT_USER:
  4113. default:
  4114. break;
  4115. }
  4116. if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
  4117. if (session->defer_end) {
  4118. ast_debug(3, "%s: Deferring session end\n", ast_sip_session_get_name(session));
  4119. session->ended_while_deferred = 1;
  4120. SCOPE_EXIT_RTN("Deferring\n");
  4121. }
  4122. if (ast_sip_push_task(session->serializer, session_end, session)) {
  4123. /* Do it anyway even though this is not the right thread. */
  4124. session_end(session);
  4125. }
  4126. }
  4127. SCOPE_EXIT_RTN();
  4128. }
  4129. static void session_inv_on_new_session(pjsip_inv_session *inv, pjsip_event *e)
  4130. {
  4131. /* XXX STUB */
  4132. }
  4133. static int session_end_if_disconnected(int id, pjsip_inv_session *inv)
  4134. {
  4135. struct ast_sip_session *session;
  4136. if (inv->state != PJSIP_INV_STATE_DISCONNECTED) {
  4137. return 0;
  4138. }
  4139. /*
  4140. * We are locking because ast_sip_dialog_get_session() needs
  4141. * the dialog locked to get the session by other threads.
  4142. */
  4143. pjsip_dlg_inc_lock(inv->dlg);
  4144. session = inv->mod_data[id];
  4145. inv->mod_data[id] = NULL;
  4146. pjsip_dlg_dec_lock(inv->dlg);
  4147. /*
  4148. * Pass the session ref held by session->inv_session to
  4149. * session_end_completion().
  4150. */
  4151. if (session
  4152. && ast_sip_push_task(session->serializer, session_end_completion, session)) {
  4153. /* Do it anyway even though this is not the right thread. */
  4154. session_end_completion(session);
  4155. }
  4156. return 1;
  4157. }
  4158. static void session_inv_on_tsx_state_changed(pjsip_inv_session *inv, pjsip_transaction *tsx, pjsip_event *e)
  4159. {
  4160. ast_sip_session_response_cb cb;
  4161. int id = session_module.id;
  4162. pjsip_tx_data *tdata;
  4163. struct ast_sip_session *session = inv->mod_data[session_module.id];
  4164. SCOPE_ENTER(1, "%s TSX State: %s Inv State: %s\n", ast_sip_session_get_name(session),
  4165. pjsip_tsx_state_str(tsx->state), pjsip_inv_state_name(inv->state));
  4166. if (ast_shutdown_final()) {
  4167. SCOPE_EXIT_RTN("Shutting down\n");
  4168. }
  4169. session = inv->mod_data[id];
  4170. print_debug_details(inv, tsx, e);
  4171. if (!session) {
  4172. /* The session has ended. Ignore the transaction change. */
  4173. SCOPE_EXIT_RTN("Session ended\n");
  4174. }
  4175. /*
  4176. * If the session is disconnected really nothing else to do unless currently transacting
  4177. * a BYE. If a BYE then hold off destruction until the transaction timeout occurs. This
  4178. * has to be done for BYEs because sometimes the dialog can be in a disconnected
  4179. * state but the BYE request transaction has not yet completed.
  4180. */
  4181. if (tsx->method.id != PJSIP_BYE_METHOD && session_end_if_disconnected(id, inv)) {
  4182. SCOPE_EXIT_RTN("Disconnected\n");
  4183. }
  4184. switch (e->body.tsx_state.type) {
  4185. case PJSIP_EVENT_TX_MSG:
  4186. /* When we create an outgoing request, we do not have access to the transaction that
  4187. * is created. Instead, We have to place transaction-specific data in the tdata. Here,
  4188. * we transfer the data into the transaction. This way, when we receive a response, we
  4189. * can dig this data out again
  4190. */
  4191. tsx->mod_data[id] = e->body.tsx_state.src.tdata->mod_data[id];
  4192. break;
  4193. case PJSIP_EVENT_RX_MSG:
  4194. cb = ast_sip_mod_data_get(tsx->mod_data, id, MOD_DATA_ON_RESPONSE);
  4195. /* As the PJSIP invite session implementation responds with a 200 OK before we have a
  4196. * chance to be invoked session supplements for BYE requests actually end up executing
  4197. * in the invite session state callback as well. To prevent session supplements from
  4198. * running on the BYE request again we explicitly squash invocation of them here.
  4199. */
  4200. if ((e->body.tsx_state.src.rdata->msg_info.msg->type != PJSIP_REQUEST_MSG) ||
  4201. (tsx->method.id != PJSIP_BYE_METHOD)) {
  4202. handle_incoming(session, e->body.tsx_state.src.rdata,
  4203. AST_SIP_SESSION_AFTER_MEDIA);
  4204. }
  4205. if (tsx->method.id == PJSIP_INVITE_METHOD) {
  4206. if (tsx->role == PJSIP_ROLE_UAC) {
  4207. if (tsx->state == PJSIP_TSX_STATE_COMPLETED) {
  4208. /* This means we got a non 2XX final response to our outgoing INVITE */
  4209. if (tsx->status_code == PJSIP_SC_REQUEST_PENDING) {
  4210. reschedule_reinvite(session, cb);
  4211. SCOPE_EXIT_RTN("Non 2XX final response\n");
  4212. }
  4213. if (inv->state == PJSIP_INV_STATE_CONFIRMED) {
  4214. ast_debug(1, "%s: reINVITE received final response code %d\n",
  4215. ast_sip_session_get_name(session),
  4216. tsx->status_code);
  4217. if ((tsx->status_code == 401 || tsx->status_code == 407
  4218. || (session->endpoint->security_negotiation && tsx->status_code == 494))
  4219. && ++session->authentication_challenge_count < MAX_RX_CHALLENGES
  4220. && !ast_sip_create_request_with_auth(
  4221. &session->endpoint->outbound_auths,
  4222. e->body.tsx_state.src.rdata, tsx->last_tx, &tdata)) {
  4223. /* Send authed reINVITE */
  4224. ast_sip_session_send_request_with_cb(session, tdata, cb);
  4225. SCOPE_EXIT_RTN("Sending authed reinvite\n");
  4226. }
  4227. /* Per RFC3261 14.1 a response to a re-INVITE should only terminate
  4228. * the dialog if a 481 or 408 occurs. All other responses should leave
  4229. * the dialog untouched.
  4230. */
  4231. if (tsx->status_code == 481 || tsx->status_code == 408) {
  4232. if (pjsip_inv_end_session(inv, 500, NULL, &tdata) == PJ_SUCCESS
  4233. && tdata) {
  4234. ast_sip_session_send_request(session, tdata);
  4235. }
  4236. }
  4237. }
  4238. } else if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
  4239. if (!inv->cancelling
  4240. && inv->role == PJSIP_ROLE_UAC
  4241. && inv->state == PJSIP_INV_STATE_CONFIRMED
  4242. && pjmedia_sdp_neg_was_answer_remote(inv->neg)
  4243. && pjmedia_sdp_neg_get_state(inv->neg) == PJMEDIA_SDP_NEG_STATE_DONE
  4244. && (session->channel && ast_channel_hangupcause(session->channel) == AST_CAUSE_BEARERCAPABILITY_NOTAVAIL)
  4245. ) {
  4246. /*
  4247. * We didn't send a CANCEL but the UAS sent us the 200 OK with an invalid or unacceptable codec SDP.
  4248. * In this case the SDP negotiation is incomplete and PJPROJECT has already sent the ACK.
  4249. * So, we send the BYE with 503 status code here. And the actual hangup cause code is already set
  4250. * to AST_CAUSE_BEARERCAPABILITY_NOTAVAIL by the session_inv_on_media_update(), setting the 503
  4251. * status code doesn't affect to hangup cause code.
  4252. */
  4253. ast_debug(1, "Endpoint '%s(%s)': Ending session due to 200 OK with incomplete SDP negotiation. %s\n",
  4254. ast_sorcery_object_get_id(session->endpoint),
  4255. session->channel ? ast_channel_name(session->channel) : "",
  4256. pjsip_rx_data_get_info(e->body.tsx_state.src.rdata));
  4257. pjsip_inv_end_session(session->inv_session, 503, NULL, &tdata);
  4258. SCOPE_EXIT_RTN("Incomplete SDP negotiation\n");
  4259. }
  4260. if (inv->cancelling && tsx->status_code == PJSIP_SC_OK) {
  4261. int sdp_negotiation_done =
  4262. pjmedia_sdp_neg_get_state(inv->neg) == PJMEDIA_SDP_NEG_STATE_DONE;
  4263. /*
  4264. * We can get here for the following reasons.
  4265. *
  4266. * 1) The race condition detailed in RFC5407 section 3.1.2.
  4267. * We sent a CANCEL at the same time that the UAS sent us a
  4268. * 200 OK with a valid SDP for the original INVITE. As a
  4269. * result, we have now received a 200 OK for a cancelled
  4270. * call and the SDP negotiation is complete. We need to
  4271. * immediately send a BYE to end the dialog.
  4272. *
  4273. * 2) We sent a CANCEL and hit the race condition but the
  4274. * UAS sent us an invalid SDP with the 200 OK. In this case
  4275. * the SDP negotiation is incomplete and PJPROJECT has
  4276. * already sent the BYE for us because of the invalid SDP.
  4277. */
  4278. ast_test_suite_event_notify("PJSIP_SESSION_CANCELED",
  4279. "Endpoint: %s\r\n"
  4280. "Channel: %s\r\n"
  4281. "Message: %s\r\n"
  4282. "SDP: %s",
  4283. ast_sorcery_object_get_id(session->endpoint),
  4284. session->channel ? ast_channel_name(session->channel) : "",
  4285. pjsip_rx_data_get_info(e->body.tsx_state.src.rdata),
  4286. sdp_negotiation_done ? "complete" : "incomplete");
  4287. if (!sdp_negotiation_done) {
  4288. ast_debug(1, "%s: Incomplete SDP negotiation cancelled session. %s\n",
  4289. ast_sip_session_get_name(session),
  4290. pjsip_rx_data_get_info(e->body.tsx_state.src.rdata));
  4291. } else if (pjsip_inv_end_session(inv, 500, NULL, &tdata) == PJ_SUCCESS
  4292. && tdata) {
  4293. ast_debug(1, "%s: Ending session due to RFC5407 race condition. %s\n",
  4294. ast_sip_session_get_name(session),
  4295. pjsip_rx_data_get_info(e->body.tsx_state.src.rdata));
  4296. ast_sip_session_send_request(session, tdata);
  4297. }
  4298. }
  4299. }
  4300. }
  4301. } else {
  4302. /* All other methods */
  4303. if (tsx->role == PJSIP_ROLE_UAC) {
  4304. if (tsx->state == PJSIP_TSX_STATE_COMPLETED) {
  4305. /* This means we got a final response to our outgoing method */
  4306. ast_debug(1, "%s: %.*s received final response code %d\n",
  4307. ast_sip_session_get_name(session),
  4308. (int) pj_strlen(&tsx->method.name), pj_strbuf(&tsx->method.name),
  4309. tsx->status_code);
  4310. if ((tsx->status_code == 401 || tsx->status_code == 407 || tsx->status_code == 494)
  4311. && ++session->authentication_challenge_count < MAX_RX_CHALLENGES
  4312. && !ast_sip_create_request_with_auth(
  4313. &session->endpoint->outbound_auths,
  4314. e->body.tsx_state.src.rdata, tsx->last_tx, &tdata)) {
  4315. /* Send authed version of the method */
  4316. ast_sip_session_send_request_with_cb(session, tdata, cb);
  4317. SCOPE_EXIT_RTN("Sending authed %.*s\n",
  4318. (int) pj_strlen(&tsx->method.name), pj_strbuf(&tsx->method.name));
  4319. }
  4320. }
  4321. }
  4322. }
  4323. if (cb) {
  4324. cb(session, e->body.tsx_state.src.rdata);
  4325. }
  4326. break;
  4327. case PJSIP_EVENT_TRANSPORT_ERROR:
  4328. case PJSIP_EVENT_TIMER:
  4329. /*
  4330. * The timer event is run by the pjsip monitor thread and not
  4331. * by the session serializer.
  4332. */
  4333. if (session_end_if_disconnected(id, inv)) {
  4334. SCOPE_EXIT_RTN("Disconnected\n");
  4335. }
  4336. break;
  4337. case PJSIP_EVENT_USER:
  4338. case PJSIP_EVENT_UNKNOWN:
  4339. case PJSIP_EVENT_TSX_STATE:
  4340. /* Inception? */
  4341. break;
  4342. }
  4343. if (AST_LIST_EMPTY(&session->delayed_requests)) {
  4344. /* No delayed request pending, so just return */
  4345. SCOPE_EXIT_RTN("Nothing delayed\n");
  4346. }
  4347. if (tsx->method.id == PJSIP_INVITE_METHOD) {
  4348. if (tsx->state == PJSIP_TSX_STATE_PROCEEDING) {
  4349. ast_debug(3, "%s: INVITE delay check. tsx-state:%s\n",
  4350. ast_sip_session_get_name(session),
  4351. pjsip_tsx_state_str(tsx->state));
  4352. check_delayed_requests(session, invite_proceeding);
  4353. } else if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
  4354. /*
  4355. * Terminated INVITE transactions always should result in
  4356. * queuing delayed requests, no matter what event caused
  4357. * the transaction to terminate.
  4358. */
  4359. ast_debug(3, "%s: INVITE delay check. tsx-state:%s\n",
  4360. ast_sip_session_get_name(session),
  4361. pjsip_tsx_state_str(tsx->state));
  4362. check_delayed_requests(session, invite_terminated);
  4363. }
  4364. } else if (tsx->role == PJSIP_ROLE_UAC
  4365. && tsx->state == PJSIP_TSX_STATE_COMPLETED
  4366. && !pj_strcmp2(&tsx->method.name, "UPDATE")) {
  4367. ast_debug(3, "%s: UPDATE delay check. tsx-state:%s\n",
  4368. ast_sip_session_get_name(session),
  4369. pjsip_tsx_state_str(tsx->state));
  4370. check_delayed_requests(session, update_completed);
  4371. }
  4372. SCOPE_EXIT_RTN();
  4373. }
  4374. static int add_sdp_streams(struct ast_sip_session_media *session_media,
  4375. struct ast_sip_session *session, pjmedia_sdp_session *answer,
  4376. const struct pjmedia_sdp_session *remote,
  4377. struct ast_stream *stream)
  4378. {
  4379. struct ast_sip_session_sdp_handler *handler = session_media->handler;
  4380. RAII_VAR(struct sdp_handler_list *, handler_list, NULL, ao2_cleanup);
  4381. int res = 0;
  4382. SCOPE_ENTER(1, "%s Stream: %s\n", ast_sip_session_get_name(session),
  4383. ast_str_tmp(128, ast_stream_to_str(stream, &STR_TMP)));
  4384. if (handler) {
  4385. /* if an already assigned handler reports a catastrophic error, fail */
  4386. res = handler->create_outgoing_sdp_stream(session, session_media, answer, remote, stream);
  4387. if (res < 0) {
  4388. SCOPE_EXIT_RTN_VALUE(-1, "Coudn't create sdp stream\n");
  4389. }
  4390. SCOPE_EXIT_RTN_VALUE(0, "Had handler\n");
  4391. }
  4392. handler_list = ao2_find(sdp_handlers, ast_codec_media_type2str(session_media->type), OBJ_KEY);
  4393. if (!handler_list) {
  4394. SCOPE_EXIT_RTN_VALUE(0, "No handlers\n");
  4395. }
  4396. /* no handler for this stream type and we have a list to search */
  4397. AST_LIST_TRAVERSE(&handler_list->list, handler, next) {
  4398. if (handler == session_media->handler) {
  4399. continue;
  4400. }
  4401. res = handler->create_outgoing_sdp_stream(session, session_media, answer, remote, stream);
  4402. if (res < 0) {
  4403. /* catastrophic error */
  4404. SCOPE_EXIT_RTN_VALUE(-1, "Coudn't create sdp stream\n");
  4405. }
  4406. if (res > 0) {
  4407. /* Handled by this handler. Move to the next stream */
  4408. session_media_set_handler(session_media, handler);
  4409. SCOPE_EXIT_RTN_VALUE(0, "Handled\n");
  4410. }
  4411. }
  4412. /* streams that weren't handled won't be included in generated outbound SDP */
  4413. SCOPE_EXIT_RTN_VALUE(0, "Not handled\n");
  4414. }
  4415. /*! \brief Bundle group building structure */
  4416. struct sip_session_media_bundle_group {
  4417. /*! \brief The media identifiers in this bundle group */
  4418. char *mids[PJMEDIA_MAX_SDP_MEDIA];
  4419. /*! \brief SDP attribute string */
  4420. struct ast_str *attr_string;
  4421. };
  4422. static int add_bundle_groups(struct ast_sip_session *session, pj_pool_t *pool, pjmedia_sdp_session *answer)
  4423. {
  4424. pj_str_t stmp;
  4425. pjmedia_sdp_attr *attr;
  4426. struct sip_session_media_bundle_group bundle_groups[PJMEDIA_MAX_SDP_MEDIA];
  4427. int index, mid_id;
  4428. struct sip_session_media_bundle_group *bundle_group;
  4429. if (session->endpoint->media.webrtc) {
  4430. attr = pjmedia_sdp_attr_create(pool, "msid-semantic", pj_cstr(&stmp, "WMS *"));
  4431. pjmedia_sdp_attr_add(&answer->attr_count, answer->attr, attr);
  4432. }
  4433. if (!session->endpoint->media.bundle) {
  4434. return 0;
  4435. }
  4436. memset(bundle_groups, 0, sizeof(bundle_groups));
  4437. /* Build the bundle group layout so we can then add it to the SDP */
  4438. for (index = 0; index < AST_VECTOR_SIZE(&session->pending_media_state->sessions); ++index) {
  4439. struct ast_sip_session_media *session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, index);
  4440. /* If this stream is not part of a bundle group we can't add it */
  4441. if (session_media->bundle_group == -1) {
  4442. continue;
  4443. }
  4444. bundle_group = &bundle_groups[session_media->bundle_group];
  4445. /* If this is the first mid then we need to allocate the attribute string and place BUNDLE in front */
  4446. if (!bundle_group->mids[0]) {
  4447. bundle_group->mids[0] = session_media->mid;
  4448. bundle_group->attr_string = ast_str_create(64);
  4449. if (!bundle_group->attr_string) {
  4450. continue;
  4451. }
  4452. ast_str_set(&bundle_group->attr_string, 0, "BUNDLE %s", session_media->mid);
  4453. continue;
  4454. }
  4455. for (mid_id = 1; mid_id < PJMEDIA_MAX_SDP_MEDIA; ++mid_id) {
  4456. if (!bundle_group->mids[mid_id]) {
  4457. bundle_group->mids[mid_id] = session_media->mid;
  4458. ast_str_append(&bundle_group->attr_string, 0, " %s", session_media->mid);
  4459. break;
  4460. } else if (!strcmp(bundle_group->mids[mid_id], session_media->mid)) {
  4461. break;
  4462. }
  4463. }
  4464. }
  4465. /* Add all bundle groups that have mids to the SDP */
  4466. for (index = 0; index < PJMEDIA_MAX_SDP_MEDIA; ++index) {
  4467. bundle_group = &bundle_groups[index];
  4468. if (!bundle_group->attr_string) {
  4469. continue;
  4470. }
  4471. attr = pjmedia_sdp_attr_create(pool, "group", pj_cstr(&stmp, ast_str_buffer(bundle_group->attr_string)));
  4472. pjmedia_sdp_attr_add(&answer->attr_count, answer->attr, attr);
  4473. ast_free(bundle_group->attr_string);
  4474. }
  4475. return 0;
  4476. }
  4477. static struct pjmedia_sdp_session *create_local_sdp(pjsip_inv_session *inv, struct ast_sip_session *session, const pjmedia_sdp_session *offer, const unsigned int ignore_active_stream_topology)
  4478. {
  4479. static const pj_str_t STR_IN = { "IN", 2 };
  4480. static const pj_str_t STR_IP4 = { "IP4", 3 };
  4481. static const pj_str_t STR_IP6 = { "IP6", 3 };
  4482. pjmedia_sdp_session *local;
  4483. int i;
  4484. int stream;
  4485. SCOPE_ENTER(3, "%s\n", ast_sip_session_get_name(session));
  4486. if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
  4487. SCOPE_EXIT_LOG_RTN_VALUE(NULL, LOG_ERROR, "%s: Failed to create session SDP. Session has been already disconnected\n",
  4488. ast_sip_session_get_name(session));
  4489. }
  4490. if (!inv->pool_prov || !(local = PJ_POOL_ZALLOC_T(inv->pool_prov, pjmedia_sdp_session))) {
  4491. SCOPE_EXIT_LOG_RTN_VALUE(NULL, LOG_ERROR, "%s: Pool allocation failure\n", ast_sip_session_get_name(session));
  4492. }
  4493. if (!offer) {
  4494. local->origin.version = local->origin.id = (pj_uint32_t)(ast_random());
  4495. } else {
  4496. local->origin.version = offer->origin.version + 1;
  4497. local->origin.id = offer->origin.id;
  4498. }
  4499. pj_strdup2(inv->pool_prov, &local->origin.user, session->endpoint->media.sdpowner);
  4500. pj_strdup2(inv->pool_prov, &local->name, session->endpoint->media.sdpsession);
  4501. if (!session->pending_media_state->topology || !ast_stream_topology_get_count(session->pending_media_state->topology)) {
  4502. /* We've encountered a situation where we have been told to create a local SDP but noone has given us any indication
  4503. * of what kind of stream topology they would like. We try to not alter the current state of the SDP negotiation
  4504. * by using what is currently negotiated. If this is unavailable we fall back to what is configured on the endpoint.
  4505. * We will also do this if wanted by the ignore_active_stream_topology flag.
  4506. */
  4507. ast_trace(-1, "no information about stream topology received\n");
  4508. ast_stream_topology_free(session->pending_media_state->topology);
  4509. if (session->active_media_state->topology && !ignore_active_stream_topology) {
  4510. ast_trace(-1, "using existing topology\n");
  4511. session->pending_media_state->topology = ast_stream_topology_clone(session->active_media_state->topology);
  4512. } else {
  4513. if (ignore_active_stream_topology) {
  4514. ast_trace(-1, "fall back to endpoint configuration - ignore active stream topolog\n");
  4515. } else {
  4516. ast_trace(-1, "fall back to endpoint configuration\n");
  4517. }
  4518. session->pending_media_state->topology = ast_stream_topology_clone(session->endpoint->media.topology);
  4519. }
  4520. if (!session->pending_media_state->topology) {
  4521. SCOPE_EXIT_LOG_RTN_VALUE(NULL, LOG_ERROR, "%s: No pending media state topology\n", ast_sip_session_get_name(session));
  4522. }
  4523. }
  4524. ast_trace(-1, "%s: Processing streams\n", ast_sip_session_get_name(session));
  4525. for (i = 0; i < ast_stream_topology_get_count(session->pending_media_state->topology); ++i) {
  4526. struct ast_sip_session_media *session_media;
  4527. struct ast_stream *stream = ast_stream_topology_get_stream(session->pending_media_state->topology, i);
  4528. unsigned int streams = local->media_count;
  4529. SCOPE_ENTER(4, "%s: Processing stream %s\n", ast_sip_session_get_name(session),
  4530. ast_str_tmp(128, ast_stream_to_str(stream, &STR_TMP)));
  4531. /* This code does not enforce any maximum stream count limitations as that is done on either
  4532. * the handling of an incoming SDP offer or on the handling of a session refresh.
  4533. */
  4534. session_media = ast_sip_session_media_state_add(session, session->pending_media_state, ast_stream_get_type(stream), i);
  4535. if (!session_media) {
  4536. local = NULL;
  4537. SCOPE_EXIT_LOG_EXPR(goto end, LOG_ERROR, "%s: Couldn't alloc/add session media for stream %s\n",
  4538. ast_sip_session_get_name(session), ast_str_tmp(128, ast_stream_to_str(stream, &STR_TMP)));
  4539. }
  4540. if (add_sdp_streams(session_media, session, local, offer, stream)) {
  4541. local = NULL;
  4542. SCOPE_EXIT_LOG_EXPR(goto end, LOG_ERROR, "%s: Couldn't add sdp streams for stream %s\n",
  4543. ast_sip_session_get_name(session), ast_str_tmp(128, ast_stream_to_str(stream, &STR_TMP)));
  4544. }
  4545. /* If a stream was actually added then add any additional details */
  4546. if (streams != local->media_count) {
  4547. pjmedia_sdp_media *media = local->media[streams];
  4548. pj_str_t stmp;
  4549. pjmedia_sdp_attr *attr;
  4550. /* Add the media identifier if present */
  4551. if (!ast_strlen_zero(session_media->mid)) {
  4552. attr = pjmedia_sdp_attr_create(inv->pool_prov, "mid", pj_cstr(&stmp, session_media->mid));
  4553. pjmedia_sdp_attr_add(&media->attr_count, media->attr, attr);
  4554. }
  4555. ast_trace(-1, "%s: Stream %s added%s%s\n", ast_sip_session_get_name(session),
  4556. ast_str_tmp(128, ast_stream_to_str(stream, &STR_TMP)),
  4557. S_COR(!ast_strlen_zero(session_media->mid), " with mid ", ""), S_OR(session_media->mid, ""));
  4558. }
  4559. /* Ensure that we never exceed the maximum number of streams PJMEDIA will allow. */
  4560. if (local->media_count == PJMEDIA_MAX_SDP_MEDIA) {
  4561. SCOPE_EXIT_EXPR(break, "%s: Stream %s exceeded max pjmedia count of %d\n",
  4562. ast_sip_session_get_name(session), ast_str_tmp(128, ast_stream_to_str(stream, &STR_TMP)),
  4563. PJMEDIA_MAX_SDP_MEDIA);
  4564. }
  4565. SCOPE_EXIT("%s: Done with %s\n", ast_sip_session_get_name(session),
  4566. ast_str_tmp(128, ast_stream_to_str(stream, &STR_TMP)));
  4567. }
  4568. /* Add any bundle groups that are present on the media state */
  4569. ast_trace(-1, "%s: Adding bundle groups (if available)\n", ast_sip_session_get_name(session));
  4570. if (add_bundle_groups(session, inv->pool_prov, local)) {
  4571. SCOPE_EXIT_LOG_RTN_VALUE(NULL, LOG_ERROR, "%s: Couldn't add bundle groups\n", ast_sip_session_get_name(session));
  4572. }
  4573. /* Use the connection details of an available media if possible for SDP level */
  4574. ast_trace(-1, "%s: Copying connection details\n", ast_sip_session_get_name(session));
  4575. for (stream = 0; stream < local->media_count; stream++) {
  4576. SCOPE_ENTER(4, "%s: Processing media %d\n", ast_sip_session_get_name(session), stream);
  4577. if (!local->media[stream]->conn) {
  4578. SCOPE_EXIT_EXPR(continue, "%s: Media %d has no connection info\n", ast_sip_session_get_name(session), stream);
  4579. }
  4580. if (local->conn) {
  4581. if (!pj_strcmp(&local->conn->net_type, &local->media[stream]->conn->net_type) &&
  4582. !pj_strcmp(&local->conn->addr_type, &local->media[stream]->conn->addr_type) &&
  4583. !pj_strcmp(&local->conn->addr, &local->media[stream]->conn->addr)) {
  4584. local->media[stream]->conn = NULL;
  4585. }
  4586. SCOPE_EXIT_EXPR(continue, "%s: Media %d has good existing connection info\n", ast_sip_session_get_name(session), stream);
  4587. }
  4588. /* This stream's connection info will serve as the connection details for SDP level */
  4589. local->conn = local->media[stream]->conn;
  4590. local->media[stream]->conn = NULL;
  4591. SCOPE_EXIT_EXPR(continue, "%s: Media %d reset\n", ast_sip_session_get_name(session), stream);
  4592. }
  4593. /* If no SDP level connection details are present then create some */
  4594. if (!local->conn) {
  4595. ast_trace(-1, "%s: Creating connection details\n", ast_sip_session_get_name(session));
  4596. local->conn = pj_pool_zalloc(inv->pool_prov, sizeof(struct pjmedia_sdp_conn));
  4597. local->conn->net_type = STR_IN;
  4598. local->conn->addr_type = session->endpoint->media.rtp.ipv6 ? STR_IP6 : STR_IP4;
  4599. if (!ast_strlen_zero(session->endpoint->media.address)) {
  4600. pj_strdup2(inv->pool_prov, &local->conn->addr, session->endpoint->media.address);
  4601. } else {
  4602. pj_strdup2(inv->pool_prov, &local->conn->addr, ast_sip_get_host_ip_string(session->endpoint->media.rtp.ipv6 ? pj_AF_INET6() : pj_AF_INET()));
  4603. }
  4604. }
  4605. pj_strassign(&local->origin.net_type, &local->conn->net_type);
  4606. pj_strassign(&local->origin.addr_type, &local->conn->addr_type);
  4607. pj_strassign(&local->origin.addr, &local->conn->addr);
  4608. end:
  4609. SCOPE_EXIT_RTN_VALUE(local, "%s\n", ast_sip_session_get_name(session));
  4610. }
  4611. static void session_inv_on_rx_offer(pjsip_inv_session *inv, const pjmedia_sdp_session *offer)
  4612. {
  4613. struct ast_sip_session *session = inv->mod_data[session_module.id];
  4614. pjmedia_sdp_session *answer;
  4615. SCOPE_ENTER(3, "%s\n", ast_sip_session_get_name(session));
  4616. if (ast_shutdown_final()) {
  4617. SCOPE_EXIT_RTN("%s: Shutdown in progress\n", ast_sip_session_get_name(session));
  4618. }
  4619. session = inv->mod_data[session_module.id];
  4620. if (handle_incoming_sdp(session, offer)) {
  4621. ast_sip_session_media_state_reset(session->pending_media_state);
  4622. SCOPE_EXIT_RTN("%s: handle_incoming_sdp failed\n", ast_sip_session_get_name(session));
  4623. }
  4624. if ((answer = create_local_sdp(inv, session, offer, 0))) {
  4625. pjsip_inv_set_sdp_answer(inv, answer);
  4626. SCOPE_EXIT_RTN("%s: Set SDP answer\n", ast_sip_session_get_name(session));
  4627. }
  4628. SCOPE_EXIT_RTN("%s: create_local_sdp failed\n", ast_sip_session_get_name(session));
  4629. }
  4630. static void session_inv_on_create_offer(pjsip_inv_session *inv, pjmedia_sdp_session **p_offer)
  4631. {
  4632. struct ast_sip_session *session = inv->mod_data[session_module.id];
  4633. const pjmedia_sdp_session *previous_sdp = NULL;
  4634. pjmedia_sdp_session *offer;
  4635. int i;
  4636. unsigned int ignore_active_stream_topology = 0;
  4637. /* We allow PJSIP to produce an SDP if no channel is present. This may result
  4638. * in an incorrect SDP occurring, but if no channel is present then we are in
  4639. * the midst of a BYE and are hanging up. This ensures that all the code to
  4640. * produce an SDP doesn't need to worry about a channel being present or not,
  4641. * just in case.
  4642. */
  4643. SCOPE_ENTER(3, "%s\n", ast_sip_session_get_name(session));
  4644. if (!session->channel) {
  4645. SCOPE_EXIT_RTN("%s: No channel\n", ast_sip_session_get_name(session));
  4646. }
  4647. /* Some devices send a re-INVITE offer with empty SDP. Asterisk by default return
  4648. * an answer with the current used codecs, which is not strictly compliant to RFC
  4649. * 3261 (SHOULD requirement). So we detect this condition and include all
  4650. * configured codecs in the answer if the workaround is activated. The actual
  4651. * logic is in the create_local_sdp function. We can't detect here that we have
  4652. * no SDP body in the INVITE, as we don't have access to the message.
  4653. */
  4654. if (inv->invite_tsx && inv->state == PJSIP_INV_STATE_CONFIRMED
  4655. && inv->invite_tsx->method.id == PJSIP_INVITE_METHOD) {
  4656. ast_trace(-1, "re-INVITE\n");
  4657. if (inv->invite_tsx->role == PJSIP_ROLE_UAS
  4658. && ast_sip_get_all_codecs_on_empty_reinvite()) {
  4659. ast_trace(-1, "UAS role, include all codecs in the answer on empty SDP\n");
  4660. ignore_active_stream_topology = 1;
  4661. }
  4662. }
  4663. if (inv->neg) {
  4664. if (pjmedia_sdp_neg_was_answer_remote(inv->neg)) {
  4665. pjmedia_sdp_neg_get_active_remote(inv->neg, &previous_sdp);
  4666. } else {
  4667. pjmedia_sdp_neg_get_active_local(inv->neg, &previous_sdp);
  4668. }
  4669. }
  4670. if (ignore_active_stream_topology) {
  4671. offer = create_local_sdp(inv, session, NULL, 1);
  4672. } else {
  4673. offer = create_local_sdp(inv, session, previous_sdp, 0);
  4674. }
  4675. if (!offer) {
  4676. SCOPE_EXIT_RTN("%s: create offer failed\n", ast_sip_session_get_name(session));
  4677. }
  4678. ast_queue_unhold(session->channel);
  4679. /*
  4680. * Some devices indicate hold with deferred SDP reinvites (i.e. no SDP in the reinvite).
  4681. * When hold is initially indicated, we
  4682. * - Receive an INVITE with no SDP
  4683. * - Send a 200 OK with SDP, indicating sendrecv in the media streams
  4684. * - Receive an ACK with SDP, indicating sendonly in the media streams
  4685. *
  4686. * At this point, the pjmedia negotiator saves the state of the media direction so that
  4687. * if we are to send any offers, we'll offer recvonly in the media streams. This is
  4688. * problematic if the device is attempting to unhold, though. If the device unholds
  4689. * by sending a reinvite with no SDP, then we will respond with a 200 OK with recvonly.
  4690. * According to RFC 3264, if an offerer offers recvonly, then the answerer MUST respond
  4691. * with sendonly or inactive. The result of this is that the stream is not off hold.
  4692. *
  4693. * Therefore, in this case, when we receive a reinvite while the stream is on hold, we
  4694. * need to be sure to offer sendrecv. This way, the answerer can respond with sendrecv
  4695. * in order to get the stream off hold. If this is actually a different purpose reinvite
  4696. * (like a session timer refresh), then the answerer can respond to our sendrecv with
  4697. * sendonly, keeping the stream on hold.
  4698. */
  4699. for (i = 0; i < offer->media_count; ++i) {
  4700. pjmedia_sdp_media *m = offer->media[i];
  4701. pjmedia_sdp_attr *recvonly;
  4702. pjmedia_sdp_attr *inactive;
  4703. pjmedia_sdp_attr *sendonly;
  4704. recvonly = pjmedia_sdp_attr_find2(m->attr_count, m->attr, "recvonly", NULL);
  4705. inactive = pjmedia_sdp_attr_find2(m->attr_count, m->attr, "inactive", NULL);
  4706. sendonly = pjmedia_sdp_attr_find2(m->attr_count, m->attr, "sendonly", NULL);
  4707. if (recvonly || inactive || sendonly) {
  4708. pjmedia_sdp_attr *to_remove = recvonly ?: inactive ?: sendonly;
  4709. pjmedia_sdp_attr *sendrecv;
  4710. pjmedia_sdp_attr_remove(&m->attr_count, m->attr, to_remove);
  4711. sendrecv = pjmedia_sdp_attr_create(session->inv_session->pool, "sendrecv", NULL);
  4712. pjmedia_sdp_media_add_attr(m, sendrecv);
  4713. }
  4714. }
  4715. *p_offer = offer;
  4716. SCOPE_EXIT_RTN("%s: offer created\n", ast_sip_session_get_name(session));
  4717. }
  4718. static void session_inv_on_media_update(pjsip_inv_session *inv, pj_status_t status)
  4719. {
  4720. struct ast_sip_session *session = inv->mod_data[session_module.id];
  4721. const pjmedia_sdp_session *local, *remote;
  4722. SCOPE_ENTER(3, "%s\n", ast_sip_session_get_name(session));
  4723. if (ast_shutdown_final()) {
  4724. SCOPE_EXIT_RTN("%s: Shutdown in progress\n", ast_sip_session_get_name(session));
  4725. }
  4726. session = inv->mod_data[session_module.id];
  4727. if (!session || !session->channel) {
  4728. /*
  4729. * If we don't have a session or channel then we really
  4730. * don't care about media updates.
  4731. * Just ignore
  4732. */
  4733. SCOPE_EXIT_RTN("%s: No channel or session\n", ast_sip_session_get_name(session));
  4734. }
  4735. if (session->endpoint) {
  4736. int bail = 0;
  4737. /*
  4738. * If following_fork is set, then this is probably the result of a
  4739. * forked INVITE and SDP asnwers coming from the different fork UAS
  4740. * destinations. In this case updated_sdp_answer will also be set.
  4741. *
  4742. * If only updated_sdp_answer is set, then this is the non-forking
  4743. * scenario where the same UAS just needs to change something like
  4744. * the media port.
  4745. */
  4746. if (inv->following_fork) {
  4747. if (session->endpoint->media.rtp.follow_early_media_fork) {
  4748. ast_trace(-1, "%s: Following early media fork with different To tags\n", ast_sip_session_get_name(session));
  4749. } else {
  4750. ast_trace(-1, "%s: Not following early media fork with different To tags\n", ast_sip_session_get_name(session));
  4751. bail = 1;
  4752. }
  4753. }
  4754. #ifdef HAVE_PJSIP_INV_ACCEPT_MULTIPLE_SDP_ANSWERS
  4755. else if (inv->updated_sdp_answer) {
  4756. if (session->endpoint->media.rtp.accept_multiple_sdp_answers) {
  4757. ast_trace(-1, "%s: Accepting updated SDP with same To tag\n", ast_sip_session_get_name(session));
  4758. } else {
  4759. ast_trace(-1, "%s: Ignoring updated SDP answer with same To tag\n", ast_sip_session_get_name(session));
  4760. bail = 1;
  4761. }
  4762. }
  4763. #endif
  4764. if (bail) {
  4765. SCOPE_EXIT_RTN("%s: Bailing\n", ast_sip_session_get_name(session));
  4766. }
  4767. }
  4768. if ((status != PJ_SUCCESS) || (pjmedia_sdp_neg_get_active_local(inv->neg, &local) != PJ_SUCCESS) ||
  4769. (pjmedia_sdp_neg_get_active_remote(inv->neg, &remote) != PJ_SUCCESS)) {
  4770. ast_channel_hangupcause_set(session->channel, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
  4771. ast_set_hangupsource(session->channel, ast_channel_name(session->channel), 0);
  4772. ast_queue_hangup(session->channel);
  4773. SCOPE_EXIT_RTN("%s: Couldn't get active or local or remote negotiator. Hanging up\n", ast_sip_session_get_name(session));
  4774. }
  4775. if (handle_negotiated_sdp(session, local, remote)) {
  4776. ast_sip_session_media_state_reset(session->pending_media_state);
  4777. SCOPE_EXIT_RTN("%s: handle_negotiated_sdp failed. Resetting pending media state\n", ast_sip_session_get_name(session));
  4778. }
  4779. SCOPE_EXIT_RTN("%s\n", ast_sip_session_get_name(session));
  4780. }
  4781. static pjsip_redirect_op session_inv_on_redirected(pjsip_inv_session *inv, const pjsip_uri *target, const pjsip_event *e)
  4782. {
  4783. struct ast_sip_session *session;
  4784. const pjsip_sip_uri *uri;
  4785. if (ast_shutdown_final()) {
  4786. return PJSIP_REDIRECT_STOP;
  4787. }
  4788. session = inv->mod_data[session_module.id];
  4789. if (!session || !session->channel) {
  4790. return PJSIP_REDIRECT_STOP;
  4791. }
  4792. if (session->endpoint->redirect_method == AST_SIP_REDIRECT_URI_PJSIP) {
  4793. return PJSIP_REDIRECT_ACCEPT;
  4794. }
  4795. if (!PJSIP_URI_SCHEME_IS_SIP(target) && !PJSIP_URI_SCHEME_IS_SIPS(target)) {
  4796. return PJSIP_REDIRECT_STOP;
  4797. }
  4798. handle_incoming(session, e->body.rx_msg.rdata, AST_SIP_SESSION_BEFORE_REDIRECTING);
  4799. uri = pjsip_uri_get_uri(target);
  4800. if (session->endpoint->redirect_method == AST_SIP_REDIRECT_USER) {
  4801. char exten[AST_MAX_EXTENSION];
  4802. ast_copy_pj_str(exten, &uri->user, sizeof(exten));
  4803. /*
  4804. * We may want to match in the dialplan without any user
  4805. * options getting in the way.
  4806. */
  4807. AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(exten);
  4808. ast_channel_call_forward_set(session->channel, exten);
  4809. } else if (session->endpoint->redirect_method == AST_SIP_REDIRECT_URI_CORE) {
  4810. char target_uri[PJSIP_MAX_URL_SIZE];
  4811. /* PJSIP/ + endpoint length + / + max URL size */
  4812. char forward[8 + strlen(ast_sorcery_object_get_id(session->endpoint)) + PJSIP_MAX_URL_SIZE];
  4813. pjsip_uri_print(PJSIP_URI_IN_REQ_URI, uri, target_uri, sizeof(target_uri));
  4814. sprintf(forward, "PJSIP/%s/%s", ast_sorcery_object_get_id(session->endpoint), target_uri);
  4815. ast_channel_call_forward_set(session->channel, forward);
  4816. }
  4817. return PJSIP_REDIRECT_STOP;
  4818. }
  4819. static pjsip_inv_callback inv_callback = {
  4820. .on_state_changed = session_inv_on_state_changed,
  4821. .on_new_session = session_inv_on_new_session,
  4822. .on_tsx_state_changed = session_inv_on_tsx_state_changed,
  4823. .on_rx_offer = session_inv_on_rx_offer,
  4824. .on_create_offer = session_inv_on_create_offer,
  4825. .on_media_update = session_inv_on_media_update,
  4826. .on_redirected = session_inv_on_redirected,
  4827. };
  4828. /*! \brief Hook for modifying outgoing messages with SDP to contain the proper address information */
  4829. static void session_outgoing_nat_hook(pjsip_tx_data *tdata, struct ast_sip_transport *transport)
  4830. {
  4831. RAII_VAR(struct ast_sip_transport_state *, transport_state, ast_sip_get_transport_state(ast_sorcery_object_get_id(transport)), ao2_cleanup);
  4832. struct ast_sip_nat_hook *hook = ast_sip_mod_data_get(
  4833. tdata->mod_data, session_module.id, MOD_DATA_NAT_HOOK);
  4834. pjsip_sdp_info *sdp_info;
  4835. pjmedia_sdp_session *sdp;
  4836. pjsip_dialog *dlg = pjsip_tdata_get_dlg(tdata);
  4837. RAII_VAR(struct ast_sip_session *, session, dlg ? ast_sip_dialog_get_session(dlg) : NULL, ao2_cleanup);
  4838. int stream;
  4839. /*
  4840. * If there's no transport_state or body, or the hook
  4841. * has already been run, just return.
  4842. */
  4843. if (ast_strlen_zero(transport->external_media_address) || !transport_state || hook || !tdata->msg->body) {
  4844. return;
  4845. }
  4846. sdp_info = pjsip_get_sdp_info(tdata->pool, tdata->msg->body, NULL, &pjsip_media_type_application_sdp);
  4847. if (sdp_info->sdp_err != PJ_SUCCESS || !sdp_info->sdp) {
  4848. return;
  4849. }
  4850. sdp = sdp_info->sdp;
  4851. if (sdp->conn) {
  4852. char host[NI_MAXHOST];
  4853. struct ast_sockaddr our_sdp_addr = { { 0, } };
  4854. ast_copy_pj_str(host, &sdp->conn->addr, sizeof(host));
  4855. ast_sockaddr_parse(&our_sdp_addr, host, PARSE_PORT_FORBID);
  4856. /* Reversed check here. We don't check the remote
  4857. * endpoint being in our local net, but whether our
  4858. * outgoing session IP is local. If it is, we'll do
  4859. * rewriting. No localnet configured? Always rewrite. */
  4860. if (ast_sip_transport_is_local(transport_state, &our_sdp_addr) || !transport_state->localnet) {
  4861. ast_debug(5, "%s: Setting external media address to %s\n", ast_sip_session_get_name(session),
  4862. ast_sockaddr_stringify_addr_remote(&transport_state->external_media_address));
  4863. pj_strdup2(tdata->pool, &sdp->conn->addr, ast_sockaddr_stringify_addr_remote(&transport_state->external_media_address));
  4864. pj_strassign(&sdp->origin.addr, &sdp->conn->addr);
  4865. }
  4866. }
  4867. for (stream = 0; stream < sdp->media_count; ++stream) {
  4868. /* See if there are registered handlers for this media stream type */
  4869. char media[20];
  4870. struct ast_sip_session_sdp_handler *handler;
  4871. RAII_VAR(struct sdp_handler_list *, handler_list, NULL, ao2_cleanup);
  4872. /* We need a null-terminated version of the media string */
  4873. ast_copy_pj_str(media, &sdp->media[stream]->desc.media, sizeof(media));
  4874. handler_list = ao2_find(sdp_handlers, media, OBJ_KEY);
  4875. if (!handler_list) {
  4876. ast_debug(4, "%s: No registered SDP handlers for media type '%s'\n", ast_sip_session_get_name(session),
  4877. media);
  4878. continue;
  4879. }
  4880. AST_LIST_TRAVERSE(&handler_list->list, handler, next) {
  4881. if (handler->change_outgoing_sdp_stream_media_address) {
  4882. handler->change_outgoing_sdp_stream_media_address(tdata, sdp->media[stream], transport);
  4883. }
  4884. }
  4885. }
  4886. /* We purposely do this so that the hook will not be invoked multiple times, ie: if a retransmit occurs */
  4887. ast_sip_mod_data_set(tdata->pool, tdata->mod_data, session_module.id, MOD_DATA_NAT_HOOK, nat_hook);
  4888. }
  4889. #ifdef TEST_FRAMEWORK
  4890. static struct ast_stream *test_stream_alloc(const char *name, enum ast_media_type type, enum ast_stream_state state)
  4891. {
  4892. struct ast_stream *stream;
  4893. stream = ast_stream_alloc(name, type);
  4894. if (!stream) {
  4895. return NULL;
  4896. }
  4897. ast_stream_set_state(stream, state);
  4898. return stream;
  4899. }
  4900. static struct ast_sip_session_media *test_media_add(
  4901. struct ast_sip_session_media_state *media_state, const char *name, enum ast_media_type type,
  4902. enum ast_stream_state state, int position)
  4903. {
  4904. struct ast_sip_session_media *session_media = NULL;
  4905. struct ast_stream *stream = NULL;
  4906. stream = test_stream_alloc(name, type, state);
  4907. if (!stream) {
  4908. return NULL;
  4909. }
  4910. if (position >= 0 && position < ast_stream_topology_get_count(media_state->topology)) {
  4911. ast_stream_topology_set_stream(media_state->topology, position, stream);
  4912. } else {
  4913. position = ast_stream_topology_append_stream(media_state->topology, stream);
  4914. }
  4915. session_media = ao2_alloc_options(sizeof(*session_media), session_media_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK);
  4916. if (!session_media) {
  4917. return NULL;
  4918. }
  4919. session_media->keepalive_sched_id = -1;
  4920. session_media->timeout_sched_id = -1;
  4921. session_media->type = type;
  4922. session_media->stream_num = position;
  4923. session_media->bundle_group = -1;
  4924. strcpy(session_media->label, name);
  4925. if (AST_VECTOR_REPLACE(&media_state->sessions, position, session_media)) {
  4926. ao2_ref(session_media, -1);
  4927. return NULL;
  4928. }
  4929. /* If this stream will be active in some way and it is the first of this type then consider this the default media session to match */
  4930. if (!media_state->default_session[type] && ast_stream_get_state(ast_stream_topology_get_stream(media_state->topology, position)) != AST_STREAM_STATE_REMOVED) {
  4931. media_state->default_session[type] = session_media;
  4932. }
  4933. return session_media;
  4934. }
  4935. static int test_is_media_session_equal(struct ast_sip_session_media *left, struct ast_sip_session_media *right)
  4936. {
  4937. if (left == right) {
  4938. return 1;
  4939. }
  4940. if (!left) {
  4941. return 1;
  4942. }
  4943. if (!right) {
  4944. return 0;
  4945. }
  4946. return memcmp(left, right, sizeof(*left)) == 0;
  4947. }
  4948. static int test_is_media_state_equal(struct ast_sip_session_media_state *left, struct ast_sip_session_media_state *right,
  4949. int assert_on_failure)
  4950. {
  4951. int i;
  4952. SCOPE_ENTER(2);
  4953. if (left == right) {
  4954. SCOPE_EXIT_RTN_VALUE(1, "equal\n");
  4955. }
  4956. if (!(left && right)) {
  4957. ast_assert(!assert_on_failure);
  4958. SCOPE_EXIT_RTN_VALUE(0, "one is null: left: %p right: %p\n", left, right);
  4959. }
  4960. if (!ast_stream_topology_equal(left->topology, right->topology)) {
  4961. ast_assert(!assert_on_failure);
  4962. SCOPE_EXIT_RTN_VALUE(0, "topologies differ\n");
  4963. }
  4964. if (AST_VECTOR_SIZE(&left->sessions) != AST_VECTOR_SIZE(&right->sessions)) {
  4965. ast_assert(!assert_on_failure);
  4966. SCOPE_EXIT_RTN_VALUE(0, "session vector sizes different: left %zu != right %zu\n",
  4967. AST_VECTOR_SIZE(&left->sessions),
  4968. AST_VECTOR_SIZE(&right->sessions));
  4969. }
  4970. if (AST_VECTOR_SIZE(&left->read_callbacks) != AST_VECTOR_SIZE(&right->read_callbacks)) {
  4971. ast_assert(!assert_on_failure);
  4972. SCOPE_EXIT_RTN_VALUE(0, "read_callback vector sizes different: left %zu != right %zu\n",
  4973. AST_VECTOR_SIZE(&left->read_callbacks),
  4974. AST_VECTOR_SIZE(&right->read_callbacks));
  4975. }
  4976. for (i = 0; i < AST_VECTOR_SIZE(&left->sessions) ; i++) {
  4977. if (!test_is_media_session_equal(AST_VECTOR_GET(&left->sessions, i), AST_VECTOR_GET(&right->sessions, i))) {
  4978. ast_assert(!assert_on_failure);
  4979. SCOPE_EXIT_RTN_VALUE(0, "Media session %d different\n", i);
  4980. }
  4981. }
  4982. for (i = 0; i < AST_VECTOR_SIZE(&left->read_callbacks) ; i++) {
  4983. if (memcmp(AST_VECTOR_GET_ADDR(&left->read_callbacks, i),
  4984. AST_VECTOR_GET_ADDR(&right->read_callbacks, i),
  4985. sizeof(struct ast_sip_session_media_read_callback_state)) != 0) {
  4986. ast_assert(!assert_on_failure);
  4987. SCOPE_EXIT_RTN_VALUE(0, "read_callback %d different\n", i);
  4988. }
  4989. }
  4990. for (i = 0; i < AST_MEDIA_TYPE_END; i++) {
  4991. if (!(left->default_session[i] && right->default_session[i])) {
  4992. continue;
  4993. }
  4994. if (!left->default_session[i] || !right->default_session[i]
  4995. || left->default_session[i]->stream_num != right->default_session[i]->stream_num) {
  4996. ast_assert(!assert_on_failure);
  4997. SCOPE_EXIT_RTN_VALUE(0, "Default media session %d different. Left: %s Right: %s\n", i,
  4998. left->default_session[i] ? left->default_session[i]->label : "null",
  4999. right->default_session[i] ? right->default_session[i]->label : "null");
  5000. }
  5001. }
  5002. SCOPE_EXIT_RTN_VALUE(1, "equal\n");
  5003. }
  5004. AST_TEST_DEFINE(test_resolve_refresh_media_states)
  5005. {
  5006. #define FREE_STATE() \
  5007. ({ \
  5008. ast_sip_session_media_state_free(new_pending_state); \
  5009. new_pending_state = NULL; \
  5010. ast_sip_session_media_state_free(delayed_pending_state); \
  5011. delayed_pending_state = NULL; \
  5012. ast_sip_session_media_state_free(delayed_active_state); \
  5013. delayed_active_state = NULL; \
  5014. ast_sip_session_media_state_free(current_active_state); \
  5015. current_active_state = NULL; \
  5016. ast_sip_session_media_state_free(expected_pending_state); \
  5017. expected_pending_state = NULL; \
  5018. })
  5019. #define RESET_STATE(__num) \
  5020. ({ \
  5021. testnum=__num; \
  5022. ast_trace(-1, "Test %d\n", testnum); \
  5023. test_failed = 0; \
  5024. delayed_pending_state = ast_sip_session_media_state_alloc(); \
  5025. delayed_pending_state->topology = ast_stream_topology_alloc(); \
  5026. delayed_active_state = ast_sip_session_media_state_alloc(); \
  5027. delayed_active_state->topology = ast_stream_topology_alloc(); \
  5028. current_active_state = ast_sip_session_media_state_alloc(); \
  5029. current_active_state->topology = ast_stream_topology_alloc(); \
  5030. expected_pending_state = ast_sip_session_media_state_alloc(); \
  5031. expected_pending_state->topology = ast_stream_topology_alloc(); \
  5032. })
  5033. #define CHECKER() \
  5034. ({ \
  5035. new_pending_state = resolve_refresh_media_states("unittest", delayed_pending_state, delayed_active_state, current_active_state, 1); \
  5036. if (!test_is_media_state_equal(new_pending_state, expected_pending_state, 0)) { \
  5037. res = AST_TEST_FAIL; \
  5038. test_failed = 1; \
  5039. ast_test_status_update(test, "da: %s\n", ast_str_tmp(256, ast_stream_topology_to_str(delayed_active_state->topology, &STR_TMP))); \
  5040. ast_test_status_update(test, "dp: %s\n", ast_str_tmp(256, ast_stream_topology_to_str(delayed_pending_state->topology, &STR_TMP))); \
  5041. ast_test_status_update(test, "ca: %s\n", ast_str_tmp(256, ast_stream_topology_to_str(current_active_state->topology, &STR_TMP))); \
  5042. ast_test_status_update(test, "ep: %s\n", ast_str_tmp(256, ast_stream_topology_to_str(expected_pending_state->topology, &STR_TMP))); \
  5043. ast_test_status_update(test, "np: %s\n", ast_str_tmp(256, ast_stream_topology_to_str(new_pending_state->topology, &STR_TMP))); \
  5044. } \
  5045. ast_test_status_update(test, "Test %d %s\n", testnum, test_failed ? "FAILED" : "passed"); \
  5046. ast_trace(-1, "Test %d %s\n", testnum, test_failed ? "FAILED" : "passed"); \
  5047. test_failed = 0; \
  5048. FREE_STATE(); \
  5049. })
  5050. struct ast_sip_session_media_state * delayed_pending_state = NULL;
  5051. struct ast_sip_session_media_state * delayed_active_state = NULL;
  5052. struct ast_sip_session_media_state * current_active_state = NULL;
  5053. struct ast_sip_session_media_state * new_pending_state = NULL;
  5054. struct ast_sip_session_media_state * expected_pending_state = NULL;
  5055. enum ast_test_result_state res = AST_TEST_PASS;
  5056. int test_failed = 0;
  5057. int testnum = 0;
  5058. SCOPE_ENTER(1);
  5059. switch (cmd) {
  5060. case TEST_INIT:
  5061. info->name = "merge_refresh_topologies";
  5062. info->category = "/res/res_pjsip_session/";
  5063. info->summary = "Test merging of delayed request topologies";
  5064. info->description = "Test merging of delayed request topologies";
  5065. SCOPE_EXIT_RTN_VALUE(AST_TEST_NOT_RUN);
  5066. case TEST_EXECUTE:
  5067. break;
  5068. }
  5069. RESET_STATE(1);
  5070. test_media_add(delayed_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5071. test_media_add(delayed_active_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5072. test_media_add(delayed_active_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5073. test_media_add(delayed_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5074. test_media_add(delayed_pending_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5075. test_media_add(delayed_pending_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5076. test_media_add(delayed_pending_state, "myvideo3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5077. test_media_add(current_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5078. test_media_add(current_active_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5079. test_media_add(current_active_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5080. test_media_add(expected_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5081. test_media_add(expected_pending_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5082. test_media_add(expected_pending_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5083. test_media_add(expected_pending_state, "myvideo3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5084. CHECKER();
  5085. RESET_STATE(2);
  5086. test_media_add(delayed_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5087. test_media_add(delayed_active_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5088. test_media_add(delayed_active_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5089. test_media_add(delayed_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5090. test_media_add(delayed_pending_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5091. test_media_add(delayed_pending_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5092. test_media_add(delayed_pending_state, "myvideo3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5093. test_media_add(current_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5094. test_media_add(current_active_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5095. test_media_add(current_active_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5096. test_media_add(expected_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5097. test_media_add(expected_pending_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5098. test_media_add(expected_pending_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5099. test_media_add(expected_pending_state, "myvideo3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5100. CHECKER();
  5101. RESET_STATE(3);
  5102. test_media_add(delayed_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5103. test_media_add(delayed_active_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5104. test_media_add(delayed_active_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5105. test_media_add(delayed_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5106. test_media_add(delayed_pending_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5107. test_media_add(delayed_pending_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5108. test_media_add(delayed_pending_state, "myvideo3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5109. test_media_add(current_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5110. test_media_add(current_active_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5111. test_media_add(current_active_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5112. test_media_add(current_active_state, "myvideo4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5113. test_media_add(current_active_state, "myvideo5", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5114. test_media_add(expected_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5115. test_media_add(expected_pending_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5116. test_media_add(expected_pending_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5117. test_media_add(expected_pending_state, "myvideo4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5118. test_media_add(expected_pending_state, "myvideo5", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5119. test_media_add(expected_pending_state, "myvideo3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5120. CHECKER();
  5121. RESET_STATE(4);
  5122. test_media_add(delayed_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5123. test_media_add(delayed_active_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5124. test_media_add(delayed_active_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5125. test_media_add(delayed_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5126. test_media_add(delayed_pending_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5127. test_media_add(delayed_pending_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5128. test_media_add(delayed_pending_state, "myvideo3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5129. test_media_add(current_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5130. test_media_add(current_active_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5131. test_media_add(current_active_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_REMOVED, -1);
  5132. test_media_add(expected_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5133. test_media_add(expected_pending_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5134. test_media_add(expected_pending_state, "myvideo3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5135. CHECKER();
  5136. RESET_STATE(5);
  5137. test_media_add(delayed_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5138. test_media_add(delayed_active_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5139. test_media_add(delayed_active_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5140. test_media_add(delayed_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5141. test_media_add(delayed_pending_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5142. test_media_add(delayed_pending_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_REMOVED, -1);
  5143. test_media_add(current_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5144. test_media_add(current_active_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5145. test_media_add(current_active_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_REMOVED, -1);
  5146. test_media_add(expected_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5147. test_media_add(expected_pending_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5148. test_media_add(expected_pending_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_REMOVED, -1);
  5149. CHECKER();
  5150. RESET_STATE(6);
  5151. test_media_add(delayed_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5152. test_media_add(delayed_active_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5153. test_media_add(delayed_active_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5154. test_media_add(delayed_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5155. test_media_add(delayed_pending_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5156. test_media_add(delayed_pending_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5157. test_media_add(delayed_pending_state, "myvideo3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5158. test_media_add(current_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5159. test_media_add(current_active_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5160. test_media_add(current_active_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_REMOVED, -1);
  5161. test_media_add(current_active_state, "myvideo4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5162. test_media_add(expected_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5163. test_media_add(expected_pending_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5164. test_media_add(expected_pending_state, "myvideo3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5165. test_media_add(expected_pending_state, "myvideo4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5166. CHECKER();
  5167. RESET_STATE(7);
  5168. test_media_add(delayed_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5169. test_media_add(delayed_active_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5170. test_media_add(delayed_active_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5171. test_media_add(delayed_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5172. test_media_add(delayed_pending_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5173. test_media_add(delayed_pending_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5174. test_media_add(delayed_pending_state, "myvideo3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5175. test_media_add(delayed_pending_state, "myvideo4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5176. test_media_add(current_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5177. test_media_add(current_active_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5178. test_media_add(current_active_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5179. test_media_add(current_active_state, "myvideo5", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5180. test_media_add(current_active_state, "myvideo6", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5181. test_media_add(expected_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5182. test_media_add(expected_pending_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5183. test_media_add(expected_pending_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5184. test_media_add(expected_pending_state, "myvideo5", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5185. test_media_add(expected_pending_state, "myvideo6", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5186. test_media_add(expected_pending_state, "myvideo3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5187. test_media_add(expected_pending_state, "myvideo4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5188. CHECKER();
  5189. RESET_STATE(8);
  5190. test_media_add(delayed_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5191. test_media_add(delayed_active_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5192. test_media_add(delayed_active_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5193. test_media_add(delayed_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5194. test_media_add(delayed_pending_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5195. test_media_add(delayed_pending_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5196. test_media_add(delayed_pending_state, "myvideo3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5197. test_media_add(delayed_pending_state, "myvideo4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5198. test_media_add(current_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5199. test_media_add(current_active_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5200. test_media_add(current_active_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_REMOVED, -1);
  5201. test_media_add(expected_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5202. test_media_add(expected_pending_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5203. test_media_add(expected_pending_state, "myvideo3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5204. test_media_add(expected_pending_state, "myvideo4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5205. CHECKER();
  5206. RESET_STATE(9);
  5207. test_media_add(delayed_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5208. test_media_add(delayed_active_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5209. test_media_add(delayed_active_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5210. test_media_add(delayed_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5211. test_media_add(delayed_pending_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5212. test_media_add(delayed_pending_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5213. test_media_add(delayed_pending_state, "myvideo3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5214. test_media_add(delayed_pending_state, "myvideo4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5215. test_media_add(current_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5216. test_media_add(current_active_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_REMOVED, -1);
  5217. test_media_add(current_active_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_REMOVED, -1);
  5218. test_media_add(expected_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5219. test_media_add(expected_pending_state, "myvideo3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5220. test_media_add(expected_pending_state, "myvideo4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5221. CHECKER();
  5222. RESET_STATE(10);
  5223. test_media_add(delayed_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5224. test_media_add(delayed_active_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5225. test_media_add(delayed_active_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5226. test_media_add(delayed_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5227. test_media_add(delayed_pending_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_REMOVED, -1);
  5228. test_media_add(delayed_pending_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_REMOVED, -1);
  5229. test_media_add(current_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5230. test_media_add(current_active_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5231. test_media_add(current_active_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5232. test_media_add(current_active_state, "myvideo3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5233. test_media_add(expected_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5234. test_media_add(expected_pending_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_REMOVED, -1);
  5235. test_media_add(expected_pending_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_REMOVED, -1);
  5236. test_media_add(expected_pending_state, "myvideo3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5237. CHECKER();
  5238. RESET_STATE(11);
  5239. test_media_add(delayed_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5240. test_media_add(delayed_active_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5241. test_media_add(delayed_active_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5242. test_media_add(delayed_active_state, "myvideo4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5243. test_media_add(delayed_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5244. test_media_add(delayed_pending_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5245. test_media_add(delayed_pending_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5246. test_media_add(delayed_pending_state, "myvideo3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5247. test_media_add(current_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5248. test_media_add(current_active_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5249. test_media_add(current_active_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5250. test_media_add(current_active_state, "myvideo4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5251. test_media_add(expected_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5252. test_media_add(expected_pending_state, "myvideo1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5253. test_media_add(expected_pending_state, "myvideo2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5254. test_media_add(expected_pending_state, "myvideo4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5255. test_media_add(expected_pending_state, "myvideo3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5256. CHECKER();
  5257. RESET_STATE(12);
  5258. test_media_add(delayed_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5259. test_media_add(delayed_active_state, "292-1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5260. test_media_add(delayed_active_state, "296-2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5261. test_media_add(delayed_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5262. test_media_add(delayed_pending_state, "292-1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5263. test_media_add(delayed_pending_state, "296-2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5264. test_media_add(delayed_pending_state, "297-4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5265. test_media_add(delayed_pending_state, "294-5", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5266. test_media_add(current_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5267. test_media_add(current_active_state, "292-1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5268. test_media_add(current_active_state, "296-2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5269. test_media_add(current_active_state, "290-3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5270. test_media_add(current_active_state, "297-4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5271. test_media_add(expected_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5272. test_media_add(expected_pending_state, "292-1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5273. test_media_add(expected_pending_state, "296-2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5274. test_media_add(expected_pending_state, "290-3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5275. test_media_add(expected_pending_state, "297-4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5276. test_media_add(expected_pending_state, "294-5", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5277. CHECKER();
  5278. RESET_STATE(13);
  5279. test_media_add(delayed_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5280. test_media_add(delayed_active_state, "293-1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5281. test_media_add(delayed_active_state, "292-2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5282. test_media_add(delayed_active_state, "294-3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5283. test_media_add(delayed_active_state, "295-4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5284. test_media_add(delayed_active_state, "296-5", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5285. test_media_add(delayed_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5286. test_media_add(delayed_pending_state, "293-1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5287. test_media_add(delayed_pending_state, "292-2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5288. test_media_add(delayed_pending_state, "294-3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5289. test_media_add(delayed_pending_state, "295-4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5290. test_media_add(delayed_pending_state, "296-5", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5291. test_media_add(delayed_pending_state, "298-7", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5292. test_media_add(current_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5293. test_media_add(current_active_state, "293-1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5294. test_media_add(current_active_state, "292-2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5295. test_media_add(current_active_state, "294-3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5296. test_media_add(current_active_state, "295-4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5297. test_media_add(current_active_state, "296-5", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5298. test_media_add(current_active_state, "290-6", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5299. test_media_add(expected_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5300. test_media_add(expected_pending_state, "293-1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5301. test_media_add(expected_pending_state, "292-2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5302. test_media_add(expected_pending_state, "294-3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5303. test_media_add(expected_pending_state, "295-4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5304. test_media_add(expected_pending_state, "296-5", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5305. test_media_add(expected_pending_state, "290-6", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5306. test_media_add(expected_pending_state, "298-7", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5307. CHECKER();
  5308. RESET_STATE(14);
  5309. test_media_add(delayed_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5310. test_media_add(delayed_active_state, "298-1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5311. test_media_add(delayed_active_state, "297-2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5312. test_media_add(delayed_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5313. test_media_add(delayed_pending_state, "298-1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5314. test_media_add(delayed_pending_state, "294-4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5315. test_media_add(delayed_pending_state, "295-5", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5316. test_media_add(current_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5317. test_media_add(current_active_state, "298-1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5318. test_media_add(current_active_state, "297-2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5319. test_media_add(current_active_state, "291-3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5320. test_media_add(current_active_state, "294-4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5321. test_media_add(expected_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5322. test_media_add(expected_pending_state, "298-1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5323. test_media_add(expected_pending_state, "297-2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5324. test_media_add(expected_pending_state, "291-3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5325. test_media_add(expected_pending_state, "294-4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5326. test_media_add(expected_pending_state, "295-5", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5327. CHECKER();
  5328. RESET_STATE(15);
  5329. test_media_add(delayed_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5330. test_media_add(delayed_active_state, "298-1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5331. test_media_add(delayed_active_state, "297-2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5332. test_media_add(delayed_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5333. test_media_add(delayed_pending_state, "298-1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDONLY, -1);
  5334. test_media_add(delayed_pending_state, "294-4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5335. test_media_add(delayed_pending_state, "295-5", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5336. test_media_add(current_active_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5337. test_media_add(current_active_state, "297-2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5338. test_media_add(current_active_state, "291-3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5339. test_media_add(current_active_state, "294-4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5340. test_media_add(current_active_state, "298-1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5341. test_media_add(expected_pending_state, "audio", AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, -1);
  5342. test_media_add(expected_pending_state, "297-2", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5343. test_media_add(expected_pending_state, "291-3", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5344. test_media_add(expected_pending_state, "294-4", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5345. test_media_add(expected_pending_state, "298-1", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDONLY, -1);
  5346. test_media_add(expected_pending_state, "295-5", AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, -1);
  5347. CHECKER();
  5348. SCOPE_EXIT_RTN_VALUE(res);
  5349. }
  5350. #endif /* TEST_FRAMEWORK */
  5351. static int load_module(void)
  5352. {
  5353. pjsip_endpoint *endpt;
  5354. if (!ast_sip_get_sorcery() || !ast_sip_get_pjsip_endpoint()) {
  5355. return AST_MODULE_LOAD_DECLINE;
  5356. }
  5357. if (!(nat_hook = ast_sorcery_alloc(ast_sip_get_sorcery(), "nat_hook", NULL))) {
  5358. return AST_MODULE_LOAD_DECLINE;
  5359. }
  5360. nat_hook->outgoing_external_message = session_outgoing_nat_hook;
  5361. ast_sorcery_create(ast_sip_get_sorcery(), nat_hook);
  5362. sdp_handlers = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  5363. SDP_HANDLER_BUCKETS, sdp_handler_list_hash, NULL, sdp_handler_list_cmp);
  5364. if (!sdp_handlers) {
  5365. return AST_MODULE_LOAD_DECLINE;
  5366. }
  5367. endpt = ast_sip_get_pjsip_endpoint();
  5368. pjsip_inv_usage_init(endpt, &inv_callback);
  5369. pjsip_100rel_init_module(endpt);
  5370. pjsip_timer_init_module(endpt);
  5371. if (ast_sip_register_service(&session_module)) {
  5372. return AST_MODULE_LOAD_DECLINE;
  5373. }
  5374. ast_sip_register_service(&session_reinvite_module);
  5375. ast_sip_register_service(&outbound_invite_auth_module);
  5376. pjsip_reason_header_load();
  5377. ast_module_shutdown_ref(ast_module_info->self);
  5378. #ifdef TEST_FRAMEWORK
  5379. AST_TEST_REGISTER(test_resolve_refresh_media_states);
  5380. #endif
  5381. return AST_MODULE_LOAD_SUCCESS;
  5382. }
  5383. static int unload_module(void)
  5384. {
  5385. pjsip_reason_header_unload();
  5386. #ifdef TEST_FRAMEWORK
  5387. AST_TEST_UNREGISTER(test_resolve_refresh_media_states);
  5388. #endif
  5389. ast_sip_unregister_service(&outbound_invite_auth_module);
  5390. ast_sip_unregister_service(&session_reinvite_module);
  5391. ast_sip_unregister_service(&session_module);
  5392. ast_sorcery_delete(ast_sip_get_sorcery(), nat_hook);
  5393. ao2_cleanup(nat_hook);
  5394. ao2_cleanup(sdp_handlers);
  5395. return 0;
  5396. }
  5397. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "PJSIP Session resource",
  5398. .support_level = AST_MODULE_SUPPORT_CORE,
  5399. .load = load_module,
  5400. .unload = unload_module,
  5401. .load_pri = AST_MODPRI_APP_DEPEND,
  5402. .requires = "res_pjsip",
  5403. );