chan_iax.c 168 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Implementation of Inter-Asterisk eXchange
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <asterisk/lock.h>
  14. #include <asterisk/frame.h>
  15. #include <asterisk/channel.h>
  16. #include <asterisk/channel_pvt.h>
  17. #include <asterisk/logger.h>
  18. #include <asterisk/module.h>
  19. #include <asterisk/pbx.h>
  20. #include <asterisk/sched.h>
  21. #include <asterisk/io.h>
  22. #include <asterisk/config.h>
  23. #include <asterisk/options.h>
  24. #include <asterisk/cli.h>
  25. #include <asterisk/translate.h>
  26. #include <asterisk/md5.h>
  27. #include <asterisk/cdr.h>
  28. #include <asterisk/crypto.h>
  29. #include <asterisk/acl.h>
  30. #include <asterisk/manager.h>
  31. #include <asterisk/utils.h>
  32. #include <arpa/inet.h>
  33. #include <sys/socket.h>
  34. #include <netinet/in.h>
  35. #include <netinet/in_systm.h>
  36. #include <netinet/ip.h>
  37. #include <sys/time.h>
  38. #include <stdlib.h>
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <errno.h>
  42. #include <unistd.h>
  43. #include <netdb.h>
  44. #include <fcntl.h>
  45. #include <signal.h>
  46. #include <sys/signal.h>
  47. #ifdef MYSQL_FRIENDS
  48. #include <mysql/mysql.h>
  49. #endif
  50. #include "iax.h"
  51. #ifndef IPTOS_MINCOST
  52. #define IPTOS_MINCOST 0x02
  53. #endif
  54. #define IAX_CONF_FILE "iax1.conf"
  55. /* Deprecated old configuration file */
  56. #define IAX_OLD_CONF_FILE "iax.conf"
  57. /*
  58. * Uncomment to try experimental IAX bridge optimization,
  59. * designed to reduce latency when IAX calls cannot
  60. * be trasnferred
  61. */
  62. #define BRIDGE_OPTIMIZATION
  63. #define DEFAULT_RETRY_TIME 1000
  64. #define MEMORY_SIZE 100
  65. #define DEFAULT_DROP 3
  66. #define DEBUG_SUPPORT
  67. /* Sample over last 100 units to determine historic jitter */
  68. #define GAMMA (0.01)
  69. #ifdef MYSQL_FRIENDS
  70. AST_MUTEX_DEFINE_STATIC(mysqllock);
  71. static MYSQL *mysql;
  72. static char mydbuser[80];
  73. static char mydbpass[80];
  74. static char mydbhost[80];
  75. static char mydbname[80];
  76. #endif
  77. static char *desc = "Inter Asterisk eXchange";
  78. static char *tdesc = "Inter Asterisk eXchange Drver";
  79. static char *ctype = "IAX";
  80. static char *type = "IAX1";
  81. static char context[80] = "default";
  82. static int max_retries = 4;
  83. static int ping_time = 20;
  84. static int lagrq_time = 10;
  85. static int nextcallno = 0;
  86. static int maxjitterbuffer=3000;
  87. static int iaxdefaultdpcache=10 * 60; /* Cache dialplan entries for 10 minutes by default */
  88. static int iaxdefaulttimeout = 5; /* Default to wait no more than 5 seconds for a reply to come back */
  89. static int netsocket = -1;
  90. static int tos = 0;
  91. static int expirey = AST_DEFAULT_REG_EXPIRE;
  92. static int usecnt;
  93. AST_MUTEX_DEFINE_STATIC(usecnt_lock);
  94. int (*iax_regfunk)(char *username, int onoff) = NULL;
  95. /* Ethernet, etc */
  96. #define IAX_CAPABILITY_FULLBANDWIDTH 0xFFFF
  97. /* T1, maybe ISDN */
  98. #define IAX_CAPABILITY_MEDBANDWIDTH (IAX_CAPABILITY_FULLBANDWIDTH & \
  99. ~AST_FORMAT_SLINEAR & \
  100. ~AST_FORMAT_ULAW & \
  101. ~AST_FORMAT_ALAW)
  102. /* A modem */
  103. #define IAX_CAPABILITY_LOWBANDWIDTH (IAX_CAPABILITY_MEDBANDWIDTH & \
  104. ~AST_FORMAT_ADPCM & \
  105. ~AST_FORMAT_G726)
  106. #define IAX_CAPABILITY_LOWFREE (IAX_CAPABILITY_LOWBANDWIDTH & \
  107. ~AST_FORMAT_G723_1)
  108. #define DEFAULT_MAXMS 2000 /* Must be faster than 2 seconds by default */
  109. #define DEFAULT_FREQ_OK 60 * 1000 /* How often to check for the host to be up */
  110. #define DEFAULT_FREQ_NOTOK 10 * 1000 /* How often to check, if the host is down... */
  111. static struct io_context *io;
  112. static struct sched_context *sched;
  113. static int iax_capability = IAX_CAPABILITY_FULLBANDWIDTH;
  114. static int iax_dropcount = DEFAULT_DROP;
  115. static int use_jitterbuffer = 1;
  116. static int iaxdebug = 0;
  117. static char accountcode[20];
  118. static int amaflags = 0;
  119. static pthread_t netthreadid = AST_PTHREADT_NULL;
  120. #define IAX_STATE_STARTED (1 << 0)
  121. #define IAX_STATE_AUTHENTICATED (1 << 1)
  122. #define IAX_STATE_TBD (1 << 2)
  123. struct iax_context {
  124. char context[AST_MAX_EXTENSION];
  125. struct iax_context *next;
  126. };
  127. struct iax_user {
  128. char name[80];
  129. char secret[80];
  130. char methods[80];
  131. char accountcode[20];
  132. char inkeys[80]; /* Key(s) this user can use to authenticate to us */
  133. int amaflags;
  134. int hascallerid;
  135. int delme;
  136. char callerid[AST_MAX_EXTENSION];
  137. struct ast_ha *ha;
  138. struct iax_context *contexts;
  139. struct iax_user *next;
  140. };
  141. struct iax_peer {
  142. char name[80];
  143. char username[80];
  144. char secret[80];
  145. char outkey[80]; /* What key we use to talk to this peer */
  146. char context[AST_MAX_EXTENSION]; /* Default context (for transfer really) */
  147. struct sockaddr_in addr;
  148. int formats;
  149. struct in_addr mask;
  150. /* Dynamic Registration fields */
  151. int dynamic; /* If this is a dynamic peer */
  152. struct sockaddr_in defaddr; /* Default address if there is one */
  153. char methods[80];
  154. char inkeys[80]; /* Key(s) this peer can use to authenticate to us */
  155. int hascallerid;
  156. /* Suggested caller id if registering */
  157. char callerid[AST_MAX_EXTENSION];
  158. /* Whether or not to send ANI */
  159. int sendani;
  160. int expire; /* Schedule entry for expirey */
  161. int expirey; /* How soon to expire */
  162. int capability; /* Capability */
  163. int delme; /* I need to be deleted */
  164. /* Qualification */
  165. int callno; /* Call number of POKE request */
  166. int pokeexpire; /* When to expire poke */
  167. int lastms; /* How long last response took (in ms), or -1 for no response */
  168. int maxms; /* Max ms we will accept for the host to be up, 0 to not monitor */
  169. struct ast_ha *ha;
  170. struct iax_peer *next;
  171. };
  172. #define REG_STATE_UNREGISTERED 0
  173. #define REG_STATE_REGSENT 1
  174. #define REG_STATE_AUTHSENT 2
  175. #define REG_STATE_REGISTERED 3
  176. #define REG_STATE_REJECTED 4
  177. #define REG_STATE_TIMEOUT 5
  178. #define REG_STATE_NOAUTH 6
  179. #define TRANSFER_NONE 0
  180. #define TRANSFER_BEGIN 1
  181. #define TRANSFER_READY 2
  182. #define TRANSFER_RELEASED 3
  183. #define TRANSFER_PASSTHROUGH 4
  184. struct iax_registry {
  185. struct sockaddr_in addr; /* Who we connect to for registration purposes */
  186. char username[80];
  187. char secret[80]; /* Password or key name in []'s */
  188. char random[80];
  189. int expire; /* Sched ID of expiration */
  190. int refresh; /* How often to refresh */
  191. int regstate;
  192. int callno; /* Associated call number if applicable */
  193. struct sockaddr_in us; /* Who the server thinks we are */
  194. struct iax_registry *next;
  195. };
  196. static struct iax_registry *registrations;
  197. /* Don't retry more frequently than every 10 ms, or less frequently than every 5 seconds */
  198. #define MIN_RETRY_TIME 10
  199. #define MAX_RETRY_TIME 10000
  200. #define MAX_JITTER_BUFFER 50
  201. /* If we have more than this much excess real jitter buffer, srhink it. */
  202. static int max_jitter_buffer = MAX_JITTER_BUFFER;
  203. struct chan_iax_pvt {
  204. /* Pipes for communication. pipe[1] belongs to the
  205. network thread (write), and pipe[0] belongs to the individual
  206. channel (read) */
  207. /* Whether or not we Quelch audio */
  208. int quelch;
  209. /* Last received voice format */
  210. int voiceformat;
  211. /* Last sent voice format */
  212. int svoiceformat;
  213. /* What we are capable of sending */
  214. int capability;
  215. /* Last received timestamp */
  216. unsigned int last;
  217. /* Last sent timestamp - never send the same timestamp twice in a single call */
  218. unsigned int lastsent;
  219. /* Ping time */
  220. unsigned int pingtime;
  221. /* Max time for initial response */
  222. int maxtime;
  223. /* Peer Address */
  224. struct sockaddr_in addr;
  225. /* Our call number */
  226. int callno;
  227. /* Peer callno */
  228. int peercallno;
  229. /* Peer selected format */
  230. int peerformat;
  231. /* Peer capability */
  232. int peercapability;
  233. /* timeval that we base our transmission on */
  234. struct timeval offset;
  235. /* timeval that we base our delivery on */
  236. struct timeval rxcore;
  237. /* Historical delivery time */
  238. int history[MEMORY_SIZE];
  239. /* Current base jitterbuffer */
  240. int jitterbuffer;
  241. /* Current jitter measure */
  242. int jitter;
  243. /* Historic jitter value */
  244. int historicjitter;
  245. /* LAG */
  246. int lag;
  247. /* Error, as discovered by the manager */
  248. int error;
  249. /* Owner if we have one */
  250. struct ast_channel *owner;
  251. /* What's our state? */
  252. int state;
  253. /* Expirey (optional) */
  254. int expirey;
  255. /* Next outgoing sequence number */
  256. unsigned short oseqno;
  257. /* Next incoming sequence number */
  258. unsigned short iseqno;
  259. /* Peer name */
  260. char peer[80];
  261. /* Default Context */
  262. char context[80];
  263. /* Caller ID if available */
  264. char callerid[80];
  265. /* Hidden Caller ID (i.e. ANI) if appropriate */
  266. char ani[80];
  267. /* Whether or not ani should be transmitted in addition to Caller*ID */
  268. int sendani;
  269. /* DNID */
  270. char dnid[80];
  271. /* Requested Extension */
  272. char exten[AST_MAX_EXTENSION];
  273. /* Expected Username */
  274. char username[80];
  275. /* Expected Secret */
  276. char secret[80];
  277. /* permitted authentication methods */
  278. char methods[80];
  279. /* MD5 challenge */
  280. char challenge[10];
  281. /* Public keys permitted keys for incoming authentication */
  282. char inkeys[80];
  283. /* Private key for outgoing authentication */
  284. char outkey[80];
  285. /* Preferred language */
  286. char language[80];
  287. /* Associated registry */
  288. struct iax_registry *reg;
  289. /* Associated peer for poking */
  290. struct iax_peer *peerpoke;
  291. /* Transferring status */
  292. int transferring;
  293. /* Already disconnected */
  294. int alreadygone;
  295. /* Who we are IAX transfering to */
  296. struct sockaddr_in transfer;
  297. /* What's the new call number for the transfer */
  298. int transfercallno;
  299. /* Status of knowledge of peer ADSI capability */
  300. int peeradsicpe;
  301. /* Who we are bridged to */
  302. int bridgecallno;
  303. int pingid; /* Transmit PING request */
  304. int lagid; /* Retransmit lag request */
  305. int autoid; /* Auto hangup for Dialplan requestor */
  306. int initid; /* Initial peer auto-congest ID (based on qualified peers) */
  307. char dproot[AST_MAX_EXTENSION];
  308. char accountcode[20];
  309. int amaflags;
  310. struct iax_dpcache *dpentries;
  311. };
  312. #define DIRECTION_INGRESS 1
  313. #define DIRECTION_OUTGRESS 2
  314. struct ast_iax_frame {
  315. /* Actual, isolated frame */
  316. struct ast_frame *f;
  317. /* /Our/ call number */
  318. short callno;
  319. /* Start of raw frame (outgoing only) */
  320. void *data;
  321. /* Length of frame (outgoing only) */
  322. int datalen;
  323. /* How many retries so far? */
  324. int retries;
  325. /* Outgoing relative timestamp (ms) */
  326. unsigned int ts;
  327. /* How long to wait before retrying */
  328. int retrytime;
  329. /* Are we received out of order? */
  330. int outoforder;
  331. /* Have we been sent at all yet? */
  332. int sentyet;
  333. /* Packet sequence number */
  334. int seqno;
  335. /* Non-zero if should be sent to transfer peer */
  336. int transfer;
  337. /* Non-zero if this is the final message */
  338. int final;
  339. /* Ingress or outgres */
  340. int direction;
  341. /* Retransmission ID */
  342. int retrans;
  343. /* Easy linking */
  344. struct ast_iax_frame *next;
  345. struct ast_iax_frame *prev;
  346. };
  347. static struct ast_iax_queue {
  348. struct ast_iax_frame *head;
  349. struct ast_iax_frame *tail;
  350. int count;
  351. ast_mutex_t lock;
  352. } iaxq;
  353. static struct ast_user_list {
  354. struct iax_user *users;
  355. ast_mutex_t lock;
  356. } userl;
  357. static struct ast_peer_list {
  358. struct iax_peer *peers;
  359. ast_mutex_t lock;
  360. } peerl;
  361. /* Extension exists */
  362. #define CACHE_FLAG_EXISTS (1 << 0)
  363. /* Extension is non-existant */
  364. #define CACHE_FLAG_NONEXISTANT (1 << 1)
  365. /* Extension can exist */
  366. #define CACHE_FLAG_CANEXIST (1 << 2)
  367. /* Waiting to hear back response */
  368. #define CACHE_FLAG_PENDING (1 << 3)
  369. /* Timed out */
  370. #define CACHE_FLAG_TIMEOUT (1 << 4)
  371. /* Request transmitted */
  372. #define CACHE_FLAG_TRANSMITTED (1 << 5)
  373. /* Timeout */
  374. #define CACHE_FLAG_UNKNOWN (1 << 6)
  375. /* Matchmore */
  376. #define CACHE_FLAG_MATCHMORE (1 << 7)
  377. static struct iax_dpcache {
  378. char peercontext[AST_MAX_EXTENSION];
  379. char exten[AST_MAX_EXTENSION];
  380. struct timeval orig;
  381. struct timeval expirey;
  382. int flags;
  383. int callno;
  384. int waiters[256];
  385. struct iax_dpcache *next;
  386. struct iax_dpcache *peer; /* For linking in peers */
  387. } *dpcache;
  388. AST_MUTEX_DEFINE_STATIC(dpcache_lock);
  389. #ifdef DEBUG_SUPPORT
  390. static void showframe(struct ast_iax_frame *f, struct ast_iax_full_hdr *fhi, int rx, struct sockaddr_in *sin)
  391. {
  392. char *frames[] = {
  393. "(0?)",
  394. "DTMF ",
  395. "VOICE ",
  396. "VIDEO ",
  397. "CONTROL",
  398. "NULL ",
  399. "IAX ",
  400. "TEXT ",
  401. "IMAGE " };
  402. char *iaxs[] = {
  403. "(0?)",
  404. "NEW ",
  405. "PING ",
  406. "PONG ",
  407. "ACK ",
  408. "HANGUP ",
  409. "REJECT ",
  410. "ACCEPT ",
  411. "AUTHREQ",
  412. "AUTHREP",
  413. "INVAL ",
  414. "LAGRQ ",
  415. "LAGRP ",
  416. "REGREQ ",
  417. "REGAUTH",
  418. "REGACK ",
  419. "REGREJ ",
  420. "REGREL ",
  421. "VNAK ",
  422. "DPREQ ",
  423. "DPREP ",
  424. "DIAL ",
  425. "TXREQ ",
  426. "TXCNT ",
  427. "TXACC ",
  428. "TXREADY",
  429. "TXREL ",
  430. "TXREJ ",
  431. "QUELCH ",
  432. "UNQULCH",
  433. "POKE",
  434. };
  435. char *cmds[] = {
  436. "(0?)",
  437. "HANGUP ",
  438. "RING ",
  439. "RINGING",
  440. "ANSWER ",
  441. "BUSY ",
  442. "TKOFFHK ",
  443. "OFFHOOK" };
  444. struct ast_iax_full_hdr *fh;
  445. char retries[20] = "";
  446. char class2[20];
  447. char subclass2[20];
  448. char *class;
  449. char *subclass;
  450. char iabuf[INET_ADDRSTRLEN];
  451. if (f) {
  452. fh = f->data;
  453. snprintf(retries, sizeof(retries), "%03d", f->retries);
  454. } else {
  455. strncpy(retries, "N/A", sizeof(retries) - 1);
  456. fh = fhi;
  457. }
  458. if (!(ntohs(fh->callno) & AST_FLAG_FULL)) {
  459. /* Don't mess with mini-frames */
  460. return;
  461. }
  462. if (fh->type > sizeof(frames)/sizeof(char *)) {
  463. snprintf(class2, sizeof(class2), "(%d?)", fh->type);
  464. class = class2;
  465. } else {
  466. class = frames[(int)fh->type];
  467. }
  468. if (fh->type == AST_FRAME_DTMF) {
  469. snprintf(subclass2, sizeof(subclass2), "%c", fh->csub);
  470. subclass = subclass2;
  471. } else if (fh->type == AST_FRAME_IAX) {
  472. if (fh->csub >= sizeof(iaxs)/sizeof(iaxs[0])) {
  473. snprintf(subclass2, sizeof(subclass2), "(%d?)", fh->csub);
  474. subclass = subclass2;
  475. } else {
  476. subclass = iaxs[(int)fh->csub];
  477. }
  478. } else if (fh->type == AST_FRAME_CONTROL) {
  479. if (fh->csub > sizeof(cmds)/sizeof(char *)) {
  480. snprintf(subclass2, sizeof(subclass2), "(%d?)", fh->csub);
  481. subclass = subclass2;
  482. } else {
  483. subclass = cmds[(int)fh->csub];
  484. }
  485. } else {
  486. snprintf(subclass2, sizeof(subclass2), "%d", fh->csub);
  487. subclass = subclass2;
  488. }
  489. ast_verbose(
  490. "%s-Frame Retry[%s] -- Seqno: %2.2d Type: %s Subclass: %s\n",
  491. (rx ? "Rx" : "Tx"),
  492. retries, ntohs(fh->seqno), class, subclass);
  493. fprintf(stderr,
  494. " Timestamp: %05ldms Callno: %5.5d DCall: %5.5d [%s:%d]\n",
  495. (long)ntohl(fh->ts),
  496. (short)(ntohs(fh->callno) & ~AST_FLAG_FULL), (short) ntohs(fh->dcallno),
  497. ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), ntohs(sin->sin_port));
  498. }
  499. #endif
  500. /* XXX We probably should use a mutex when working with this XXX */
  501. static struct chan_iax_pvt *iaxs[AST_IAX_MAX_CALLS];
  502. static ast_mutex_t iaxsl[AST_IAX_MAX_CALLS];
  503. static int send_command(struct chan_iax_pvt *, char, int, unsigned int, char *, int, int);
  504. static int send_command_immediate(struct chan_iax_pvt *, char, int, unsigned int, char *, int, int);
  505. static int send_command_final(struct chan_iax_pvt *, char, int, unsigned int, char *, int, int);
  506. static int send_command_transfer(struct chan_iax_pvt *, char, int, unsigned int, char *, int);
  507. static unsigned int calc_timestamp(struct chan_iax_pvt *p, unsigned int ts);
  508. static int send_ping(void *data)
  509. {
  510. int callno = (long)data;
  511. /* Ping only if it's real, not if it's bridged */
  512. if (iaxs[callno]) {
  513. #ifdef BRIDGE_OPTIMIZATION
  514. if (iaxs[callno]->bridgecallno < 0)
  515. #endif
  516. send_command(iaxs[callno], AST_FRAME_IAX, AST_IAX_COMMAND_PING, 0, NULL, 0, -1);
  517. return 1;
  518. } else
  519. return 0;
  520. }
  521. static int send_lagrq(void *data)
  522. {
  523. int callno = (long)data;
  524. /* Ping only if it's real not if it's bridged */
  525. if (iaxs[callno]) {
  526. #ifdef BRIDGE_OPTIMIZATION
  527. if (iaxs[callno]->bridgecallno < 0)
  528. #endif
  529. send_command(iaxs[callno], AST_FRAME_IAX, AST_IAX_COMMAND_LAGRQ, 0, NULL, 0, -1);
  530. return 1;
  531. } else
  532. return 0;
  533. }
  534. static unsigned char compress_subclass(int subclass)
  535. {
  536. int x;
  537. int power=-1;
  538. /* If it's 128 or smaller, just return it */
  539. if (subclass < AST_FLAG_SC_LOG)
  540. return subclass;
  541. /* Otherwise find its power */
  542. for (x = 0; x < AST_MAX_SHIFT; x++) {
  543. if (subclass & (1 << x)) {
  544. if (power > -1) {
  545. ast_log(LOG_WARNING, "Can't compress subclass %d\n", subclass);
  546. return 0;
  547. } else
  548. power = x;
  549. }
  550. }
  551. return power | AST_FLAG_SC_LOG;
  552. }
  553. static int uncompress_subclass(unsigned char csub)
  554. {
  555. /* If the SC_LOG flag is set, return 2^csub otherwise csub */
  556. if (csub & AST_FLAG_SC_LOG) {
  557. /* special case for 'compressed' -1 */
  558. if (csub == 0xff)
  559. return -1;
  560. else
  561. return 1 << (csub & ~AST_FLAG_SC_LOG & AST_MAX_SHIFT);
  562. }
  563. else
  564. return csub;
  565. }
  566. static struct chan_iax_pvt *new_iax(void)
  567. {
  568. struct chan_iax_pvt *tmp;
  569. tmp = malloc(sizeof(struct chan_iax_pvt));
  570. if (tmp) {
  571. memset(tmp, 0, sizeof(struct chan_iax_pvt));
  572. tmp->callno = -1;
  573. tmp->peercallno = -1;
  574. tmp->transfercallno = -1;
  575. tmp->bridgecallno = -1;
  576. tmp->pingid = -1;
  577. tmp->lagid = -1;
  578. tmp->autoid = -1;
  579. tmp->initid = -1;
  580. /* strncpy(tmp->context, context, sizeof(tmp->context)-1); */
  581. strncpy(tmp->exten, "s", sizeof(tmp->exten)-1);
  582. }
  583. return tmp;
  584. }
  585. static int get_samples(struct ast_frame *f)
  586. {
  587. int samples=0;
  588. switch(f->subclass) {
  589. case AST_FORMAT_G723_1:
  590. samples = 240 /* XXX Not necessarily true XXX */;
  591. break;
  592. case AST_FORMAT_GSM:
  593. samples = 160 * (f->datalen / 33);
  594. break;
  595. case AST_FORMAT_ILBC:
  596. samples = 240 * (f->datalen / 50);
  597. break;
  598. case AST_FORMAT_G729A:
  599. samples = 160 * (f->datalen / 20);
  600. break;
  601. case AST_FORMAT_SLINEAR:
  602. samples = f->datalen / 2;
  603. break;
  604. case AST_FORMAT_LPC10:
  605. samples = 22 * 8;
  606. samples += (((char *)(f->data))[7] & 0x1) * 8;
  607. break;
  608. case AST_FORMAT_ULAW:
  609. samples = f->datalen;
  610. break;
  611. case AST_FORMAT_ALAW:
  612. samples = f->datalen;
  613. break;
  614. case AST_FORMAT_ADPCM:
  615. case AST_FORMAT_G726:
  616. samples = f->datalen *2;
  617. break;
  618. case AST_FORMAT_SPEEX:
  619. samples = (f->datalen/39)*160;
  620. break;
  621. default:
  622. ast_log(LOG_WARNING, "Don't know how to calculate samples on %d packets\n", f->subclass);
  623. }
  624. return samples;
  625. }
  626. static int frames = 0;
  627. static int iframes = 0;
  628. static int oframes = 0;
  629. static struct ast_iax_frame *ast_iax_frame_new(int direction)
  630. {
  631. struct ast_iax_frame *fr;
  632. fr = malloc(sizeof(struct ast_iax_frame));
  633. if (fr) {
  634. fr->direction = direction;
  635. fr->retrans = -1;
  636. frames++;
  637. if (fr->direction == DIRECTION_INGRESS)
  638. iframes++;
  639. else
  640. oframes++;
  641. }
  642. return fr;
  643. }
  644. static void ast_iax_frame_free(struct ast_iax_frame *fr)
  645. {
  646. if (fr->retrans > -1)
  647. ast_sched_del(sched, fr->retrans);
  648. if (fr->direction == DIRECTION_INGRESS)
  649. iframes--;
  650. else if (fr->direction == DIRECTION_OUTGRESS)
  651. oframes--;
  652. else {
  653. ast_log(LOG_WARNING, "Attempt to double free frame detected\n");
  654. CRASH;
  655. return;
  656. }
  657. fr->direction = 0;
  658. free(fr);
  659. frames--;
  660. }
  661. static struct ast_iax_frame *iaxfrdup2(struct ast_iax_frame *fr, int ch)
  662. {
  663. /* Malloc() a copy of a frame */
  664. struct ast_iax_frame *new = ast_iax_frame_new(DIRECTION_INGRESS);
  665. if (new) {
  666. memcpy(new, fr, sizeof(struct ast_iax_frame));
  667. new->f = ast_frdup(fr->f);
  668. /* Copy full header */
  669. if (ch) {
  670. memcpy(new->f->data - sizeof(struct ast_iax_full_hdr),
  671. fr->f->data - sizeof(struct ast_iax_full_hdr),
  672. sizeof(struct ast_iax_full_hdr));
  673. /* Grab new data pointer */
  674. new->data = new->f->data - (fr->f->data - fr->data);
  675. } else {
  676. new->data = NULL;
  677. new->datalen = 0;
  678. }
  679. new->direction = DIRECTION_INGRESS;
  680. new->retrans = -1;
  681. }
  682. return new;
  683. }
  684. #define NEW_PREVENT 0
  685. #define NEW_ALLOW 1
  686. #define NEW_FORCE 2
  687. static int match(struct sockaddr_in *sin, short callno, short dcallno, struct chan_iax_pvt *cur)
  688. {
  689. if ((cur->addr.sin_addr.s_addr == sin->sin_addr.s_addr) &&
  690. (cur->addr.sin_port == sin->sin_port)) {
  691. /* This is the main host */
  692. if ((cur->peercallno == callno) ||
  693. ((dcallno == cur->callno) && (cur->peercallno) == -1)) {
  694. /* That's us. Be sure we keep track of the peer call number */
  695. return 1;
  696. }
  697. }
  698. if ((cur->transfer.sin_addr.s_addr == sin->sin_addr.s_addr) &&
  699. (cur->transfer.sin_port == sin->sin_port) && (cur->transferring)) {
  700. /* We're transferring */
  701. if (dcallno == cur->callno)
  702. return 1;
  703. }
  704. return 0;
  705. }
  706. static int find_callno(short callno, short dcallno ,struct sockaddr_in *sin, int new)
  707. {
  708. int res = -1;
  709. int x;
  710. int start;
  711. if (new <= NEW_ALLOW) {
  712. /* Look for an existing connection first */
  713. for (x=0;(res < 0) && (x<AST_IAX_MAX_CALLS);x++) {
  714. ast_mutex_lock(&iaxsl[x]);
  715. if (iaxs[x]) {
  716. /* Look for an exact match */
  717. if (match(sin, callno, dcallno, iaxs[x])) {
  718. res = x;
  719. }
  720. }
  721. ast_mutex_unlock(&iaxsl[x]);
  722. }
  723. }
  724. if ((res < 0) && (new >= NEW_ALLOW)) {
  725. /* Create a new one */
  726. start = nextcallno;
  727. for (x = (nextcallno + 1) % AST_IAX_MAX_CALLS; iaxs[x] && (x != start); x = (x + 1) % AST_IAX_MAX_CALLS)
  728. if (x == start) {
  729. ast_log(LOG_WARNING, "Unable to accept more calls\n");
  730. return -1;
  731. }
  732. ast_mutex_lock(&iaxsl[x]);
  733. iaxs[x] = new_iax();
  734. ast_mutex_unlock(&iaxsl[x]);
  735. if (iaxs[x]) {
  736. if (option_debug)
  737. ast_log(LOG_DEBUG, "Creating new call structure %d\n", x);
  738. iaxs[x]->addr.sin_port = sin->sin_port;
  739. iaxs[x]->addr.sin_family = sin->sin_family;
  740. iaxs[x]->addr.sin_addr.s_addr = sin->sin_addr.s_addr;
  741. iaxs[x]->peercallno = callno;
  742. iaxs[x]->callno = x;
  743. iaxs[x]->pingtime = DEFAULT_RETRY_TIME;
  744. iaxs[x]->expirey = expirey;
  745. iaxs[x]->pingid = ast_sched_add(sched, ping_time * 1000, send_ping, (void *)(long)x);
  746. iaxs[x]->lagid = ast_sched_add(sched, lagrq_time * 1000, send_lagrq, (void *)(long)x);
  747. iaxs[x]->amaflags = amaflags;
  748. strncpy(iaxs[x]->accountcode, accountcode, sizeof(iaxs[x]->accountcode)-1);
  749. } else {
  750. ast_log(LOG_WARNING, "Out of resources\n");
  751. return -1;
  752. }
  753. res = x;
  754. nextcallno = x;
  755. }
  756. return res;
  757. }
  758. static int iax_queue_frame(int callno, struct ast_frame *f)
  759. {
  760. int pass =0;
  761. /* Assumes lock for callno is already held... */
  762. for (;;) {
  763. pass++;
  764. if (iaxs[callno] && iaxs[callno]->owner) {
  765. if (ast_mutex_trylock(&iaxs[callno]->owner->lock)) {
  766. /* Avoid deadlock by pausing and trying again */
  767. ast_mutex_unlock(&iaxsl[callno]);
  768. usleep(1);
  769. ast_mutex_lock(&iaxsl[callno]);
  770. } else {
  771. ast_queue_frame(iaxs[callno]->owner, f);
  772. ast_mutex_unlock(&iaxs[callno]->owner->lock);
  773. break;
  774. }
  775. } else
  776. break;
  777. }
  778. return 0;
  779. }
  780. static int iax_send(struct chan_iax_pvt *pvt, struct ast_frame *f, unsigned int ts, int seqno, int now, int transfer, int final);
  781. static int __do_deliver(void *data)
  782. {
  783. /* Just deliver the packet by using queueing. This is called by
  784. the IAX thread with the iaxsl lock held. */
  785. struct ast_iax_frame *fr = data;
  786. unsigned int ts;
  787. fr->retrans = -1;
  788. if (iaxs[fr->callno] && !iaxs[fr->callno]->alreadygone) {
  789. if (fr->f->frametype == AST_FRAME_IAX) {
  790. /* We have to treat some of these packets specially because
  791. they're LAG measurement packets */
  792. if (fr->f->subclass == AST_IAX_COMMAND_LAGRQ) {
  793. /* If we got a queued request, build a reply and send it */
  794. fr->f->subclass = AST_IAX_COMMAND_LAGRP;
  795. iax_send(iaxs[fr->callno], fr->f, fr->ts, -1, 0, 0, 0);
  796. } else if (fr->f->subclass == AST_IAX_COMMAND_LAGRP) {
  797. /* This is a reply we've been given, actually measure the difference */
  798. ts = calc_timestamp(iaxs[fr->callno], 0);
  799. iaxs[fr->callno]->lag = ts - fr->ts;
  800. }
  801. } else {
  802. iax_queue_frame(fr->callno, fr->f);
  803. }
  804. }
  805. /* Free the packet */
  806. ast_frfree(fr->f);
  807. /* And our iax frame */
  808. ast_iax_frame_free(fr);
  809. /* And don't run again */
  810. return 0;
  811. }
  812. static int do_deliver(void *data)
  813. {
  814. /* Locking version of __do_deliver */
  815. struct ast_iax_frame *fr = data;
  816. int callno = fr->callno;
  817. int res;
  818. ast_mutex_lock(&iaxsl[callno]);
  819. res = __do_deliver(data);
  820. ast_mutex_unlock(&iaxsl[callno]);
  821. return res;
  822. }
  823. static int handle_error(void)
  824. {
  825. /* XXX Ideally we should figure out why an error occured and then abort those
  826. rather than continuing to try. Unfortunately, the published interface does
  827. not seem to work XXX */
  828. #if 0
  829. struct sockaddr_in *sin;
  830. int res;
  831. struct msghdr m;
  832. struct sock_extended_err e;
  833. m.msg_name = NULL;
  834. m.msg_namelen = 0;
  835. m.msg_iov = NULL;
  836. m.msg_control = &e;
  837. m.msg_controllen = sizeof(e);
  838. m.msg_flags = 0;
  839. res = recvmsg(netsocket, &m, MSG_ERRQUEUE);
  840. if (res < 0)
  841. ast_log(LOG_WARNING, "Error detected, but unable to read error: %s\n", strerror(errno));
  842. else {
  843. if (m.msg_controllen) {
  844. sin = (struct sockaddr_in *)SO_EE_OFFENDER(&e);
  845. if (sin)
  846. ast_log(LOG_WARNING, "Receive error from %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr));
  847. else
  848. ast_log(LOG_WARNING, "No address detected??\n");
  849. } else {
  850. ast_log(LOG_WARNING, "Local error: %s\n", strerror(e.ee_errno));
  851. }
  852. }
  853. #endif
  854. return 0;
  855. }
  856. static int send_packet(struct ast_iax_frame *f)
  857. {
  858. int res;
  859. char iabuf[INET_ADDRSTRLEN];
  860. /* Called with iaxsl held */
  861. if (option_debug)
  862. ast_log(LOG_DEBUG, "Sending %d on %d/%d to %s:%d\n", f->ts, f->callno, iaxs[f->callno]->peercallno, ast_inet_ntoa(iabuf, sizeof(iabuf), iaxs[f->callno]->addr.sin_addr), ntohs(iaxs[f->callno]->addr.sin_port));
  863. /* Don't send if there was an error, but return error instead */
  864. if (f->callno < 0) {
  865. ast_log(LOG_WARNING, "Call number = %d\n", f->callno);
  866. return -1;
  867. }
  868. if (!iaxs[f->callno])
  869. return -1;
  870. if (iaxs[f->callno]->error)
  871. return -1;
  872. if (f->transfer) {
  873. #ifdef DEBUG_SUPPORT
  874. if (iaxdebug)
  875. showframe(f, NULL, 0, &iaxs[f->callno]->transfer);
  876. #endif
  877. res = sendto(netsocket, f->data, f->datalen, 0,(struct sockaddr *)&iaxs[f->callno]->transfer,
  878. sizeof(iaxs[f->callno]->transfer));
  879. } else {
  880. #ifdef DEBUG_SUPPORT
  881. if (iaxdebug)
  882. showframe(f, NULL, 0, &iaxs[f->callno]->addr);
  883. #endif
  884. res = sendto(netsocket, f->data, f->datalen, 0,(struct sockaddr *)&iaxs[f->callno]->addr,
  885. sizeof(iaxs[f->callno]->addr));
  886. }
  887. if (res < 0) {
  888. if (option_debug)
  889. ast_log(LOG_DEBUG, "Received error: %s\n", strerror(errno));
  890. handle_error();
  891. } else
  892. res = 0;
  893. return res;
  894. }
  895. static int iax_predestroy(int callno)
  896. {
  897. struct ast_channel *c;
  898. struct chan_iax_pvt *pvt;
  899. ast_mutex_lock(&iaxsl[callno]);
  900. pvt = iaxs[callno];
  901. if (!pvt) {
  902. ast_mutex_unlock(&iaxsl[callno]);
  903. return -1;
  904. }
  905. if (!pvt->alreadygone) {
  906. /* No more pings or lagrq's */
  907. if (pvt->pingid > -1)
  908. ast_sched_del(sched, pvt->pingid);
  909. if (pvt->lagid > -1)
  910. ast_sched_del(sched, pvt->lagid);
  911. if (pvt->autoid > -1)
  912. ast_sched_del(sched, pvt->autoid);
  913. if (pvt->initid > -1)
  914. ast_sched_del(sched, pvt->initid);
  915. pvt->pingid = -1;
  916. pvt->lagid = -1;
  917. pvt->autoid = -1;
  918. pvt->initid = -1;
  919. pvt->alreadygone = 1;
  920. }
  921. c = pvt->owner;
  922. if (c) {
  923. c->_softhangup |= AST_SOFTHANGUP_DEV;
  924. c->pvt->pvt = NULL;
  925. pvt->owner = NULL;
  926. ast_mutex_lock(&usecnt_lock);
  927. usecnt--;
  928. if (usecnt < 0)
  929. ast_log(LOG_WARNING, "Usecnt < 0???\n");
  930. ast_mutex_unlock(&usecnt_lock);
  931. ast_update_use_count();
  932. }
  933. ast_mutex_unlock(&iaxsl[callno]);
  934. return 0;
  935. }
  936. static int iax_predestroy_nolock(int callno)
  937. {
  938. int res;
  939. ast_mutex_unlock(&iaxsl[callno]);
  940. res = iax_predestroy(callno);
  941. ast_mutex_lock(&iaxsl[callno]);
  942. return res;
  943. }
  944. static void iax_destroy(int callno)
  945. {
  946. struct chan_iax_pvt *pvt;
  947. struct ast_iax_frame *cur;
  948. struct ast_channel *owner;
  949. retry:
  950. ast_mutex_lock(&iaxsl[callno]);
  951. pvt = iaxs[callno];
  952. iaxs[callno] = NULL;
  953. if (pvt)
  954. owner = pvt->owner;
  955. else
  956. owner = NULL;
  957. if (owner) {
  958. if (ast_mutex_trylock(&owner->lock)) {
  959. ast_log(LOG_NOTICE, "Avoiding IAX destroy deadlock\n");
  960. ast_mutex_unlock(&iaxsl[callno]);
  961. usleep(1);
  962. goto retry;
  963. }
  964. }
  965. if (pvt) {
  966. pvt->owner = NULL;
  967. /* No more pings or lagrq's */
  968. if (pvt->pingid > -1)
  969. ast_sched_del(sched, pvt->pingid);
  970. if (pvt->lagid > -1)
  971. ast_sched_del(sched, pvt->lagid);
  972. if (pvt->autoid > -1)
  973. ast_sched_del(sched, pvt->autoid);
  974. if (pvt->initid > -1)
  975. ast_sched_del(sched, pvt->initid);
  976. pvt->pingid = -1;
  977. pvt->lagid = -1;
  978. pvt->autoid = -1;
  979. pvt->initid = -1;
  980. /* Already gone */
  981. pvt->alreadygone = 1;
  982. if (owner) {
  983. /* If there's an owner, prod it to give up */
  984. owner->pvt->pvt = NULL;
  985. owner->_softhangup |= AST_SOFTHANGUP_DEV;
  986. ast_queue_hangup(owner);
  987. }
  988. for (cur = iaxq.head; cur ; cur = cur->next) {
  989. /* Cancel any pending transmissions */
  990. if (cur->callno == pvt->callno)
  991. cur->retries = -1;
  992. }
  993. if (pvt->reg) {
  994. pvt->reg->callno = -1;
  995. }
  996. free(pvt);
  997. }
  998. if (owner) {
  999. ast_mutex_unlock(&owner->lock);
  1000. }
  1001. ast_mutex_unlock(&iaxsl[callno]);
  1002. }
  1003. static void iax_destroy_nolock(int callno)
  1004. {
  1005. /* Actually it's easier to unlock, kill it, and relock */
  1006. ast_mutex_unlock(&iaxsl[callno]);
  1007. iax_destroy(callno);
  1008. ast_mutex_lock(&iaxsl[callno]);
  1009. }
  1010. static int attempt_transmit(void *data)
  1011. {
  1012. /* Attempt to transmit the frame to the remote peer...
  1013. Called without iaxsl held. */
  1014. struct ast_iax_frame *f = data;
  1015. int freeme=0;
  1016. int callno = f->callno;
  1017. char iabuf[INET_ADDRSTRLEN];
  1018. /* Make sure this call is still active */
  1019. if (callno > -1)
  1020. ast_mutex_lock(&iaxsl[callno]);
  1021. if ((f->callno > -1) && iaxs[f->callno]) {
  1022. if ((f->retries < 0) /* Already ACK'd */ ||
  1023. (f->retries >= max_retries) /* Too many attempts */) {
  1024. /* Record an error if we've transmitted too many times */
  1025. if (f->retries >= max_retries) {
  1026. if (f->transfer) {
  1027. /* Transfer timeout */
  1028. send_command(iaxs[f->callno], AST_FRAME_IAX, AST_IAX_COMMAND_TXREJ, 0, NULL, 0, -1);
  1029. } else if (f->final) {
  1030. if (f->final)
  1031. iax_destroy_nolock(f->callno);
  1032. } else {
  1033. if (iaxs[f->callno]->owner)
  1034. ast_log(LOG_WARNING, "Max retries exceeded to host %s on %s (type = %d, subclass = %d, ts=%d, seqno=%d)\n", ast_inet_ntoa(iabuf, sizeof(iabuf), iaxs[f->callno]->addr.sin_addr),iaxs[f->callno]->owner->name , f->f->frametype, f->f->subclass, f->ts, f->seqno);
  1035. iaxs[f->callno]->error = ETIMEDOUT;
  1036. if (iaxs[f->callno]->owner) {
  1037. struct ast_frame fr = { 0, };
  1038. /* Hangup the fd */
  1039. fr.frametype = AST_FRAME_CONTROL;
  1040. fr.subclass = AST_CONTROL_HANGUP;
  1041. iax_queue_frame(f->callno, &fr);
  1042. } else {
  1043. if (iaxs[f->callno]->reg) {
  1044. memset(&iaxs[f->callno]->reg->us, 0, sizeof(iaxs[f->callno]->reg->us));
  1045. iaxs[f->callno]->reg->regstate = REG_STATE_TIMEOUT;
  1046. iaxs[f->callno]->reg->refresh = AST_DEFAULT_REG_EXPIRE;
  1047. }
  1048. iax_destroy_nolock(f->callno);
  1049. }
  1050. }
  1051. }
  1052. freeme++;
  1053. } else {
  1054. /* Attempt transmission */
  1055. send_packet(f);
  1056. f->retries++;
  1057. /* Try again later after 10 times as long */
  1058. f->retrytime *= 10;
  1059. if (f->retrytime > MAX_RETRY_TIME)
  1060. f->retrytime = MAX_RETRY_TIME;
  1061. /* Transfer messages max out at one second */
  1062. if (f->transfer && (f->retrytime > 1000))
  1063. f->retrytime = 1000;
  1064. f->retrans = ast_sched_add(sched, f->retrytime, attempt_transmit, f);
  1065. }
  1066. } else {
  1067. /* Make sure it gets freed */
  1068. f->retries = -1;
  1069. freeme++;
  1070. }
  1071. if (callno > -1)
  1072. ast_mutex_unlock(&iaxsl[callno]);
  1073. /* Do not try again */
  1074. if (freeme) {
  1075. /* Don't attempt delivery, just remove it from the queue */
  1076. ast_mutex_lock(&iaxq.lock);
  1077. if (f->prev)
  1078. f->prev->next = f->next;
  1079. else
  1080. iaxq.head = f->next;
  1081. if (f->next)
  1082. f->next->prev = f->prev;
  1083. else
  1084. iaxq.tail = f->prev;
  1085. iaxq.count--;
  1086. ast_mutex_unlock(&iaxq.lock);
  1087. /* Free the frame */
  1088. ast_frfree(f->f);
  1089. f->retrans = -1;
  1090. ast_iax_frame_free(f);
  1091. }
  1092. return 0;
  1093. }
  1094. static int iax_set_jitter(int fd, int argc, char *argv[])
  1095. {
  1096. if ((argc != 4) && (argc != 5))
  1097. return RESULT_SHOWUSAGE;
  1098. if (argc == 4) {
  1099. max_jitter_buffer = atoi(argv[3]);
  1100. if (max_jitter_buffer < 0)
  1101. max_jitter_buffer = 0;
  1102. } else {
  1103. if (argc == 5) {
  1104. if ((atoi(argv[3]) >= 0) && (atoi(argv[3]) < AST_IAX_MAX_CALLS)) {
  1105. if (iaxs[atoi(argv[3])]) {
  1106. iaxs[atoi(argv[3])]->jitterbuffer = atoi(argv[4]);
  1107. if (iaxs[atoi(argv[3])]->jitterbuffer < 0)
  1108. iaxs[atoi(argv[3])]->jitterbuffer = 0;
  1109. } else
  1110. ast_cli(fd, "No such call '%d'\n", atoi(argv[3]));
  1111. } else
  1112. ast_cli(fd, "%d is not a valid call number\n", atoi(argv[3]));
  1113. }
  1114. }
  1115. return RESULT_SUCCESS;
  1116. }
  1117. static char jitter_usage[] =
  1118. "Usage: iax set jitter [callid] <value>\n"
  1119. " If used with a callid, it sets the jitter buffer to the given static\n"
  1120. "value (until its next calculation). If used without a callid, the value is used\n"
  1121. "to establish the maximum excess jitter buffer that is permitted before the jitter\n"
  1122. "buffer size is reduced.";
  1123. static int iax_show_stats(int fd, int argc, char *argv[])
  1124. {
  1125. struct ast_iax_frame *cur;
  1126. int cnt = 0, dead=0, final=0;
  1127. if (argc != 3)
  1128. return RESULT_SHOWUSAGE;
  1129. for (cur = iaxq.head; cur ; cur = cur->next) {
  1130. if (cur->retries < 0)
  1131. dead++;
  1132. if (cur->final)
  1133. final++;
  1134. cnt++;
  1135. }
  1136. ast_cli(fd, " IAX Statistics\n");
  1137. ast_cli(fd, "---------------------\n");
  1138. ast_cli(fd, "Outstanding frames: %d (%d ingress, %d outgress)\n", frames, iframes, oframes);
  1139. ast_cli(fd, "Packets in transmit queue: %d dead, %d final, %d total\n", dead, final, cnt);
  1140. return RESULT_SUCCESS;
  1141. }
  1142. static int iax_show_cache(int fd, int argc, char *argv[])
  1143. {
  1144. struct iax_dpcache *dp;
  1145. char tmp[1024] = "", *pc;
  1146. int s;
  1147. int x,y;
  1148. struct timeval tv;
  1149. gettimeofday(&tv, NULL);
  1150. ast_mutex_lock(&dpcache_lock);
  1151. dp = dpcache;
  1152. ast_cli(fd, "%-20.20s %-12.12s %-9.9s %-8.8s %s\n", "Peer/Context", "Exten", "Exp.", "Wait.", "Flags");
  1153. while(dp) {
  1154. s = dp->expirey.tv_sec - tv.tv_sec;
  1155. tmp[0] = '\0';
  1156. if (dp->flags & CACHE_FLAG_EXISTS)
  1157. strncat(tmp, "EXISTS|", sizeof(tmp) - strlen(tmp) - 1);
  1158. if (dp->flags & CACHE_FLAG_NONEXISTANT)
  1159. strncat(tmp, "NONEXISTANT|", sizeof(tmp) - strlen(tmp) - 1);
  1160. if (dp->flags & CACHE_FLAG_CANEXIST)
  1161. strncat(tmp, "CANEXIST|", sizeof(tmp) - strlen(tmp) - 1);
  1162. if (dp->flags & CACHE_FLAG_PENDING)
  1163. strncat(tmp, "PENDING|", sizeof(tmp) - strlen(tmp) - 1);
  1164. if (dp->flags & CACHE_FLAG_TIMEOUT)
  1165. strncat(tmp, "TIMEOUT|", sizeof(tmp) - strlen(tmp) - 1);
  1166. if (dp->flags & CACHE_FLAG_TRANSMITTED)
  1167. strncat(tmp, "TRANSMITTED|", sizeof(tmp) - strlen(tmp) - 1);
  1168. if (dp->flags & CACHE_FLAG_MATCHMORE)
  1169. strncat(tmp, "MATCHMORE|", sizeof(tmp) - strlen(tmp) - 1);
  1170. if (dp->flags & CACHE_FLAG_UNKNOWN)
  1171. strncat(tmp, "UNKNOWN|", sizeof(tmp) - strlen(tmp) - 1);
  1172. /* Trim trailing pipe */
  1173. if (strlen(tmp))
  1174. tmp[strlen(tmp) - 1] = '\0';
  1175. else
  1176. strncpy(tmp, "(none)", sizeof(tmp) - 1);
  1177. y=0;
  1178. pc = strchr(dp->peercontext, '@');
  1179. if (!pc)
  1180. pc = dp->peercontext;
  1181. else
  1182. pc++;
  1183. for (x=0;x<sizeof(dp->waiters) / sizeof(dp->waiters[0]); x++)
  1184. if (dp->waiters[x] > -1)
  1185. y++;
  1186. if (s > 0)
  1187. ast_cli(fd, "%-20.20s %-12.12s %-9d %-8d %s\n", pc, dp->exten, s, y, tmp);
  1188. else
  1189. ast_cli(fd, "%-20.20s %-12.12s %-9.9s %-8d %s\n", pc, dp->exten, "(expired)", y, tmp);
  1190. dp = dp->next;
  1191. }
  1192. ast_mutex_unlock(&dpcache_lock);
  1193. return RESULT_SUCCESS;
  1194. }
  1195. static char show_stats_usage[] =
  1196. "Usage: iax1 show stats\n"
  1197. " Display statistics on IAX channel driver.\n";
  1198. static char show_cache_usage[] =
  1199. "Usage: iax1 show cache\n"
  1200. " Display currently cached IAX Dialplan results.\n";
  1201. static struct ast_cli_entry cli_set_jitter =
  1202. { { "iax1", "set", "jitter", NULL }, iax_set_jitter, "Sets IAX jitter buffer", jitter_usage };
  1203. static struct ast_cli_entry cli_show_stats =
  1204. { { "iax1", "show", "stats", NULL }, iax_show_stats, "Display IAX statistics", show_stats_usage };
  1205. static struct ast_cli_entry cli_show_cache =
  1206. { { "iax1", "show", "cache", NULL }, iax_show_cache, "Display IAX cached dialplan", show_cache_usage };
  1207. static unsigned int calc_rxstamp(struct chan_iax_pvt *p);
  1208. #ifdef BRIDGE_OPTIMIZATION
  1209. static unsigned int calc_fakestamp(struct chan_iax_pvt *from, struct chan_iax_pvt *to, unsigned int ts);
  1210. static int forward_delivery(struct ast_iax_frame *fr)
  1211. {
  1212. struct chan_iax_pvt *p1, *p2;
  1213. p1 = iaxs[fr->callno];
  1214. p2 = iaxs[p1->bridgecallno];
  1215. if (!p1)
  1216. return -1;
  1217. if (!p2)
  1218. return -1;
  1219. /* Fix relative timestamp */
  1220. fr->ts = calc_fakestamp(p1, p2, fr->ts);
  1221. /* Now just send it send on the 2nd one
  1222. with adjusted timestamp */
  1223. return iax_send(p2, fr->f, fr->ts, -1, 0, 0, 0);
  1224. }
  1225. #endif
  1226. static int schedule_delivery(struct ast_iax_frame *fr, int reallydeliver)
  1227. {
  1228. int ms,x;
  1229. int drops[MEMORY_SIZE];
  1230. int min, max=0, maxone=0,y,z, match;
  1231. /* ms is a measure of the "lateness" of the packet relative to the first
  1232. packet we received, which always has a lateness of 1. Called by
  1233. IAX thread, with iaxsl lock held. */
  1234. ms = calc_rxstamp(iaxs[fr->callno]) - fr->ts;
  1235. if (ms > 32767) {
  1236. /* What likely happened here is that our counter has circled but we haven't
  1237. gotten the update from the main packet. We'll just pretend that we did, and
  1238. update the timestamp appropriately. */
  1239. ms -= 65536;
  1240. }
  1241. if (ms < -32768) {
  1242. /* We got this packet out of order. Lets add 65536 to it to bring it into our new
  1243. time frame */
  1244. ms += 65536;
  1245. }
  1246. /* Rotate our history queue of "lateness". Don't worry about those initial
  1247. zeros because the first entry will always be zero */
  1248. for (x=0;x<MEMORY_SIZE - 1;x++)
  1249. iaxs[fr->callno]->history[x] = iaxs[fr->callno]->history[x+1];
  1250. /* Add a history entry for this one */
  1251. iaxs[fr->callno]->history[x] = ms;
  1252. /* Initialize the minimum to reasonable values. It's too much
  1253. work to do the same for the maximum, repeatedly */
  1254. min=iaxs[fr->callno]->history[0];
  1255. for (z=0;z < iax_dropcount + 1;z++) {
  1256. /* Start very optimistic ;-) */
  1257. max=-999999999;
  1258. for (x=0;x<MEMORY_SIZE;x++) {
  1259. if (max < iaxs[fr->callno]->history[x]) {
  1260. /* We have a candidate new maximum value. Make
  1261. sure it's not in our drop list */
  1262. match = 0;
  1263. for (y=0;!match && (y<z);y++)
  1264. match |= (drops[y] == x);
  1265. if (!match) {
  1266. /* It's not in our list, use it as the new maximum */
  1267. max = iaxs[fr->callno]->history[x];
  1268. maxone = x;
  1269. }
  1270. }
  1271. if (!z) {
  1272. /* On our first pass, find the minimum too */
  1273. if (min > iaxs[fr->callno]->history[x])
  1274. min = iaxs[fr->callno]->history[x];
  1275. }
  1276. }
  1277. #if 1
  1278. drops[z] = maxone;
  1279. #endif
  1280. }
  1281. /* Just for reference, keep the "jitter" value, the difference between the
  1282. earliest and the latest. */
  1283. iaxs[fr->callno]->jitter = max - min;
  1284. /* IIR filter for keeping track of historic jitter, but always increase
  1285. historic jitter immediately for increase */
  1286. if (iaxs[fr->callno]->jitter > iaxs[fr->callno]->historicjitter )
  1287. iaxs[fr->callno]->historicjitter = iaxs[fr->callno]->jitter;
  1288. else
  1289. iaxs[fr->callno]->historicjitter = GAMMA * (double)iaxs[fr->callno]->jitter + (1-GAMMA) *
  1290. iaxs[fr->callno]->historicjitter;
  1291. /* If our jitter buffer is too big (by a significant margin), then we slowly
  1292. shrink it by about 1 ms each time to avoid letting the change be perceived */
  1293. if (max < iaxs[fr->callno]->jitterbuffer - max_jitter_buffer)
  1294. iaxs[fr->callno]->jitterbuffer -= 2;
  1295. #if 1
  1296. /* Constrain our maximum jitter buffer appropriately */
  1297. if (max > min + maxjitterbuffer) {
  1298. if (option_debug)
  1299. ast_log(LOG_DEBUG, "Constraining buffer from %d to %d + %d\n", max, min , maxjitterbuffer);
  1300. max = min + maxjitterbuffer;
  1301. }
  1302. #endif
  1303. /* If our jitter buffer is smaller than our maximum delay, grow the jitter
  1304. buffer immediately to accomodate it (and a little more). */
  1305. if (max > iaxs[fr->callno]->jitterbuffer)
  1306. iaxs[fr->callno]->jitterbuffer = max
  1307. /* + ((float)iaxs[fr->callno]->jitter) * 0.1 */;
  1308. if (option_debug)
  1309. ast_log(LOG_DEBUG, "min = %d, max = %d, jb = %d, lateness = %d\n", min, max, iaxs[fr->callno]->jitterbuffer, ms);
  1310. /* Subtract the lateness from our jitter buffer to know how long to wait
  1311. before sending our packet. */
  1312. ms = iaxs[fr->callno]->jitterbuffer - ms;
  1313. if (!use_jitterbuffer)
  1314. ms = 0;
  1315. /* If the caller just wanted us to update, return now */
  1316. if (!reallydeliver)
  1317. return 0;
  1318. if (ms < 1) {
  1319. if (option_debug)
  1320. ast_log(LOG_DEBUG, "Calculated ms is %d\n", ms);
  1321. /* Don't deliver it more than 4 ms late */
  1322. if ((ms > -4) || (fr->f->frametype != AST_FRAME_VOICE)) {
  1323. __do_deliver(fr);
  1324. } else {
  1325. if (option_debug)
  1326. ast_log(LOG_DEBUG, "Dropping voice packet since %d ms is, too old\n", ms);
  1327. /* Free the packet */
  1328. ast_frfree(fr->f);
  1329. /* And our iax frame */
  1330. ast_iax_frame_free(fr);
  1331. }
  1332. } else {
  1333. if (option_debug)
  1334. ast_log(LOG_DEBUG, "Scheduling delivery in %d ms\n", ms);
  1335. fr->retrans = ast_sched_add(sched, ms, do_deliver, fr);
  1336. }
  1337. return 0;
  1338. }
  1339. static int iax_transmit(struct ast_iax_frame *fr)
  1340. {
  1341. /* Lock the queue and place this packet at the end */
  1342. fr->next = NULL;
  1343. fr->prev = NULL;
  1344. /* By setting this to 0, the network thread will send it for us, and
  1345. queue retransmission if necessary */
  1346. fr->sentyet = 0;
  1347. ast_mutex_lock(&iaxq.lock);
  1348. if (!iaxq.head) {
  1349. /* Empty queue */
  1350. iaxq.head = fr;
  1351. iaxq.tail = fr;
  1352. } else {
  1353. /* Double link */
  1354. iaxq.tail->next = fr;
  1355. fr->prev = iaxq.tail;
  1356. iaxq.tail = fr;
  1357. }
  1358. iaxq.count++;
  1359. ast_mutex_unlock(&iaxq.lock);
  1360. /* Wake up the network thread */
  1361. pthread_kill(netthreadid, SIGURG);
  1362. return 0;
  1363. }
  1364. static int iax_digit(struct ast_channel *c, char digit)
  1365. {
  1366. return send_command(c->pvt->pvt, AST_FRAME_DTMF, digit, 0, NULL, 0, -1);
  1367. }
  1368. static int iax_sendtext(struct ast_channel *c, char *text)
  1369. {
  1370. return send_command(c->pvt->pvt, AST_FRAME_TEXT,
  1371. 0, 0, text, strlen(text) + 1, -1);
  1372. }
  1373. static int iax_sendimage(struct ast_channel *c, struct ast_frame *img)
  1374. {
  1375. return send_command(c->pvt->pvt, AST_FRAME_IMAGE, img->subclass, 0, img->data, img->datalen, -1);
  1376. }
  1377. static int iax_sendhtml(struct ast_channel *c, int subclass, char *data, int datalen)
  1378. {
  1379. return send_command(c->pvt->pvt, AST_FRAME_HTML, subclass, 0, data, datalen, -1);
  1380. }
  1381. static int iax_fixup(struct ast_channel *oldchannel, struct ast_channel *newchan)
  1382. {
  1383. struct chan_iax_pvt *pvt = newchan->pvt->pvt;
  1384. pvt->owner = newchan;
  1385. return 0;
  1386. }
  1387. #ifdef MYSQL_FRIENDS
  1388. static void mysql_update_peer(char *peer, struct sockaddr_in *sin)
  1389. {
  1390. if (mysql && (strlen(peer) < 128)) {
  1391. char query[512];
  1392. char *name;
  1393. time_t nowtime;
  1394. name = alloca(strlen(peer) * 2 + 1);
  1395. time(&nowtime);
  1396. mysql_real_escape_string(mysql, name, peer, strlen(peer));
  1397. snprintf(query, sizeof(query), "UPDATE iax1friends SET ipaddr=\"%s\", port=\"%d\", regseconds=\"%ld\" WHERE name=\"%s\"",
  1398. ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), ntohs(sin->sin_port), nowtime, name);
  1399. ast_mutex_lock(&mysqllock);
  1400. if (mysql_real_query(mysql, query, strlen(query)))
  1401. ast_log(LOG_WARNING, "Unable to update database\n");
  1402. ast_mutex_unlock(&mysqllock);
  1403. }
  1404. }
  1405. static struct iax_peer *mysql_peer(char *peer)
  1406. {
  1407. struct iax_peer *p;
  1408. int success = 0;
  1409. p = malloc(sizeof(struct iax_peer));
  1410. memset(p, 0, sizeof(struct iax_peer));
  1411. if (mysql && (strlen(peer) < 128)) {
  1412. char query[512];
  1413. char *name;
  1414. int numfields, x;
  1415. int port;
  1416. time_t regseconds, nowtime;
  1417. MYSQL_RES *result;
  1418. MYSQL_FIELD *fields;
  1419. MYSQL_ROW rowval;
  1420. name = alloca(strlen(peer) * 2 + 1);
  1421. mysql_real_escape_string(mysql, name, peer, strlen(peer));
  1422. snprintf(query, sizeof(query), "SELECT name, secret, context, ipaddr, port, regseconds FROM iax1friends WHERE name=\"%s\"", name);
  1423. ast_mutex_lock(&mysqllock);
  1424. mysql_query(mysql, query);
  1425. if ((result = mysql_store_result(mysql))) {
  1426. if ((rowval = mysql_fetch_row(result))) {
  1427. numfields = mysql_num_fields(result);
  1428. fields = mysql_fetch_fields(result);
  1429. success = 1;
  1430. for (x=0;x<numfields;x++) {
  1431. if (rowval[x]) {
  1432. if (!strcasecmp(fields[x].name, "secret")) {
  1433. strncpy(p->secret, rowval[x], sizeof(p->secret) - 1);
  1434. } else if (!strcasecmp(fields[x].name, "context")) {
  1435. strncpy(p->context, rowval[x], sizeof(p->context) - 1);
  1436. } else if (!strcasecmp(fields[x].name, "ipaddr")) {
  1437. inet_aton(rowval[x], &p->addr.sin_addr);
  1438. } else if (!strcasecmp(fields[x].name, "port")) {
  1439. if (sscanf(rowval[x], "%i", &port) != 1)
  1440. port = 0;
  1441. p->addr.sin_port = htons(port);
  1442. } else if (!strcasecmp(fields[x].name, "regseconds")) {
  1443. if (sscanf(rowval[x], "%li", &regseconds) != 1)
  1444. regseconds = 0;
  1445. }
  1446. }
  1447. }
  1448. time(&nowtime);
  1449. if ((nowtime - regseconds) > AST_DEFAULT_REG_EXPIRE)
  1450. memset(&p->addr, 0, sizeof(p->addr));
  1451. }
  1452. }
  1453. ast_mutex_unlock(&mysqllock);
  1454. }
  1455. if (!success) {
  1456. free(p);
  1457. p = NULL;
  1458. } else {
  1459. strncpy(p->name, peer, sizeof(p->name) - 1);
  1460. p->dynamic = 1;
  1461. p->delme = 1;
  1462. p->expire = -1;
  1463. p->capability = iax_capability;
  1464. strncpy(p->methods, "md5,plaintext", sizeof(p->methods) - 1);
  1465. }
  1466. return p;
  1467. }
  1468. static struct iax_user *mysql_user(char *user)
  1469. {
  1470. struct iax_user *p;
  1471. struct iax_context *con;
  1472. int success = 0;
  1473. p = malloc(sizeof(struct iax_user));
  1474. memset(p, 0, sizeof(struct iax_user));
  1475. con = malloc(sizeof(struct iax_context));
  1476. memset(con, 0, sizeof(struct iax_context));
  1477. strncpy(con->context, "default", sizeof(con->context) - 1);
  1478. p->contexts = con;
  1479. if (mysql && (strlen(user) < 128)) {
  1480. char query[512];
  1481. char *name;
  1482. int numfields, x;
  1483. MYSQL_RES *result;
  1484. MYSQL_FIELD *fields;
  1485. MYSQL_ROW rowval;
  1486. name = alloca(strlen(user) * 2 + 1);
  1487. mysql_real_escape_string(mysql, name, user, strlen(user));
  1488. snprintf(query, sizeof(query), "SELECT name, secret, context, ipaddr, port, regseconds FROM iax1friends WHERE name=\"%s\"", name);
  1489. ast_mutex_lock(&mysqllock);
  1490. mysql_query(mysql, query);
  1491. if ((result = mysql_store_result(mysql))) {
  1492. if ((rowval = mysql_fetch_row(result))) {
  1493. numfields = mysql_num_fields(result);
  1494. fields = mysql_fetch_fields(result);
  1495. success = 1;
  1496. for (x=0;x<numfields;x++) {
  1497. if (rowval[x]) {
  1498. if (!strcasecmp(fields[x].name, "secret")) {
  1499. strncpy(p->secret, rowval[x], sizeof(p->secret) - 1);
  1500. } else if (!strcasecmp(fields[x].name, "context")) {
  1501. strncpy(p->contexts->context, rowval[x], sizeof(p->contexts->context) - 1);
  1502. }
  1503. }
  1504. }
  1505. }
  1506. }
  1507. ast_mutex_unlock(&mysqllock);
  1508. }
  1509. if (!success) {
  1510. if (p->contexts)
  1511. free(p->contexts);
  1512. free(p);
  1513. p = NULL;
  1514. } else {
  1515. strncpy(p->name, user, sizeof(p->name) - 1);
  1516. p->delme = 1;
  1517. strncpy(p->methods, "md5,plaintext", sizeof(p->methods) - 1);
  1518. }
  1519. return p;
  1520. }
  1521. #endif /* MYSQL_FRIENDS */
  1522. static int create_addr(struct sockaddr_in *sin, int *capability, int *sendani, int *maxtime, char *peer, char *context)
  1523. {
  1524. struct ast_hostent ahp; struct hostent *hp;
  1525. struct iax_peer *p;
  1526. int found=0;
  1527. if (sendani)
  1528. *sendani = 0;
  1529. if (maxtime)
  1530. *maxtime = 0;
  1531. sin->sin_family = AF_INET;
  1532. ast_mutex_lock(&peerl.lock);
  1533. p = peerl.peers;
  1534. while(p) {
  1535. if (!strcasecmp(p->name, peer)) {
  1536. break;
  1537. }
  1538. p = p->next;
  1539. }
  1540. #ifdef MYSQL_FRIENDS
  1541. if (!p)
  1542. p = mysql_peer(peer);
  1543. #endif
  1544. if (p) {
  1545. found++;
  1546. if (capability)
  1547. *capability = p->capability;
  1548. if ((p->addr.sin_addr.s_addr || p->defaddr.sin_addr.s_addr) &&
  1549. (!p->maxms || ((p->lastms > 0) && (p->lastms <= p->maxms)))) {
  1550. if (sendani)
  1551. *sendani = p->sendani; /* Whether we transmit ANI */
  1552. if (maxtime)
  1553. *maxtime = p->maxms; /* Max time they should take */
  1554. if (context)
  1555. strncpy(context, p->context, AST_MAX_EXTENSION - 1);
  1556. if (p->addr.sin_addr.s_addr) {
  1557. sin->sin_addr = p->addr.sin_addr;
  1558. sin->sin_port = p->addr.sin_port;
  1559. } else {
  1560. sin->sin_addr = p->defaddr.sin_addr;
  1561. sin->sin_port = p->defaddr.sin_port;
  1562. }
  1563. } else {
  1564. if (p->delme)
  1565. free(p);
  1566. p = NULL;
  1567. }
  1568. }
  1569. ast_mutex_unlock(&peerl.lock);
  1570. if (!p && !found) {
  1571. hp = ast_gethostbyname(peer, &ahp);
  1572. if (hp) {
  1573. memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
  1574. sin->sin_port = htons(AST_DEFAULT_IAX_PORTNO);
  1575. return 0;
  1576. } else {
  1577. ast_log(LOG_WARNING, "No such host: %s\n", peer);
  1578. return -1;
  1579. }
  1580. } else if (!p)
  1581. return -1;
  1582. if (p->delme)
  1583. free(p);
  1584. return 0;
  1585. }
  1586. static int auto_congest(void *nothing)
  1587. {
  1588. int callno = (int)(long)(nothing);
  1589. struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_CONGESTION };
  1590. ast_mutex_lock(&iaxsl[callno]);
  1591. if (iaxs[callno]) {
  1592. iaxs[callno]->initid = -1;
  1593. iax_queue_frame(callno, &f);
  1594. ast_log(LOG_NOTICE, "Auto-congesting call due to slow response\n");
  1595. }
  1596. ast_mutex_unlock(&iaxsl[callno]);
  1597. return 0;
  1598. }
  1599. static int iax_call(struct ast_channel *c, char *dest, int timeout)
  1600. {
  1601. struct sockaddr_in sin;
  1602. char host[256];
  1603. char *rdest;
  1604. char *rcontext;
  1605. char *username;
  1606. char *secret = NULL;
  1607. char *hname;
  1608. char requeststr[256] = "";
  1609. char myrdest [5] = "s";
  1610. char context[AST_MAX_EXTENSION] ="";
  1611. char *portno = NULL;
  1612. struct chan_iax_pvt *p = c->pvt->pvt;
  1613. char *stringp=NULL;
  1614. if ((c->_state != AST_STATE_DOWN) && (c->_state != AST_STATE_RESERVED)) {
  1615. ast_log(LOG_WARNING, "Line is already in use (%s)?\n", c->name);
  1616. return -1;
  1617. }
  1618. strncpy(host, dest, sizeof(host)-1);
  1619. stringp=host;
  1620. strsep(&stringp, "/");
  1621. /* If no destination extension specified, use 's' */
  1622. rdest = strsep(&stringp, "/");
  1623. if (!rdest)
  1624. rdest = myrdest;
  1625. stringp=rdest;
  1626. strsep(&stringp, "@");
  1627. rcontext = strsep(&stringp, "@");
  1628. stringp=host;
  1629. strsep(&stringp, "@");
  1630. username = strsep(&stringp, "@");
  1631. if (username) {
  1632. /* Really the second argument is the host, not the username */
  1633. hname = username;
  1634. username = host;
  1635. } else {
  1636. hname = host;
  1637. }
  1638. if (username) {
  1639. stringp=username;
  1640. username = strsep(&stringp, ":");
  1641. secret = strsep(&stringp, ":");
  1642. }
  1643. stringp=hname;
  1644. if (strsep(&stringp, ":")) {
  1645. stringp=hname;
  1646. strsep(&stringp, ":");
  1647. portno = strsep(&stringp, ":");
  1648. }
  1649. if (create_addr(&sin, NULL, NULL, NULL, hname, context)) {
  1650. ast_log(LOG_WARNING, "No address associated with '%s'\n", hname);
  1651. return -1;
  1652. }
  1653. /* Keep track of the context for outgoing calls too */
  1654. strncpy(c->context, context, sizeof(c->context) - 1);
  1655. if (portno) {
  1656. sin.sin_port = htons(atoi(portno));
  1657. }
  1658. /* Now we build our request string */
  1659. #define MYSNPRINTF snprintf(requeststr + strlen(requeststr), sizeof(requeststr) - strlen(requeststr),
  1660. #define MYSNPRINTF2 snprintf(requeststr + strlen(requeststr), reqsize - strlen(requeststr),
  1661. MYSNPRINTF "exten=%s;", rdest);
  1662. if (c->callerid)
  1663. MYSNPRINTF "callerid=%s;", c->callerid);
  1664. if (p->sendani && c->ani)
  1665. MYSNPRINTF "ani=%s;", c->ani);
  1666. if (c->language && strlen(c->language))
  1667. MYSNPRINTF "language=%s;", c->language);
  1668. if (c->dnid)
  1669. MYSNPRINTF "dnid=%s;", c->dnid);
  1670. if (rcontext)
  1671. MYSNPRINTF "context=%s;", rcontext);
  1672. if (username)
  1673. MYSNPRINTF "username=%s;", username);
  1674. if (secret) {
  1675. if (secret[0] == '[') {
  1676. /* This is an RSA key, not a normal secret */
  1677. strncpy(p->outkey, secret + 1, sizeof(p->secret)-1);
  1678. if (strlen(p->outkey)) {
  1679. p->outkey[strlen(p->outkey) - 1] = '\0';
  1680. }
  1681. } else
  1682. strncpy(p->secret, secret, sizeof(p->secret)-1);
  1683. }
  1684. MYSNPRINTF "formats=%d;", c->nativeformats);
  1685. MYSNPRINTF "capability=%d;", p->capability);
  1686. MYSNPRINTF "version=%d;", AST_IAX_PROTO_VERSION);
  1687. MYSNPRINTF "adsicpe=%d;", c->adsicpe);
  1688. /* Trim the trailing ";" */
  1689. if (strlen(requeststr))
  1690. requeststr[strlen(requeststr) - 1] = '\0';
  1691. /* Transmit the string in a "NEW" request */
  1692. if (option_verbose > 2)
  1693. ast_verbose(VERBOSE_PREFIX_3 "Calling using options '%s'\n", requeststr);
  1694. if (p->maxtime) {
  1695. /* Initialize pingtime and auto-congest time */
  1696. p->pingtime = p->maxtime / 2;
  1697. p->initid = ast_sched_add(sched, p->maxtime * 2, auto_congest, (void *)(long)p->callno);
  1698. }
  1699. send_command(p, AST_FRAME_IAX,
  1700. AST_IAX_COMMAND_NEW, 0, requeststr, strlen(requeststr) + 1, -1);
  1701. ast_setstate(c, AST_STATE_RINGING);
  1702. return 0;
  1703. }
  1704. static int iax_hangup(struct ast_channel *c)
  1705. {
  1706. struct chan_iax_pvt *pvt = c->pvt->pvt;
  1707. int alreadygone;
  1708. int callno;
  1709. if (pvt) {
  1710. callno = pvt->callno;
  1711. ast_mutex_lock(&iaxsl[callno]);
  1712. ast_log(LOG_DEBUG, "We're hanging up %s now...\n", c->name);
  1713. alreadygone = pvt->alreadygone;
  1714. /* Send the hangup unless we have had a transmission error or are already gone */
  1715. if (!pvt->error && !alreadygone)
  1716. send_command_final(pvt, AST_FRAME_IAX, AST_IAX_COMMAND_HANGUP, 0, NULL, 0, -1);
  1717. /* Explicitly predestroy it */
  1718. iax_predestroy_nolock(callno);
  1719. /* If we were already gone to begin with, destroy us now */
  1720. if (alreadygone) {
  1721. ast_log(LOG_DEBUG, "Really destroying %s now...\n", c->name);
  1722. iax_destroy_nolock(callno);
  1723. }
  1724. ast_mutex_unlock(&iaxsl[callno]);
  1725. }
  1726. if (option_verbose > 2)
  1727. ast_verbose(VERBOSE_PREFIX_3 "Hungup '%s'\n", c->name);
  1728. return 0;
  1729. }
  1730. static int iax_setoption(struct ast_channel *c, int option, void *data, int datalen)
  1731. {
  1732. struct ast_option_header *h;
  1733. int res;
  1734. h = malloc(datalen + sizeof(struct ast_option_header));
  1735. if (h) {
  1736. h->flag = AST_OPTION_FLAG_REQUEST;
  1737. h->option = htons(option);
  1738. memcpy(h->data, data, datalen);
  1739. res = send_command((struct chan_iax_pvt *)c->pvt->pvt, AST_FRAME_CONTROL,
  1740. AST_CONTROL_OPTION, 0, (char *)h, datalen + sizeof(struct ast_option_header), -1);
  1741. free(h);
  1742. return res;
  1743. } else
  1744. ast_log(LOG_WARNING, "Out of memory\n");
  1745. return -1;
  1746. }
  1747. static struct ast_frame *iax_read(struct ast_channel *c)
  1748. {
  1749. static struct ast_frame f = { AST_FRAME_NULL, };
  1750. ast_log(LOG_NOTICE, "I should never be called!\n");
  1751. return &f;
  1752. }
  1753. static int iax_start_transfer(struct ast_channel *c0, struct ast_channel *c1)
  1754. {
  1755. int res;
  1756. char req0[256];
  1757. char req1[256];
  1758. char iabuf[INET_ADDRSTRLEN];
  1759. struct chan_iax_pvt *p0 = c0->pvt->pvt;
  1760. struct chan_iax_pvt *p1 = c1->pvt->pvt;
  1761. snprintf(req0, sizeof(req0), "remip=%s;remport=%d;remcall=%d;", ast_inet_ntoa(iabuf, sizeof(iabuf), p1->addr.sin_addr), ntohs(p1->addr.sin_port), p1->peercallno);
  1762. snprintf(req1, sizeof(req1), "remip=%s;remport=%d;remcall=%d;", ast_inet_ntoa(iabuf, sizeof(iabuf), p0->addr.sin_addr), ntohs(p0->addr.sin_port), p0->peercallno);
  1763. res = send_command(p0, AST_FRAME_IAX, AST_IAX_COMMAND_TXREQ, 0, req0, strlen(req0) + 1, -1);
  1764. if (res)
  1765. return -1;
  1766. res = send_command(p1, AST_FRAME_IAX, AST_IAX_COMMAND_TXREQ, 0, req1, strlen(req1) + 1, -1);
  1767. if (res)
  1768. return -1;
  1769. p0->transferring = TRANSFER_BEGIN;
  1770. p1->transferring = TRANSFER_BEGIN;
  1771. return 0;
  1772. }
  1773. static int iax_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
  1774. {
  1775. struct ast_channel *cs[3];
  1776. struct ast_channel *who;
  1777. int to = -1;
  1778. int res = -1;
  1779. int transferstarted=0;
  1780. struct ast_frame *f;
  1781. struct chan_iax_pvt *p0 = c0->pvt->pvt;
  1782. struct chan_iax_pvt *p1 = c1->pvt->pvt;
  1783. /* Put them in native bridge mode */
  1784. p0->bridgecallno = p1->callno;
  1785. p1->bridgecallno = p0->callno;
  1786. /* If not, try to bridge until we can execute a transfer, if we can */
  1787. cs[0] = c0;
  1788. cs[1] = c1;
  1789. for (/* ever */;;) {
  1790. /* Check in case we got masqueraded into */
  1791. if ((c0->type != type) || (c1->type != type)) {
  1792. if (option_verbose > 2)
  1793. ast_verbose(VERBOSE_PREFIX_3 "Can't masquerade, we're different...\n");
  1794. return -2;
  1795. }
  1796. if (c0->nativeformats != c1->nativeformats) {
  1797. ast_verbose(VERBOSE_PREFIX_3 "Operating with different codecs, can't native bridge...\n");
  1798. return -2;
  1799. }
  1800. if (!transferstarted) {
  1801. /* Try the transfer */
  1802. if (iax_start_transfer(c0, c1))
  1803. ast_log(LOG_WARNING, "Unable to start the transfer\n");
  1804. transferstarted = 1;
  1805. }
  1806. if ((p0->transferring == TRANSFER_RELEASED) && (p1->transferring == TRANSFER_RELEASED)) {
  1807. /* Call has been transferred. We're no longer involved */
  1808. sleep(1);
  1809. c0->_softhangup |= AST_SOFTHANGUP_DEV;
  1810. c1->_softhangup |= AST_SOFTHANGUP_DEV;
  1811. *fo = NULL;
  1812. *rc = c0;
  1813. res = 0;
  1814. break;
  1815. }
  1816. to = 1000;
  1817. who = ast_waitfor_n(cs, 2, &to);
  1818. if (!who) {
  1819. if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
  1820. res = -1;
  1821. break;
  1822. }
  1823. continue;
  1824. }
  1825. f = ast_read(who);
  1826. if (!f) {
  1827. *fo = NULL;
  1828. *rc = who;
  1829. res = 0;
  1830. break;
  1831. }
  1832. if ((f->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
  1833. *fo = f;
  1834. *rc = who;
  1835. res = 0;
  1836. break;
  1837. }
  1838. if ((f->frametype == AST_FRAME_VOICE) ||
  1839. (f->frametype == AST_FRAME_TEXT) ||
  1840. (f->frametype == AST_FRAME_VIDEO) ||
  1841. (f->frametype == AST_FRAME_IMAGE) ||
  1842. (f->frametype == AST_FRAME_DTMF)) {
  1843. if ((f->frametype == AST_FRAME_DTMF) &&
  1844. (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))) {
  1845. if ((who == c0)) {
  1846. if ((flags & AST_BRIDGE_DTMF_CHANNEL_0)) {
  1847. *rc = c0;
  1848. *fo = f;
  1849. /* Take out of conference mode */
  1850. res = 0;
  1851. break;
  1852. } else
  1853. goto tackygoto;
  1854. } else
  1855. if ((who == c1)) {
  1856. if (flags & AST_BRIDGE_DTMF_CHANNEL_1) {
  1857. *rc = c1;
  1858. *fo = f;
  1859. res = 0;
  1860. break;
  1861. } else
  1862. goto tackygoto;
  1863. }
  1864. } else {
  1865. #if 0
  1866. ast_log(LOG_DEBUG, "Read from %s\n", who->name);
  1867. if (who == last)
  1868. ast_log(LOG_DEBUG, "Servicing channel %s twice in a row?\n", last->name);
  1869. last = who;
  1870. #endif
  1871. tackygoto:
  1872. if (who == c0)
  1873. ast_write(c1, f);
  1874. else
  1875. ast_write(c0, f);
  1876. }
  1877. ast_frfree(f);
  1878. } else
  1879. ast_frfree(f);
  1880. /* Swap who gets priority */
  1881. cs[2] = cs[0];
  1882. cs[0] = cs[1];
  1883. cs[1] = cs[2];
  1884. }
  1885. p0->bridgecallno = -1;
  1886. p1->bridgecallno = -1;
  1887. return res;
  1888. }
  1889. static int iax_answer(struct ast_channel *c)
  1890. {
  1891. struct chan_iax_pvt *pvt = c->pvt->pvt;
  1892. if (option_debug)
  1893. ast_log(LOG_DEBUG, "Answering\n");
  1894. return send_command(pvt, AST_FRAME_CONTROL, AST_CONTROL_ANSWER, 0, NULL, 0, -1);
  1895. }
  1896. static int iax_indicate(struct ast_channel *c, int condition)
  1897. {
  1898. struct chan_iax_pvt *pvt = c->pvt->pvt;
  1899. if (option_debug)
  1900. ast_log(LOG_DEBUG, "Indicating condition %d\n", condition);
  1901. return send_command(pvt, AST_FRAME_CONTROL, condition, 0, NULL, 0, -1);
  1902. }
  1903. static int iax_write(struct ast_channel *c, struct ast_frame *f);
  1904. static int iax_getpeername(struct sockaddr_in sin, char *host, int len)
  1905. {
  1906. struct iax_peer *peer;
  1907. int res = 0;
  1908. ast_mutex_lock(&peerl.lock);
  1909. peer = peerl.peers;
  1910. while(peer) {
  1911. if ((peer->addr.sin_addr.s_addr == sin.sin_addr.s_addr) &&
  1912. (peer->addr.sin_port == sin.sin_port)) {
  1913. strncpy(host, peer->name, len-1);
  1914. res = 1;
  1915. break;
  1916. }
  1917. peer = peer->next;
  1918. }
  1919. ast_mutex_unlock(&peerl.lock);
  1920. return res;
  1921. }
  1922. static struct ast_channel *ast_iax_new(struct chan_iax_pvt *i, int state, int capability)
  1923. {
  1924. char host[256];
  1925. struct ast_channel *tmp;
  1926. char iabuf[INET_ADDRSTRLEN];
  1927. tmp = ast_channel_alloc(1);
  1928. if (tmp) {
  1929. if (!iax_getpeername(i->addr, host, sizeof(host)))
  1930. snprintf(host, sizeof(host), "%s:%d", ast_inet_ntoa(iabuf, sizeof(iabuf), i->addr.sin_addr), ntohs(i->addr.sin_port));
  1931. if (strlen(i->username))
  1932. snprintf(tmp->name, sizeof(tmp->name), "IAX[%s@%s]/%d", i->username, host, i->callno);
  1933. else
  1934. snprintf(tmp->name, sizeof(tmp->name), "IAX[%s]/%d", host, i->callno);
  1935. tmp->type = type;
  1936. /* We can support any format by default, until we get restricted */
  1937. tmp->nativeformats = capability;
  1938. tmp->readformat = 0;
  1939. tmp->writeformat = 0;
  1940. tmp->pvt->pvt = i;
  1941. tmp->pvt->send_digit = iax_digit;
  1942. tmp->pvt->send_text = iax_sendtext;
  1943. tmp->pvt->send_image = iax_sendimage;
  1944. tmp->pvt->send_html = iax_sendhtml;
  1945. tmp->pvt->call = iax_call;
  1946. tmp->pvt->hangup = iax_hangup;
  1947. tmp->pvt->answer = iax_answer;
  1948. tmp->pvt->read = iax_read;
  1949. tmp->pvt->write = iax_write;
  1950. tmp->pvt->indicate = iax_indicate;
  1951. tmp->pvt->setoption = iax_setoption;
  1952. tmp->pvt->bridge = iax_bridge;
  1953. if (strlen(i->callerid))
  1954. tmp->callerid = strdup(i->callerid);
  1955. if (strlen(i->ani))
  1956. tmp->ani = strdup(i->ani);
  1957. if (strlen(i->language))
  1958. strncpy(tmp->language, i->language, sizeof(tmp->language)-1);
  1959. if (strlen(i->dnid))
  1960. tmp->dnid = strdup(i->dnid);
  1961. if (strlen(i->accountcode))
  1962. strncpy(tmp->accountcode, i->accountcode, sizeof(tmp->accountcode)-1);
  1963. if (i->amaflags)
  1964. tmp->amaflags = i->amaflags;
  1965. strncpy(tmp->context, i->context, sizeof(tmp->context)-1);
  1966. strncpy(tmp->exten, i->exten, sizeof(tmp->exten)-1);
  1967. tmp->adsicpe = i->peeradsicpe;
  1968. tmp->pvt->fixup = iax_fixup;
  1969. i->owner = tmp;
  1970. i->capability = capability;
  1971. ast_setstate(tmp, state);
  1972. ast_mutex_lock(&usecnt_lock);
  1973. usecnt++;
  1974. ast_mutex_unlock(&usecnt_lock);
  1975. ast_update_use_count();
  1976. if (state != AST_STATE_DOWN) {
  1977. if (ast_pbx_start(tmp)) {
  1978. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
  1979. ast_hangup(tmp);
  1980. tmp = NULL;
  1981. }
  1982. }
  1983. }
  1984. return tmp;
  1985. }
  1986. static unsigned int calc_timestamp(struct chan_iax_pvt *p, unsigned int ts)
  1987. {
  1988. struct timeval tv;
  1989. unsigned int ms;
  1990. if (!p->offset.tv_sec && !p->offset.tv_usec)
  1991. gettimeofday(&p->offset, NULL);
  1992. /* If the timestamp is specified, just send it as is */
  1993. if (ts)
  1994. return ts;
  1995. gettimeofday(&tv, NULL);
  1996. ms = (tv.tv_sec - p->offset.tv_sec) * 1000 + (tv.tv_usec - p->offset.tv_usec) / 1000;
  1997. /* We never send the same timestamp twice, so fudge a little if we must */
  1998. if (ms <= p->lastsent)
  1999. ms = p->lastsent + 1;
  2000. p->lastsent = ms;
  2001. return ms;
  2002. }
  2003. #ifdef BRIDGE_OPTIMIZATION
  2004. static unsigned int calc_fakestamp(struct chan_iax_pvt *p1, struct chan_iax_pvt *p2, unsigned int fakets)
  2005. {
  2006. int ms;
  2007. /* Receive from p1, send to p2 */
  2008. /* Setup rxcore if necessary on outgoing channel */
  2009. if (!p1->rxcore.tv_sec && !p1->rxcore.tv_usec)
  2010. gettimeofday(&p1->rxcore, NULL);
  2011. /* Setup txcore if necessary on outgoing channel */
  2012. if (!p2->offset.tv_sec && !p2->offset.tv_usec)
  2013. gettimeofday(&p2->offset, NULL);
  2014. /* Now, ts is the timestamp of the original packet in the orignal context.
  2015. Adding rxcore to it gives us when we would want the packet to be delivered normally.
  2016. Subtracting txcore of the outgoing channel gives us what we'd expect */
  2017. ms = (p1->rxcore.tv_sec - p2->offset.tv_sec) * 1000 + (p1->rxcore.tv_usec - p1->offset.tv_usec) / 1000;
  2018. fakets += ms;
  2019. if (fakets <= p2->lastsent)
  2020. fakets = p2->lastsent + 1;
  2021. p2->lastsent = fakets;
  2022. return fakets;
  2023. }
  2024. #endif
  2025. static unsigned int calc_rxstamp(struct chan_iax_pvt *p)
  2026. {
  2027. /* Returns where in "receive time" we are */
  2028. struct timeval tv;
  2029. unsigned int ms;
  2030. /* Setup rxcore if necessary */
  2031. if (!p->rxcore.tv_sec && !p->rxcore.tv_usec)
  2032. gettimeofday(&p->rxcore, NULL);
  2033. gettimeofday(&tv, NULL);
  2034. ms = (tv.tv_sec - p->rxcore.tv_sec) * 1000 + (tv.tv_usec - p->rxcore.tv_usec) / 1000;
  2035. return ms;
  2036. }
  2037. static int iax_send(struct chan_iax_pvt *pvt, struct ast_frame *f, unsigned int ts, int seqno, int now, int transfer, int final)
  2038. {
  2039. /* Queue a packet for delivery on a given private structure. Use "ts" for
  2040. timestamp, or calculate if ts is 0. Send immediately without retransmission
  2041. or delayed, with retransmission */
  2042. struct ast_iax_full_hdr *fh;
  2043. struct ast_iax_mini_hdr *mh;
  2044. struct ast_iax_frame *fr, fr2;
  2045. int res;
  2046. unsigned int lastsent;
  2047. /* Allocate an ast_iax_frame */
  2048. if (now)
  2049. fr = &fr2;
  2050. else
  2051. fr = ast_iax_frame_new(DIRECTION_OUTGRESS);
  2052. if (!fr) {
  2053. ast_log(LOG_WARNING, "Out of memory\n");
  2054. return -1;
  2055. }
  2056. if (!pvt) {
  2057. ast_log(LOG_WARNING, "No private structure for packet (%d)?\n", fr->callno);
  2058. if (!now)
  2059. ast_iax_frame_free(fr);
  2060. return -1;
  2061. }
  2062. /* Isolate our frame for transmission */
  2063. fr->f = ast_frdup(f);
  2064. if (!fr->f) {
  2065. ast_log(LOG_WARNING, "Out of memory\n");
  2066. if (!now)
  2067. ast_iax_frame_free(fr);
  2068. return -1;
  2069. }
  2070. if (fr->f->offset < sizeof(struct ast_iax_full_hdr)) {
  2071. ast_log(LOG_WARNING, "Packet from '%s' not friendly\n", fr->f->src);
  2072. free(fr);
  2073. return -1;
  2074. }
  2075. lastsent = pvt->lastsent;
  2076. fr->ts = calc_timestamp(pvt, ts);
  2077. if (!fr->ts) {
  2078. ast_log(LOG_WARNING, "timestamp is 0?\n");
  2079. if (!now)
  2080. ast_iax_frame_free(fr);
  2081. return -1;
  2082. }
  2083. fr->callno = pvt->callno;
  2084. fr->transfer = transfer;
  2085. fr->final = final;
  2086. if (((fr->ts & 0xFFFF0000L) != (lastsent & 0xFFFF0000L))
  2087. /* High two bits of timestamp differ */ ||
  2088. (fr->f->frametype != AST_FRAME_VOICE)
  2089. /* or not a voice frame */ ||
  2090. (fr->f->subclass != pvt->svoiceformat)
  2091. /* or new voice format */ ) {
  2092. /* We need a full frame */
  2093. if (seqno > -1)
  2094. fr->seqno = seqno;
  2095. else
  2096. fr->seqno = pvt->oseqno++;
  2097. fh = (struct ast_iax_full_hdr *)(fr->f->data - sizeof(struct ast_iax_full_hdr));
  2098. fh->callno = htons(fr->callno | AST_FLAG_FULL);
  2099. fh->ts = htonl(fr->ts);
  2100. fh->seqno = htons(fr->seqno);
  2101. fh->type = fr->f->frametype & 0xFF;
  2102. fh->csub = compress_subclass(fr->f->subclass);
  2103. if (transfer) {
  2104. fh->dcallno = htons(pvt->transfercallno);
  2105. } else
  2106. fh->dcallno = htons(pvt->peercallno);
  2107. fr->datalen = fr->f->datalen + sizeof(struct ast_iax_full_hdr);
  2108. fr->data = fh;
  2109. fr->retries = 0;
  2110. /* Retry after 2x the ping time has passed */
  2111. fr->retrytime = pvt->pingtime * 2;
  2112. if (fr->retrytime < MIN_RETRY_TIME)
  2113. fr->retrytime = MIN_RETRY_TIME;
  2114. if (fr->retrytime > MAX_RETRY_TIME)
  2115. fr->retrytime = MAX_RETRY_TIME;
  2116. /* Acks' don't get retried */
  2117. if ((f->frametype == AST_FRAME_IAX) && (f->subclass == AST_IAX_COMMAND_ACK))
  2118. fr->retries = -1;
  2119. if (f->frametype == AST_FRAME_VOICE) {
  2120. pvt->svoiceformat = f->subclass;
  2121. }
  2122. if (now) {
  2123. res = send_packet(fr);
  2124. ast_frfree(fr->f);
  2125. } else
  2126. res = iax_transmit(fr);
  2127. } else {
  2128. /* Mini-frames have no sequence number */
  2129. fr->seqno = -1;
  2130. /* Mini frame will do */
  2131. mh = (struct ast_iax_mini_hdr *)(fr->f->data - sizeof(struct ast_iax_mini_hdr));
  2132. mh->callno = htons(fr->callno);
  2133. mh->ts = htons(fr->ts & 0xFFFF);
  2134. fr->datalen = fr->f->datalen + sizeof(struct ast_iax_mini_hdr);
  2135. fr->data = mh;
  2136. fr->retries = -1;
  2137. if (now) {
  2138. res = send_packet(fr);
  2139. ast_frfree(fr->f);
  2140. } else
  2141. res = iax_transmit(fr);
  2142. }
  2143. return res;
  2144. }
  2145. static int iax_show_users(int fd, int argc, char *argv[])
  2146. {
  2147. #define FORMAT "%-15.15s %-15.15s %-15.15s %-15.15s %-5.5s\n"
  2148. struct iax_user *user;
  2149. if (argc != 3)
  2150. return RESULT_SHOWUSAGE;
  2151. ast_mutex_lock(&userl.lock);
  2152. ast_cli(fd, FORMAT, "Username", "Secret", "Authen", "Def.Context", "A/C");
  2153. for(user=userl.users;user;user=user->next) {
  2154. ast_cli(fd, FORMAT, user->name, user->secret, user->methods,
  2155. user->contexts ? user->contexts->context : context,
  2156. user->ha ? "Yes" : "No");
  2157. }
  2158. ast_mutex_unlock(&userl.lock);
  2159. return RESULT_SUCCESS;
  2160. #undef FORMAT
  2161. }
  2162. static int iax_show_peers(int fd, int argc, char *argv[])
  2163. {
  2164. #define FORMAT2 "%-15.15s %-15.15s %s %-15.15s %-8s %-10s\n"
  2165. #define FORMAT "%-15.15s %-15.15s %s %-15.15s %-8d %-10s\n"
  2166. struct iax_peer *peer;
  2167. char name[256] = "";
  2168. char iabuf[INET_ADDRSTRLEN];
  2169. if (argc != 3)
  2170. return RESULT_SHOWUSAGE;
  2171. ast_mutex_lock(&peerl.lock);
  2172. ast_cli(fd, FORMAT2, "Name/Username", "Host", " ", "Mask", "Port", "Status");
  2173. for (peer = peerl.peers;peer;peer = peer->next) {
  2174. char nm[20];
  2175. char status[20] = "";
  2176. if (strlen(peer->username))
  2177. snprintf(name, sizeof(name), "%s/%s", peer->name, peer->username);
  2178. else
  2179. strncpy(name, peer->name, sizeof(name) - 1);
  2180. if (peer->maxms) {
  2181. if (peer->lastms < 0)
  2182. strncpy(status, "UNREACHABLE", sizeof(status) - 1);
  2183. else if (peer->lastms > peer->maxms)
  2184. snprintf(status, sizeof(status), "LAGGED (%d ms)", peer->lastms);
  2185. else if (peer->lastms)
  2186. snprintf(status, sizeof(status), "OK (%d ms)", peer->lastms);
  2187. else
  2188. strncpy(status, "UNKNOWN", sizeof(status) - 1);
  2189. } else
  2190. strncpy(status, "Unmonitored", sizeof(status) - 1);
  2191. strncpy(nm, ast_inet_ntoa(iabuf, sizeof(iabuf), peer->mask), sizeof(nm)-1);
  2192. ast_cli(fd, FORMAT, name,
  2193. peer->addr.sin_addr.s_addr ? ast_inet_ntoa(iabuf, sizeof(iabuf), peer->addr.sin_addr) : "(Unspecified)",
  2194. peer->dynamic ? "(D)" : "(S)",
  2195. nm,
  2196. ntohs(peer->addr.sin_port), status);
  2197. }
  2198. ast_mutex_unlock(&peerl.lock);
  2199. return RESULT_SUCCESS;
  2200. #undef FORMAT
  2201. #undef FORMAT2
  2202. }
  2203. /* JDG: callback to display iax peers in manager */
  2204. static int manager_iax_show_peers( struct mansession *s, struct message *m )
  2205. {
  2206. char *a[] = { "iax", "show", "users" };
  2207. int ret;
  2208. ret = iax_show_peers( s->fd, 3, a );
  2209. ast_cli( s->fd, "\r\n" );
  2210. return ret;
  2211. } /* /JDG */
  2212. static char *regstate2str(int regstate)
  2213. {
  2214. switch(regstate) {
  2215. case REG_STATE_UNREGISTERED:
  2216. return "Unregistered";
  2217. case REG_STATE_REGSENT:
  2218. return "Request Sent";
  2219. case REG_STATE_AUTHSENT:
  2220. return "Auth. Sent";
  2221. case REG_STATE_REGISTERED:
  2222. return "Registered";
  2223. case REG_STATE_REJECTED:
  2224. return "Rejected";
  2225. case REG_STATE_TIMEOUT:
  2226. return "Timeout";
  2227. case REG_STATE_NOAUTH:
  2228. return "No Authentication";
  2229. default:
  2230. return "Unknown";
  2231. }
  2232. }
  2233. static int iax_show_registry(int fd, int argc, char *argv[])
  2234. {
  2235. #define FORMAT2 "%-20.20s %-10.10s %-20.20s %8.8s %s\n"
  2236. #define FORMAT "%-20.20s %-10.10s %-20.20s %8d %s\n"
  2237. struct iax_registry *reg;
  2238. char host[80];
  2239. char perceived[80] = "";
  2240. char iabuf[INET_ADDRSTRLEN];
  2241. if (argc != 3)
  2242. return RESULT_SHOWUSAGE;
  2243. ast_mutex_lock(&peerl.lock);
  2244. ast_cli(fd, FORMAT2, "Host", "Username", "Perceived", "Refresh", "State");
  2245. for (reg = registrations;reg;reg = reg->next) {
  2246. snprintf(host, sizeof(host), "%s:%d", ast_inet_ntoa(iabuf, sizeof(iabuf), reg->addr.sin_addr), ntohs(reg->addr.sin_port));
  2247. if (reg->us.sin_addr.s_addr)
  2248. snprintf(perceived, sizeof(perceived), "%s:%d", ast_inet_ntoa(iabuf, sizeof(iabuf), reg->us.sin_addr), ntohs(reg->us.sin_port));
  2249. else
  2250. strncpy(perceived, "<Unregistered>", sizeof(perceived) - 1);
  2251. ast_cli(fd, FORMAT, host,
  2252. reg->username, perceived, reg->refresh, regstate2str(reg->regstate));
  2253. }
  2254. ast_mutex_unlock(&peerl.lock);
  2255. return RESULT_SUCCESS;
  2256. #undef FORMAT
  2257. #undef FORMAT2
  2258. }
  2259. static int iax_show_channels(int fd, int argc, char *argv[])
  2260. {
  2261. #define FORMAT2 "%-15.15s %-10.10s %-11.11s %-11.11s %-7.7s %-6.6s %s\n"
  2262. #define FORMAT "%-15.15s %-10.10s %5.5d/%5.5d %5.5d/%5.5d %-5.5dms %-4.4dms %-6.6s\n"
  2263. int x;
  2264. int numchans = 0;
  2265. char iabuf[INET_ADDRSTRLEN];
  2266. if (argc != 3)
  2267. return RESULT_SHOWUSAGE;
  2268. ast_cli(fd, FORMAT2, "Peer", "Username", "ID (Lo/Rem)", "Seq (Tx/Rx)", "Lag", "Jitter", "Format");
  2269. for (x=0;x<AST_IAX_MAX_CALLS;x++) {
  2270. ast_mutex_lock(&iaxsl[x]);
  2271. if (iaxs[x]) {
  2272. ast_cli(fd, FORMAT, ast_inet_ntoa(iabuf, sizeof(iabuf), iaxs[x]->addr.sin_addr),
  2273. strlen(iaxs[x]->username) ? iaxs[x]->username : "(None)",
  2274. iaxs[x]->callno, iaxs[x]->peercallno,
  2275. iaxs[x]->oseqno, iaxs[x]->iseqno,
  2276. iaxs[x]->lag,
  2277. iaxs[x]->jitter,
  2278. ast_getformatname(iaxs[x]->voiceformat) );
  2279. numchans++;
  2280. }
  2281. ast_mutex_unlock(&iaxsl[x]);
  2282. }
  2283. ast_cli(fd, "%d active IAX channel(s)\n", numchans);
  2284. return RESULT_SUCCESS;
  2285. #undef FORMAT
  2286. #undef FORMAT2
  2287. }
  2288. static int iax_do_debug(int fd, int argc, char *argv[])
  2289. {
  2290. if (argc != 2)
  2291. return RESULT_SHOWUSAGE;
  2292. iaxdebug = 1;
  2293. ast_cli(fd, "IAX Debugging Enabled\n");
  2294. return RESULT_SUCCESS;
  2295. }
  2296. static int iax_no_debug(int fd, int argc, char *argv[])
  2297. {
  2298. if (argc != 3)
  2299. return RESULT_SHOWUSAGE;
  2300. iaxdebug = 0;
  2301. ast_cli(fd, "IAX Debugging Disabled\n");
  2302. return RESULT_SUCCESS;
  2303. }
  2304. static char show_users_usage[] =
  2305. "Usage: iax1 show users\n"
  2306. " Lists all users known to the IAX (Inter-Asterisk eXchange) subsystem.\n";
  2307. static char show_channels_usage[] =
  2308. "Usage: iax1 show channels\n"
  2309. " Lists all currently active IAX channels.\n";
  2310. static char show_peers_usage[] =
  2311. "Usage: iax1 show peers\n"
  2312. " Lists all known IAX peers.\n";
  2313. static char show_reg_usage[] =
  2314. "Usage: iax1 show registry\n"
  2315. " Lists all registration requests and status.\n";
  2316. #ifdef DEBUG_SUPPORT
  2317. static char debug_usage[] =
  2318. "Usage: iax1 debug\n"
  2319. " Enables dumping of IAX packets for debugging purposes\n";
  2320. static char no_debug_usage[] =
  2321. "Usage: iax1 no debug\n"
  2322. " Disables dumping of IAX packets for debugging purposes\n";
  2323. #endif
  2324. static struct ast_cli_entry cli_show_users =
  2325. { { "iax1", "show", "users", NULL }, iax_show_users, "Show defined IAX users", show_users_usage };
  2326. static struct ast_cli_entry cli_show_channels =
  2327. { { "iax1", "show", "channels", NULL }, iax_show_channels, "Show active IAX channels", show_channels_usage };
  2328. static struct ast_cli_entry cli_show_peers =
  2329. { { "iax1", "show", "peers", NULL }, iax_show_peers, "Show defined IAX peers", show_peers_usage };
  2330. static struct ast_cli_entry cli_show_registry =
  2331. { { "iax1", "show", "registry", NULL }, iax_show_registry, "Show IAX registration status", show_reg_usage };
  2332. static struct ast_cli_entry cli_debug =
  2333. { { "iax1", "debug", NULL }, iax_do_debug, "Enable IAX debugging", debug_usage };
  2334. static struct ast_cli_entry cli_no_debug =
  2335. { { "iax1", "no", "debug", NULL }, iax_no_debug, "Disable IAX debugging", no_debug_usage };
  2336. static int iax_write(struct ast_channel *c, struct ast_frame *f)
  2337. {
  2338. struct chan_iax_pvt *i = c->pvt->pvt;
  2339. if (!i)
  2340. return -1;
  2341. /* If there's an outstanding error, return failure now */
  2342. if (i->error) {
  2343. ast_log(LOG_DEBUG, "Write error: %s\n", strerror(errno));
  2344. return -1;
  2345. }
  2346. /* If it's already gone, just return */
  2347. if (i->alreadygone)
  2348. return 0;
  2349. /* Don't waste bandwidth sending null frames */
  2350. if (f->frametype == AST_FRAME_NULL)
  2351. return 0;
  2352. /* If we're quelching voice, don't bother sending it */
  2353. if ((f->frametype == AST_FRAME_VOICE) && i->quelch)
  2354. return 0;
  2355. /* Simple, just queue for transmission */
  2356. return iax_send(i, f, 0, -1, 0, 0, 0);
  2357. }
  2358. static int __send_command(struct chan_iax_pvt *i, char type, int command, unsigned int ts, char *data, int datalen, int seqno,
  2359. int now, int transfer, int final)
  2360. {
  2361. struct ast_frame f;
  2362. f.frametype = type;
  2363. f.subclass = command;
  2364. f.datalen = datalen;
  2365. f.samples = 0;
  2366. f.mallocd = 0;
  2367. f.offset = 0;
  2368. f.src = __FUNCTION__;
  2369. f.data = data;
  2370. return iax_send(i, &f, ts, seqno, now, transfer, final);
  2371. }
  2372. static int send_command(struct chan_iax_pvt *i, char type, int command, unsigned int ts, char *data, int datalen, int seqno)
  2373. {
  2374. return __send_command(i, type, command, ts, data, datalen, seqno, 0, 0, 0);
  2375. }
  2376. #ifdef BRIDGE_OPTIMIZATION
  2377. static int forward_command(struct chan_iax_pvt *i, char type, int command, unsigned int ts, char *data, int datalen, int seqno)
  2378. {
  2379. return __send_command(iaxs[i->bridgecallno], type, command, ts, data, datalen, seqno, 0, 0, 0);
  2380. }
  2381. #endif
  2382. static int send_command_final(struct chan_iax_pvt *i, char type, int command, unsigned int ts, char *data, int datalen, int seqno)
  2383. {
  2384. /* It is assumed that the callno has already been locked */
  2385. iax_predestroy_nolock(i->callno);
  2386. return __send_command(i, type, command, ts, data, datalen, seqno, 0, 0, 1);
  2387. }
  2388. static int send_command_immediate(struct chan_iax_pvt *i, char type, int command, unsigned int ts, char *data, int datalen, int seqno)
  2389. {
  2390. return __send_command(i, type, command, ts, data, datalen, seqno, 1, 0, 0);
  2391. }
  2392. static int send_command_transfer(struct chan_iax_pvt *i, char type, int command, unsigned int ts, char *data, int datalen)
  2393. {
  2394. return __send_command(i, type, command, ts, data, datalen, 0, 0, 1, 0);
  2395. }
  2396. static int apply_context(struct iax_context *con, char *context)
  2397. {
  2398. while(con) {
  2399. if (!strcmp(con->context, context))
  2400. return -1;
  2401. con = con->next;
  2402. }
  2403. return 0;
  2404. }
  2405. static int iax_getformats(int callno, char *orequest)
  2406. {
  2407. char *var, *value;
  2408. char request[256];
  2409. char *stringp=NULL;
  2410. strncpy(request, orequest, sizeof(request)-1);
  2411. stringp=request;
  2412. var = strsep(&stringp, ";");
  2413. while(var) {
  2414. value = strchr(var, '=');
  2415. if (value) {
  2416. *value='\0';
  2417. value++;
  2418. if (!strcmp(var, "formats")) {
  2419. iaxs[callno]->peerformat = atoi(value);
  2420. } else
  2421. ast_log(LOG_WARNING, "Unknown variable '%s' with value '%s'\n", var, value);
  2422. }
  2423. var = strsep(&stringp, ";");
  2424. }
  2425. return 0;
  2426. }
  2427. static int check_access(int callno, struct sockaddr_in *sin, char *orequest, int requestl)
  2428. {
  2429. /* Start pessimistic */
  2430. int res = -1;
  2431. int version = 1;
  2432. char *var, *value;
  2433. struct iax_user *user;
  2434. char request[256];
  2435. char iabuf[INET_ADDRSTRLEN];
  2436. int gotcapability=0;
  2437. char *stringp=NULL;
  2438. strncpy(request, orequest, sizeof(request)-1);
  2439. if (!iaxs[callno])
  2440. return res;
  2441. stringp=request;
  2442. var = strsep(&stringp, ";");
  2443. while(var) {
  2444. value = strchr(var, '=');
  2445. if (value) {
  2446. *value='\0';
  2447. value++;
  2448. if (!strcmp(var, "exten"))
  2449. strncpy(iaxs[callno]->exten, value, sizeof(iaxs[callno]->exten)-1);
  2450. else if (!strcmp(var, "callerid"))
  2451. strncpy(iaxs[callno]->callerid, value, sizeof(iaxs[callno]->callerid)-1);
  2452. else if (!strcmp(var, "ani"))
  2453. strncpy(iaxs[callno]->ani, value, sizeof(iaxs[callno]->ani) - 1);
  2454. else if (!strcmp(var, "dnid"))
  2455. strncpy(iaxs[callno]->dnid, value, sizeof(iaxs[callno]->dnid)-1);
  2456. else if (!strcmp(var, "context"))
  2457. strncpy(iaxs[callno]->context, value, sizeof(iaxs[callno]->context)-1);
  2458. else if (!strcmp(var, "language"))
  2459. strncpy(iaxs[callno]->language, value, sizeof(iaxs[callno]->language)-1);
  2460. else if (!strcmp(var, "username"))
  2461. strncpy(iaxs[callno]->username, value, sizeof(iaxs[callno]->username)-1);
  2462. else if (!strcmp(var, "formats"))
  2463. iaxs[callno]->peerformat = atoi(value);
  2464. else if (!strcmp(var, "adsicpe"))
  2465. iaxs[callno]->peeradsicpe = atoi(value);
  2466. else if (!strcmp(var, "capability")) {
  2467. gotcapability = 1;
  2468. iaxs[callno]->peercapability = atoi(value);
  2469. } else if (!strcmp(var, "version"))
  2470. version = atoi(value);
  2471. else
  2472. ast_log(LOG_WARNING, "Unknown variable '%s' with value '%s'\n", var, value);
  2473. }
  2474. var = strsep(&stringp, ";");
  2475. }
  2476. if (!gotcapability)
  2477. iaxs[callno]->peercapability = iaxs[callno]->peerformat;
  2478. if (version > AST_IAX_PROTO_VERSION) {
  2479. ast_log(LOG_WARNING, "Peer '%s' has too new a protocol version (%d) for me\n",
  2480. ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), version);
  2481. return res;
  2482. }
  2483. ast_mutex_lock(&userl.lock);
  2484. /* Search the userlist for a compatible entry, and fill in the rest */
  2485. user = userl.users;
  2486. while(user) {
  2487. if ((!strlen(iaxs[callno]->username) || /* No username specified */
  2488. !strcmp(iaxs[callno]->username, user->name)) /* Or this username specified */
  2489. && ast_apply_ha(user->ha, sin) /* Access is permitted from this IP */
  2490. && (!strlen(iaxs[callno]->context) || /* No context specified */
  2491. apply_context(user->contexts, iaxs[callno]->context))) { /* Context is permitted */
  2492. break;
  2493. }
  2494. user = user->next;
  2495. }
  2496. #ifdef MYSQL_FRIENDS
  2497. if (!user && mysql && strlen(iaxs[callno]->username) && (strlen(iaxs[callno]->username) < 128)) {
  2498. user = mysql_user(iaxs[callno]->username);
  2499. if (user && strlen(iaxs[callno]->context) && /* No context specified */
  2500. !apply_context(user->contexts, iaxs[callno]->context)) { /* Context is permitted */
  2501. if (user->contexts)
  2502. free(user->contexts);
  2503. free(user);
  2504. user = NULL;
  2505. }
  2506. }
  2507. #endif
  2508. ast_mutex_unlock(&userl.lock);
  2509. if (user) {
  2510. /* We found our match (use the first) */
  2511. /* Store the requested username if not specified */
  2512. if (!strlen(iaxs[callno]->username))
  2513. strncpy(iaxs[callno]->username, user->name, sizeof(iaxs[callno]->username)-1);
  2514. /* And use the default context */
  2515. if (!strlen(iaxs[callno]->context)) {
  2516. if (user->contexts)
  2517. strncpy(iaxs[callno]->context, user->contexts->context, sizeof(iaxs[callno]->context)-1);
  2518. else
  2519. strncpy(iaxs[callno]->context, context, sizeof(iaxs[callno]->context)-1);
  2520. }
  2521. /* Copy the secret */
  2522. strncpy(iaxs[callno]->secret, user->secret, sizeof(iaxs[callno]->secret)-1);
  2523. /* And any input keys */
  2524. strncpy(iaxs[callno]->inkeys, user->inkeys, sizeof(iaxs[callno]->inkeys) - 1);
  2525. /* And the permitted authentication methods */
  2526. strncpy(iaxs[callno]->methods, user->methods, sizeof(iaxs[callno]->methods)-1);
  2527. /* If they have callerid, override the given caller id. Always store the ANI */
  2528. if (strlen(iaxs[callno]->callerid)) {
  2529. if (user->hascallerid)
  2530. strncpy(iaxs[callno]->callerid, user->callerid, sizeof(iaxs[callno]->callerid)-1);
  2531. strncpy(iaxs[callno]->ani, user->callerid, sizeof(iaxs[callno]->ani)-1);
  2532. }
  2533. if (strlen(user->accountcode))
  2534. strncpy(iaxs[callno]->accountcode, user->accountcode, sizeof(iaxs[callno]->accountcode)-1);
  2535. if (user->amaflags)
  2536. iaxs[callno]->amaflags = user->amaflags;
  2537. res = 0;
  2538. }
  2539. return res;
  2540. }
  2541. static int raw_hangup(struct sockaddr_in *sin, short src, short dst)
  2542. {
  2543. struct ast_iax_full_hdr fh;
  2544. char iabuf[INET_ADDRSTRLEN];
  2545. fh.callno = htons(src | AST_FLAG_FULL);
  2546. fh.dcallno = htons(dst);
  2547. fh.ts = 0;
  2548. fh.seqno = 0;
  2549. fh.type = AST_FRAME_IAX;
  2550. fh.csub = compress_subclass(AST_IAX_COMMAND_INVAL);
  2551. #if 0
  2552. if (option_debug)
  2553. #endif
  2554. ast_log(LOG_DEBUG, "Raw Hangup %s:%d, src=%d, dst=%d\n",
  2555. ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), ntohs(sin->sin_port), src, dst);
  2556. return sendto(netsocket, &fh, sizeof(fh), 0, (struct sockaddr *)sin, sizeof(*sin));
  2557. }
  2558. static int authenticate_request(struct chan_iax_pvt *p)
  2559. {
  2560. char requeststr[256] = "";
  2561. MYSNPRINTF "methods=%s;", p->methods);
  2562. if (strstr(p->methods, "md5") || strstr(p->methods, "rsa")) {
  2563. /* Build the challenge */
  2564. snprintf(p->challenge, sizeof(p->challenge), "%d", rand());
  2565. MYSNPRINTF "challenge=%s;", p->challenge);
  2566. }
  2567. MYSNPRINTF "username=%s;", p->username);
  2568. if (strlen(requeststr))
  2569. requeststr[strlen(requeststr) - 1] = '\0';
  2570. return send_command(p, AST_FRAME_IAX, AST_IAX_COMMAND_AUTHREQ, 0, requeststr, strlen(requeststr) + 1, -1);
  2571. }
  2572. static int authenticate_verify(struct chan_iax_pvt *p, char *orequest)
  2573. {
  2574. char requeststr[256] = "";
  2575. char *var, *value, request[256];
  2576. char md5secret[256] = "";
  2577. char secret[256] = "";
  2578. char rsasecret[256] = "";
  2579. int res = -1;
  2580. int x;
  2581. char *stringp=NULL;
  2582. if (!(p->state & IAX_STATE_AUTHENTICATED))
  2583. return res;
  2584. strncpy(request, orequest, sizeof(request)-1);
  2585. stringp=request;
  2586. var = strsep(&stringp, ";");
  2587. while(var) {
  2588. value = strchr(var, '=');
  2589. if (value) {
  2590. *value='\0';
  2591. value++;
  2592. if (!strcmp(var, "secret"))
  2593. strncpy(secret, value, sizeof(secret)-1);
  2594. else if (!strcmp(var, "md5secret"))
  2595. strncpy(md5secret, value, sizeof(md5secret)-1);
  2596. else if (!strcmp(var, "rsasecret"))
  2597. strncpy(rsasecret, value, sizeof(rsasecret)-1);
  2598. else
  2599. ast_log(LOG_WARNING, "Unknown variable '%s' with value '%s'\n", var, value);
  2600. }
  2601. var = strsep(&stringp, ";");
  2602. }
  2603. if (strstr(p->methods, "rsa") && strlen(rsasecret) && strlen(p->inkeys)) {
  2604. struct ast_key *key;
  2605. char *keyn;
  2606. char tmpkey[256] = "";
  2607. char *stringp=NULL;
  2608. strncpy(tmpkey, p->inkeys, sizeof(tmpkey) - 1);
  2609. stringp=tmpkey;
  2610. keyn = strsep(&stringp, ":");
  2611. while(keyn) {
  2612. key = ast_key_get(keyn, AST_KEY_PUBLIC);
  2613. if (key && !ast_check_signature(key, p->challenge, rsasecret)) {
  2614. res = 0;
  2615. break;
  2616. } else if (!key)
  2617. ast_log(LOG_WARNING, "requested inkey '%s' for RSA authentication does not exist\n", keyn);
  2618. keyn = strsep(&stringp, ":");
  2619. }
  2620. } else if (strstr(p->methods, "md5")) {
  2621. struct MD5Context md5;
  2622. unsigned char digest[16];
  2623. MD5Init(&md5);
  2624. MD5Update(&md5, p->challenge, strlen(p->challenge));
  2625. MD5Update(&md5, p->secret, strlen(p->secret));
  2626. MD5Final(digest, &md5);
  2627. /* If they support md5, authenticate with it. */
  2628. for (x=0;x<16;x++)
  2629. MYSNPRINTF "%2.2x", digest[x]);
  2630. if (!strcasecmp(requeststr, md5secret))
  2631. res = 0;
  2632. } else if (strstr(p->methods, "plaintext")) {
  2633. if (!strcmp(secret, p->secret))
  2634. res = 0;
  2635. }
  2636. return res;
  2637. }
  2638. static int register_verify(int callno, struct sockaddr_in *sin, char *orequest)
  2639. {
  2640. char request[256];
  2641. char requeststr[256] = "";
  2642. char peer[256] = "";
  2643. char md5secret[256] = "";
  2644. char rsasecret[256] = "";
  2645. char secret[256] = "";
  2646. char iabuf[INET_ADDRSTRLEN];
  2647. struct iax_peer *p;
  2648. struct ast_key *key;
  2649. char *var;
  2650. char *value;
  2651. char *keyn;
  2652. int x;
  2653. int expire = 0;
  2654. char *stringp=NULL;
  2655. iaxs[callno]->state &= ~IAX_STATE_AUTHENTICATED;
  2656. iaxs[callno]->peer[0] = '\0';
  2657. if (!orequest)
  2658. return -1;
  2659. strncpy(request, orequest, sizeof(request)-1);
  2660. stringp=request;
  2661. var = strsep(&stringp, ";");
  2662. while(var) {
  2663. value = strchr(var, '=');
  2664. if (value) {
  2665. *value='\0';
  2666. value++;
  2667. if (!strcmp(var, "peer"))
  2668. strncpy(peer, value, sizeof(peer)-1);
  2669. else if (!strcmp(var, "md5secret"))
  2670. strncpy(md5secret, value, sizeof(md5secret)-1);
  2671. else if (!strcmp(var, "rsasecret"))
  2672. strncpy(rsasecret, value, sizeof(rsasecret)-1);
  2673. else if (!strcmp(var, "secret"))
  2674. strncpy(secret, value, sizeof(secret)-1);
  2675. else if (!strcmp(var, "refresh"))
  2676. expire = atoi(value);
  2677. else
  2678. ast_log(LOG_WARNING, "Unknown variable '%s' with value '%s'\n", var, value);
  2679. }
  2680. var = strsep(&stringp, ";");
  2681. }
  2682. if (!strlen(peer)) {
  2683. ast_log(LOG_NOTICE, "Empty registration from %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr));
  2684. return -1;
  2685. }
  2686. for (p = peerl.peers; p ; p = p->next)
  2687. if (!strcasecmp(p->name, peer))
  2688. break;
  2689. #ifdef MYSQL_FRIENDS
  2690. if (!p)
  2691. p = mysql_peer(peer);
  2692. #endif
  2693. if (!p) {
  2694. ast_log(LOG_NOTICE, "No registration for peer '%s' (from %s)\n", peer, ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr));
  2695. return -1;
  2696. }
  2697. if (!p->dynamic) {
  2698. ast_log(LOG_NOTICE, "Peer '%s' is not dynamic (from %s)\n", peer, ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr));
  2699. if (p->delme)
  2700. free(p);
  2701. return -1;
  2702. }
  2703. if (!ast_apply_ha(p->ha, sin)) {
  2704. ast_log(LOG_NOTICE, "Host %s denied access to register peer '%s'\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), p->name);
  2705. if (p->delme)
  2706. free(p);
  2707. return -1;
  2708. }
  2709. strncpy(iaxs[callno]->secret, p->secret, sizeof(iaxs[callno]->secret)-1);
  2710. strncpy(iaxs[callno]->inkeys, p->inkeys, sizeof(iaxs[callno]->inkeys)-1);
  2711. /* Check secret against what we have on file */
  2712. if (strlen(rsasecret) && strstr(p->methods, "rsa") && strlen(iaxs[callno]->challenge)) {
  2713. if (strlen(p->inkeys)) {
  2714. char tmpkeys[256];
  2715. char *stringp=NULL;
  2716. strncpy(tmpkeys, p->inkeys, sizeof(tmpkeys) - 1);
  2717. stringp=tmpkeys;
  2718. keyn = strsep(&stringp, ":");
  2719. while(keyn) {
  2720. key = ast_key_get(keyn, AST_KEY_PUBLIC);
  2721. if (key && !ast_check_signature(key, iaxs[callno]->challenge, rsasecret)) {
  2722. iaxs[callno]->state |= IAX_STATE_AUTHENTICATED;
  2723. break;
  2724. } else if (!key)
  2725. ast_log(LOG_WARNING, "requested inkey '%s' does not exist\n", keyn);
  2726. keyn = strsep(&stringp, ":");
  2727. }
  2728. if (!keyn) {
  2729. ast_log(LOG_NOTICE, "Host %s failed RSA authentication with inkeys '%s'\n", peer, p->inkeys);
  2730. if (p->delme)
  2731. free(p);
  2732. return -1;
  2733. }
  2734. } else {
  2735. ast_log(LOG_NOTICE, "Host '%s' trying to do RSA authentication, but we have no inkeys\n", peer);
  2736. if (p->delme)
  2737. free(p);
  2738. return -1;
  2739. }
  2740. } else if (strlen(secret) && strstr(p->methods, "plaintext")) {
  2741. /* They've provided a plain text password and we support that */
  2742. if (strcmp(secret, p->secret)) {
  2743. ast_log(LOG_NOTICE, "Host %s did not provide proper plaintext password for '%s'\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), p->name);
  2744. if (p->delme)
  2745. free(p);
  2746. return -1;
  2747. } else
  2748. iaxs[callno]->state |= IAX_STATE_AUTHENTICATED;
  2749. } else if (strlen(md5secret) && strstr(p->methods, "md5") && strlen(iaxs[callno]->challenge)) {
  2750. struct MD5Context md5;
  2751. unsigned char digest[16];
  2752. MD5Init(&md5);
  2753. MD5Update(&md5, iaxs[callno]->challenge, strlen(iaxs[callno]->challenge));
  2754. MD5Update(&md5, p->secret, strlen(p->secret));
  2755. MD5Final(digest, &md5);
  2756. for (x=0;x<16;x++)
  2757. MYSNPRINTF "%2.2x", digest[x]);
  2758. if (strcasecmp(requeststr, md5secret)) {
  2759. ast_log(LOG_NOTICE, "Host %s failed MD5 authentication for '%s' (%s != %s)\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), p->name, requeststr, md5secret);
  2760. if (p->delme)
  2761. free(p);
  2762. return -1;
  2763. } else
  2764. iaxs[callno]->state |= IAX_STATE_AUTHENTICATED;
  2765. } else if (strlen(md5secret) || strlen(secret)) {
  2766. ast_log(LOG_NOTICE, "Inappropriate authentication received\n");
  2767. if (p->delme)
  2768. free(p);
  2769. return -1;
  2770. }
  2771. strncpy(iaxs[callno]->peer, peer, sizeof(iaxs[callno]->peer)-1);
  2772. /* Choose lowest expirey number */
  2773. if (expire && (expire < iaxs[callno]->expirey))
  2774. iaxs[callno]->expirey = expire;
  2775. if (p->delme)
  2776. free(p);
  2777. return 0;
  2778. }
  2779. static int authenticate(char *challenge, char *secret, char *keyn, char *methods, char *requeststr, int reqsize, struct sockaddr_in *sin)
  2780. {
  2781. int res = -1;
  2782. int x;
  2783. char iabuf[INET_ADDRSTRLEN];
  2784. if (keyn && strlen(keyn)) {
  2785. if (!strstr(methods, "rsa")) {
  2786. if (!secret || !strlen(secret))
  2787. ast_log(LOG_NOTICE, "Asked to authenticate to %s with an RSA key, but they don't allow RSA authentication\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr));
  2788. } else if (!strlen(challenge)) {
  2789. ast_log(LOG_NOTICE, "No challenge provided for RSA authentication to %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr));
  2790. } else {
  2791. char sig[256];
  2792. struct ast_key *key;
  2793. key = ast_key_get(keyn, AST_KEY_PRIVATE);
  2794. if (!key) {
  2795. ast_log(LOG_NOTICE, "Unable to find private key '%s'\n", keyn);
  2796. } else {
  2797. if (ast_sign(key, challenge, sig)) {
  2798. ast_log(LOG_NOTICE, "Unable to sign challenge withy key\n");
  2799. res = -1;
  2800. } else {
  2801. MYSNPRINTF2 "rsasecret=%s;", sig);
  2802. res = 0;
  2803. }
  2804. }
  2805. }
  2806. }
  2807. /* Fall back */
  2808. if (res && secret && strlen(secret)) {
  2809. if (strstr(methods, "md5") && strlen(challenge)) {
  2810. struct MD5Context md5;
  2811. unsigned char digest[16];
  2812. MD5Init(&md5);
  2813. MD5Update(&md5, challenge, strlen(challenge));
  2814. MD5Update(&md5, secret, strlen(secret));
  2815. MD5Final(digest, &md5);
  2816. /* If they support md5, authenticate with it. */
  2817. MYSNPRINTF2 "md5secret=");
  2818. for (x=0;x<16;x++)
  2819. MYSNPRINTF2 "%2.2x", digest[x]);
  2820. MYSNPRINTF2 ";");
  2821. res = 0;
  2822. } else if (strstr(methods, "plaintext")) {
  2823. MYSNPRINTF2 "secret=%s;", secret);
  2824. res = 0;
  2825. } else
  2826. ast_log(LOG_NOTICE, "No way to send secret to peer '%s' (their methods: %s)\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), methods);
  2827. }
  2828. return res;
  2829. }
  2830. static int authenticate_reply(struct chan_iax_pvt *p, struct sockaddr_in *sin, char *orequest, char *override, char *okey)
  2831. {
  2832. struct iax_peer *peer;
  2833. /* Start pessimistic */
  2834. int res = -1;
  2835. char request[256];
  2836. char methods[80] = "";
  2837. char requeststr[256] = "";
  2838. char *var, *value;
  2839. char *stringp=NULL;
  2840. strncpy(request, orequest, sizeof(request)-1);
  2841. stringp=request;
  2842. var = strsep(&stringp, ";");
  2843. while(var) {
  2844. value = strchr(var, '=');
  2845. if (value) {
  2846. *value='\0';
  2847. value++;
  2848. if (!strcmp(var, "username"))
  2849. strncpy(p->username, value, sizeof(p->username)-1);
  2850. else if (!strcmp(var, "challenge"))
  2851. strncpy(p->challenge, value, sizeof(p->challenge)-1);
  2852. else if (!strcmp(var, "methods"))
  2853. strncpy(methods, value, sizeof(methods)-1);
  2854. else
  2855. ast_log(LOG_WARNING, "Unknown variable '%s' with value '%s'\n", var, value);
  2856. }
  2857. var = strsep(&stringp, ";");
  2858. }
  2859. /* Check for override RSA authentication first */
  2860. if ((override && strlen(override)) || (okey && strlen(okey))) {
  2861. /* Normal password authentication */
  2862. res = authenticate(p->challenge, override, okey, methods, requeststr, sizeof(requeststr), sin);
  2863. } else {
  2864. ast_mutex_lock(&peerl.lock);
  2865. peer = peerl.peers;
  2866. while(peer) {
  2867. if ((!strlen(p->peer) || !strcmp(p->peer, peer->name))
  2868. /* No peer specified at our end, or this is the peer */
  2869. && (!strlen(peer->username) || (!strcmp(peer->username, p->username)))
  2870. /* No username specified in peer rule, or this is the right username */
  2871. && (!peer->addr.sin_addr.s_addr || ((sin->sin_addr.s_addr & peer->mask.s_addr) == (peer->addr.sin_addr.s_addr & peer->mask.s_addr)))
  2872. /* No specified host, or this is our host */
  2873. ) {
  2874. res = authenticate(p->challenge, peer->secret, peer->outkey, methods, requeststr, sizeof(requeststr), sin);
  2875. if (!res)
  2876. break;
  2877. }
  2878. peer = peer->next;
  2879. }
  2880. ast_mutex_unlock(&peerl.lock);
  2881. }
  2882. if (strlen(requeststr))
  2883. requeststr[strlen(requeststr)-1] = '\0';
  2884. if (!res)
  2885. res = send_command(p, AST_FRAME_IAX, AST_IAX_COMMAND_AUTHREP, 0, requeststr, strlen(requeststr) + 1, -1);
  2886. return res;
  2887. }
  2888. static int iax_do_register(struct iax_registry *reg);
  2889. static int iax_do_register_s(void *data)
  2890. {
  2891. struct iax_registry *reg = data;
  2892. reg->expire = -1;
  2893. iax_do_register(reg);
  2894. return 0;
  2895. }
  2896. static int try_transfer(struct chan_iax_pvt *pvt, char *orequest)
  2897. {
  2898. int newport = 0;
  2899. int newcall = 0;
  2900. char newip[256] = "";
  2901. char request[256] = "";
  2902. char *var, *value;
  2903. struct sockaddr_in new;
  2904. char *stringp=NULL;
  2905. if (!orequest)
  2906. return -1;
  2907. strncpy(request, orequest, sizeof(request)-1);
  2908. stringp=request;
  2909. var = strsep(&stringp, ";");
  2910. while(var) {
  2911. value = strchr(var, '=');
  2912. if (value) {
  2913. *value='\0';
  2914. value++;
  2915. if (!strcmp(var, "remip"))
  2916. strncpy(newip, value, sizeof(newip)-1);
  2917. else if (!strcmp(var, "remport"))
  2918. newport = atoi(value);
  2919. else if (!strcmp(var, "remcall"))
  2920. newcall = atoi(value);
  2921. else
  2922. ast_log(LOG_WARNING, "Unknown variable '%s' with value '%s'\n", var, value);
  2923. }
  2924. var = strsep(&stringp, ";");
  2925. }
  2926. if (!newcall || !inet_aton(newip, &new.sin_addr) || !newport) {
  2927. ast_log(LOG_WARNING, "Invalid transfer request\n");
  2928. return -1;
  2929. }
  2930. pvt->transfercallno = newcall;
  2931. inet_aton(newip, &pvt->transfer.sin_addr);
  2932. pvt->transfer.sin_port = htons(newport);
  2933. pvt->transfer.sin_family = AF_INET;
  2934. pvt->transferring = TRANSFER_BEGIN;
  2935. send_command_transfer(pvt, AST_FRAME_IAX, AST_IAX_COMMAND_TXCNT, 0, NULL, 0);
  2936. return 0;
  2937. }
  2938. static int complete_dpreply(struct chan_iax_pvt *pvt, char *orequest)
  2939. {
  2940. char *var, *value;
  2941. char request[256] = "";
  2942. char exten[256] = "";
  2943. int status = CACHE_FLAG_UNKNOWN;
  2944. int expirey = iaxdefaultdpcache;
  2945. int x;
  2946. int matchmore = 0;
  2947. struct iax_dpcache *dp, *prev;
  2948. char *stringp=NULL;
  2949. strncpy(request, orequest, sizeof(request)-1);
  2950. stringp=request;
  2951. var = strsep(&stringp, ";");
  2952. while(var) {
  2953. value = strchr(var, '=');
  2954. if (value) {
  2955. *value='\0';
  2956. value++;
  2957. if (!strcmp(var, "number"))
  2958. strncpy(exten, value, sizeof(exten)-1);
  2959. else if (!strcmp(var, "status")) {
  2960. if (!strcasecmp(value, "exists"))
  2961. status = CACHE_FLAG_EXISTS;
  2962. else if (!strcasecmp(value, "nonexistant"))
  2963. status = CACHE_FLAG_NONEXISTANT;
  2964. else if (!strcasecmp(value, "canexist"))
  2965. status = CACHE_FLAG_CANEXIST;
  2966. else
  2967. ast_log(LOG_WARNING, "Unknown status '%s'\n", value);
  2968. } else if (!strcmp(var, "expirey"))
  2969. expirey = atoi(value);
  2970. else if (!strcmp(var, "ignorepat")) {
  2971. /* Don' really do much with it */
  2972. } else if (!strcmp(var, "matchmore")) {
  2973. matchmore = CACHE_FLAG_MATCHMORE;
  2974. } else
  2975. ast_log(LOG_WARNING, "Unknown variable '%s' with value '%s'\n", var, value);
  2976. }
  2977. var = strsep(&stringp, ";");
  2978. }
  2979. ast_mutex_lock(&dpcache_lock);
  2980. prev = NULL;
  2981. dp = pvt->dpentries;
  2982. while(dp) {
  2983. if (!strcmp(dp->exten, exten)) {
  2984. /* Let them go */
  2985. if (prev)
  2986. prev->peer = dp->peer;
  2987. else
  2988. pvt->dpentries = dp->peer;
  2989. dp->peer = NULL;
  2990. dp->callno = -1;
  2991. dp->expirey.tv_sec = dp->orig.tv_sec + expirey;
  2992. if (dp->flags & CACHE_FLAG_PENDING) {
  2993. dp->flags &= ~CACHE_FLAG_PENDING;
  2994. dp->flags |= status;
  2995. dp->flags |= CACHE_FLAG_MATCHMORE;
  2996. }
  2997. /* Wake up waiters */
  2998. for (x=0;x<sizeof(dp->waiters) / sizeof(dp->waiters[0]); x++)
  2999. if (dp->waiters[x] > -1)
  3000. write(dp->waiters[x], "asdf", 4);
  3001. }
  3002. prev = dp;
  3003. dp = dp->peer;
  3004. }
  3005. ast_mutex_unlock(&dpcache_lock);
  3006. return 0;
  3007. }
  3008. static int complete_transfer(int callno, char *orequest)
  3009. {
  3010. int peercallno = -1;
  3011. char request[256] = "";
  3012. char *var, *value;
  3013. struct chan_iax_pvt *pvt = iaxs[callno];
  3014. struct ast_iax_frame *cur;
  3015. char *stringp=NULL;
  3016. if (!orequest)
  3017. return -1;
  3018. strncpy(request, orequest, sizeof(request)-1);
  3019. stringp=request;
  3020. var = strsep(&stringp, ";");
  3021. while(var) {
  3022. value = strchr(var, '=');
  3023. if (value) {
  3024. *value='\0';
  3025. value++;
  3026. if (!strcmp(var, "peercallno"))
  3027. peercallno = atoi(value);
  3028. else
  3029. ast_log(LOG_WARNING, "Unknown variable '%s' with value '%s'\n", var, value);
  3030. }
  3031. var = strsep(&stringp, ";");
  3032. }
  3033. if (peercallno < 0) {
  3034. ast_log(LOG_WARNING, "Invalid transfer request\n");
  3035. return -1;
  3036. }
  3037. memcpy(&pvt->addr, &pvt->transfer, sizeof(pvt->addr));
  3038. memset(&pvt->transfer, 0, sizeof(pvt->transfer));
  3039. /* Reset sequence numbers */
  3040. pvt->oseqno = 0;
  3041. pvt->iseqno = 0;
  3042. pvt->peercallno = peercallno;
  3043. pvt->transferring = TRANSFER_NONE;
  3044. pvt->svoiceformat = -1;
  3045. pvt->voiceformat = 0;
  3046. pvt->transfercallno = -1;
  3047. memset(&pvt->rxcore, 0, sizeof(pvt->rxcore));
  3048. memset(&pvt->offset, 0, sizeof(pvt->offset));
  3049. memset(&pvt->history, 0, sizeof(pvt->history));
  3050. pvt->jitterbuffer = 0;
  3051. pvt->jitter = 0;
  3052. pvt->historicjitter = 0;
  3053. pvt->lag = 0;
  3054. pvt->last = 0;
  3055. pvt->lastsent = 0;
  3056. pvt->pingtime = DEFAULT_RETRY_TIME;
  3057. ast_mutex_lock(&iaxq.lock);
  3058. for (cur = iaxq.head; cur ; cur = cur->next) {
  3059. /* We must cancel any packets that would have been transmitted
  3060. because now we're talking to someone new. It's okay, they
  3061. were transmitted to someone that didn't care anyway. */
  3062. if (callno == cur->callno)
  3063. cur->retries = -1;
  3064. }
  3065. ast_mutex_unlock(&iaxq.lock);
  3066. return 0;
  3067. }
  3068. static int iax_ack_registry(char *orequest, struct sockaddr_in *sin, int callno)
  3069. {
  3070. struct iax_registry *reg;
  3071. /* Start pessimistic */
  3072. char request[256] = "";
  3073. char peer[256] = "";
  3074. int ourport = 0;
  3075. int refresh = 0;
  3076. char ourip[256] = "<Unspecified>";
  3077. char iabuf[INET_ADDRSTRLEN];
  3078. struct sockaddr_in oldus;
  3079. char *var, *value;
  3080. char *stringp=NULL;
  3081. if (!orequest)
  3082. return -1;
  3083. strncpy(request, orequest, sizeof(request)-1);
  3084. stringp=request;
  3085. var = strsep(&stringp, ";");
  3086. while(var) {
  3087. value = strchr(var, '=');
  3088. if (value) {
  3089. *value='\0';
  3090. value++;
  3091. if (!strcmp(var, "yourip"))
  3092. strncpy(ourip, value, sizeof(ourip)-1);
  3093. else if (!strcmp(var, "peer"))
  3094. strncpy(peer, value, sizeof(peer)-1);
  3095. else if (!strcmp(var, "yourport"))
  3096. ourport = atoi(value);
  3097. else if (!strcmp(var, "refresh"))
  3098. refresh = atoi(value);
  3099. else if (!strcmp(var, "callerid")) {
  3100. /* We don't really care about suggested Caller*ID, that's more for phones */
  3101. } else
  3102. ast_log(LOG_WARNING, "Unknown variable '%s' with value '%s'\n", var, value);
  3103. }
  3104. var = strsep(&stringp, ";");
  3105. }
  3106. reg = iaxs[callno]->reg;
  3107. memcpy(&oldus, &reg->us, sizeof(oldus));
  3108. if (memcmp(&reg->addr, sin, sizeof(&reg->addr))) {
  3109. ast_log(LOG_WARNING, "Received unsolicited registry ack from '%s'\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr));
  3110. return -1;
  3111. }
  3112. if (!inet_aton(ourip, &reg->us.sin_addr)) {
  3113. ast_log(LOG_WARNING, "Registry ack from '%s' contains invalid IP '%s'\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), ourip);
  3114. return -1;
  3115. }
  3116. reg->us.sin_port = htons(ourport);
  3117. if (refresh && (reg->refresh < refresh)) {
  3118. /* Refresh faster if necessary */
  3119. reg->refresh = refresh;
  3120. if (reg->expire > -1)
  3121. ast_sched_del(sched, reg->expire);
  3122. reg->expire = ast_sched_add(sched, (5 * reg->refresh / 6) * 1000, iax_do_register_s, reg);
  3123. }
  3124. if (memcmp(&oldus, &reg->us, sizeof(oldus)) && (option_verbose > 2)) {
  3125. snprintf(ourip, sizeof(ourip), "%s:%d", ast_inet_ntoa(iabuf, sizeof(iabuf), reg->us.sin_addr), ntohs(reg->us.sin_port));
  3126. ast_verbose(VERBOSE_PREFIX_3 "Registered to '%s', who sees us as %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), ourip);
  3127. }
  3128. reg->regstate = REG_STATE_REGISTERED;
  3129. return 0;
  3130. ast_log(LOG_WARNING, "Registry acknowledge on unknown registery '%s'\n", peer);
  3131. return -1;
  3132. }
  3133. static int iax_register(char *value, int lineno)
  3134. {
  3135. struct iax_registry *reg;
  3136. char copy[256];
  3137. char *username, *hostname, *secret;
  3138. char *porta;
  3139. char *stringp=NULL;
  3140. struct ast_hostent ahp; struct hostent *hp;
  3141. if (!value)
  3142. return -1;
  3143. strncpy(copy, value, sizeof(copy)-1);
  3144. stringp=copy;
  3145. username = strsep(&stringp, "@");
  3146. hostname = strsep(&stringp, "@");
  3147. if (!hostname) {
  3148. ast_log(LOG_WARNING, "Format for registration is user[:secret]@host[:port] at line %d", lineno);
  3149. return -1;
  3150. }
  3151. stringp=username;
  3152. username = strsep(&stringp, ":");
  3153. secret = strsep(&stringp, ":");
  3154. stringp=hostname;
  3155. hostname = strsep(&stringp, ":");
  3156. porta = strsep(&stringp, ":");
  3157. if (porta && !atoi(porta)) {
  3158. ast_log(LOG_WARNING, "%s is not a valid port number at line %d\n", porta, lineno);
  3159. return -1;
  3160. }
  3161. hp = ast_gethostbyname(hostname, &ahp);
  3162. if (!hp) {
  3163. ast_log(LOG_WARNING, "Host '%s' not found at line %d\n", hostname, lineno);
  3164. return -1;
  3165. }
  3166. reg = malloc(sizeof(struct iax_registry));
  3167. if (reg) {
  3168. memset(reg, 0, sizeof(struct iax_registry));
  3169. strncpy(reg->username, username, sizeof(reg->username)-1);
  3170. if (secret)
  3171. strncpy(reg->secret, secret, sizeof(reg->secret)-1);
  3172. reg->expire = -1;
  3173. reg->refresh = AST_DEFAULT_REG_EXPIRE;
  3174. reg->addr.sin_family = AF_INET;
  3175. memcpy(&reg->addr.sin_addr, hp->h_addr, sizeof(&reg->addr.sin_addr));
  3176. reg->addr.sin_port = porta ? htons(atoi(porta)) : htons(AST_DEFAULT_IAX_PORTNO);
  3177. reg->next = registrations;
  3178. reg->callno = -1;
  3179. registrations = reg;
  3180. } else {
  3181. ast_log(LOG_ERROR, "Out of memory\n");
  3182. return -1;
  3183. }
  3184. return 0;
  3185. }
  3186. static int expire_registry(void *data)
  3187. {
  3188. struct iax_peer *p = data;
  3189. /* Reset the address */
  3190. memset(&p->addr, 0, sizeof(p->addr));
  3191. /* Reset expire notice */
  3192. p->expire = -1;
  3193. /* Reset expirey value */
  3194. p->expirey = expirey;
  3195. if (iax_regfunk)
  3196. iax_regfunk(p->name, 0);
  3197. return 0;
  3198. }
  3199. static int iax_poke_peer(struct iax_peer *peer);
  3200. static int update_registry(char *name, struct sockaddr_in *sin, int callno)
  3201. {
  3202. /* Called from IAX thread only, with proper iaxsl lock */
  3203. char requeststr[256] = "";
  3204. struct iax_peer *p;
  3205. char iabuf[INET_ADDRSTRLEN];
  3206. for (p = peerl.peers;p;p = p->next) {
  3207. if (!strcasecmp(name, p->name)) {
  3208. break;
  3209. }
  3210. }
  3211. #ifdef MYSQL_FRIENDS
  3212. if (!p)
  3213. p = mysql_peer(name);
  3214. #endif
  3215. if (p) {
  3216. #ifdef MYSQL_FRIENDS
  3217. if (p->delme)
  3218. mysql_update_peer(name, sin);
  3219. #endif
  3220. if (memcmp(&p->addr, sin, sizeof(p->addr))) {
  3221. if (iax_regfunk)
  3222. iax_regfunk(p->name, 1);
  3223. if (option_verbose > 2)
  3224. ast_verbose(VERBOSE_PREFIX_3 "Registered '%s' (%s) at %s:%d\n", p->name,
  3225. iaxs[callno]->state & IAX_STATE_AUTHENTICATED ? "AUTHENTICATED" : "UNAUTHENTICATED", ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), htons(sin->sin_port));
  3226. iax_poke_peer(p);
  3227. }
  3228. /* Update the host */
  3229. memcpy(&p->addr, sin, sizeof(p->addr));
  3230. /* Setup the expirey */
  3231. if (p->expire > -1)
  3232. ast_sched_del(sched, p->expire);
  3233. if (p->expirey)
  3234. p->expire = ast_sched_add(sched, p->expirey * 1000, expire_registry, (void *)p);
  3235. MYSNPRINTF "peer=%s;yourip=%s;yourport=%d;refresh=%d;",
  3236. p->name, ast_inet_ntoa(iabuf, sizeof(iabuf), p->addr.sin_addr), ntohs(p->addr.sin_port), p->expirey);
  3237. if (p->hascallerid)
  3238. MYSNPRINTF "callerid=%s;", p->callerid);
  3239. requeststr[strlen(requeststr)-1] = '\0';
  3240. if (p->delme)
  3241. free(p);
  3242. return send_command_final(iaxs[callno], AST_FRAME_IAX, AST_IAX_COMMAND_REGACK, 0, requeststr, strlen(requeststr) + 1, -1);;
  3243. }
  3244. ast_log(LOG_WARNING, "No such peer '%s'\n", name);
  3245. return -1;
  3246. }
  3247. static int registry_authrequest(char *name, int callno)
  3248. {
  3249. char requeststr[256] = "";
  3250. struct iax_peer *p;
  3251. for (p = peerl.peers;p;p = p->next) {
  3252. if (!strcasecmp(name, p->name)) {
  3253. break;
  3254. }
  3255. }
  3256. #ifdef MYSQL_FRIENDS
  3257. if (!p)
  3258. p = mysql_peer(name);
  3259. #endif
  3260. if (p) {
  3261. MYSNPRINTF "methods=%s;", p->methods);
  3262. if (strstr(p->methods, "md5") || strstr(p->methods, "rsa")) {
  3263. /* Build the challenge */
  3264. snprintf(iaxs[callno]->challenge, sizeof(iaxs[callno]->challenge), "%d", rand());
  3265. MYSNPRINTF "challenge=%s;", iaxs[callno]->challenge);
  3266. }
  3267. MYSNPRINTF "peer=%s;", name);
  3268. requeststr[strlen(requeststr)-1] = '\0';
  3269. if (p->delme)
  3270. free(p);
  3271. return send_command(iaxs[callno], AST_FRAME_IAX, AST_IAX_COMMAND_REGAUTH, 0, requeststr, strlen(requeststr) + 1, -1);
  3272. }
  3273. ast_log(LOG_WARNING, "No such peer '%s'\n", name);
  3274. return 0;
  3275. }
  3276. static int registry_rerequest(char *orequest, int callno, struct sockaddr_in *sin)
  3277. {
  3278. struct iax_registry *reg;
  3279. /* Start pessimistic */
  3280. char request[256] = "";
  3281. char requeststr[256] = "";
  3282. char peer[256] = "";
  3283. char methods[256] = "";
  3284. char challenge[256] = "";
  3285. char iabuf[INET_ADDRSTRLEN];
  3286. char *var, *value;
  3287. int res;
  3288. char *stringp=NULL;
  3289. if (!orequest)
  3290. return -1;
  3291. strncpy(request, orequest, sizeof(request)-1);
  3292. stringp=request;
  3293. var = strsep(&stringp, ";");
  3294. while(var) {
  3295. value = strchr(var, '=');
  3296. if (value) {
  3297. *value='\0';
  3298. value++;
  3299. if (!strcmp(var, "methods"))
  3300. strncpy(methods, value, sizeof(methods)-1);
  3301. else if (!strcmp(var, "peer"))
  3302. strncpy(peer, value, sizeof(peer)-1);
  3303. else if (!strcmp(var, "challenge"))
  3304. strncpy(challenge, value, sizeof(challenge)-1);
  3305. else
  3306. ast_log(LOG_WARNING, "Unknown variable '%s' with value '%s'\n", var, value);
  3307. }
  3308. var = strsep(&stringp, ";");
  3309. }
  3310. reg = iaxs[callno]->reg;
  3311. if (memcmp(&reg->addr, sin, sizeof(&reg->addr))) {
  3312. ast_log(LOG_WARNING, "Received unsolicited registry authenticate request from '%s'\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr));
  3313. return -1;
  3314. }
  3315. if (!strlen(reg->secret)) {
  3316. ast_log(LOG_NOTICE, "No secret associated with peer '%s'\n", reg->username);
  3317. reg->regstate = REG_STATE_NOAUTH;
  3318. return -1;
  3319. }
  3320. MYSNPRINTF "peer=%s;refresh=%d;", reg->username, reg->refresh);
  3321. if (reg->secret[0] == '[') {
  3322. char tmpkey[256];
  3323. strncpy(tmpkey, reg->secret + 1, sizeof(tmpkey) - 1);
  3324. tmpkey[strlen(tmpkey) - 1] = '\0';
  3325. res = authenticate(challenge, NULL, tmpkey, methods, requeststr, sizeof(requeststr), sin);
  3326. } else
  3327. res = authenticate(challenge, reg->secret, NULL, methods, requeststr, sizeof(requeststr), sin);
  3328. if (!res) {
  3329. reg->regstate = REG_STATE_AUTHSENT;
  3330. return send_command(iaxs[callno], AST_FRAME_IAX, AST_IAX_COMMAND_REGREQ, 0, requeststr, strlen(requeststr) + 1, -1);
  3331. } else
  3332. return -1;
  3333. ast_log(LOG_WARNING, "Registry acknowledge on unknown registery '%s'\n", peer);
  3334. return -1;
  3335. }
  3336. static int stop_stuff(int callno)
  3337. {
  3338. if (iaxs[callno]->lagid > -1)
  3339. ast_sched_del(sched, iaxs[callno]->lagid);
  3340. iaxs[callno]->lagid = -1;
  3341. if (iaxs[callno]->pingid > -1)
  3342. ast_sched_del(sched, iaxs[callno]->pingid);
  3343. iaxs[callno]->pingid = -1;
  3344. if (iaxs[callno]->autoid > -1)
  3345. ast_sched_del(sched, iaxs[callno]->autoid);
  3346. iaxs[callno]->autoid = -1;
  3347. if (iaxs[callno]->initid > -1)
  3348. ast_sched_del(sched, iaxs[callno]->initid);
  3349. iaxs[callno]->initid = -1;
  3350. return 0;
  3351. }
  3352. static int auto_hangup(void *nothing)
  3353. {
  3354. /* Called from IAX thread only, without iaxs lock */
  3355. int callno = (int)(long)(nothing);
  3356. ast_mutex_lock(&iaxsl[callno]);
  3357. if (iaxs[callno]) {
  3358. iaxs[callno]->autoid = -1;
  3359. send_command_final(iaxs[callno], AST_FRAME_IAX, AST_IAX_COMMAND_HANGUP, 0, "Timeout", strlen("Timeout") + 1, -1);
  3360. }
  3361. ast_mutex_unlock(&iaxsl[callno]);
  3362. return 0;
  3363. }
  3364. static void iax_dprequest(struct iax_dpcache *dp, int callno)
  3365. {
  3366. /* Auto-hangup with 30 seconds of inactivity */
  3367. if (iaxs[callno]->autoid > -1)
  3368. ast_sched_del(sched, iaxs[callno]->autoid);
  3369. iaxs[callno]->autoid = ast_sched_add(sched, 30000, auto_hangup, (void *)(long)callno);
  3370. send_command(iaxs[callno], AST_FRAME_IAX, AST_IAX_COMMAND_DPREQ, 0, dp->exten, strlen(dp->exten) + 1, -1);
  3371. dp->flags |= CACHE_FLAG_TRANSMITTED;
  3372. }
  3373. static int iax_vnak(int callno)
  3374. {
  3375. return send_command_immediate(iaxs[callno], AST_FRAME_IAX, AST_IAX_COMMAND_VNAK, 0, NULL, 0, iaxs[callno]->iseqno);
  3376. }
  3377. static void vnak_retransmit(int callno, int last)
  3378. {
  3379. struct ast_iax_frame *f;
  3380. ast_mutex_lock(&iaxq.lock);
  3381. f = iaxq.head;
  3382. while(f) {
  3383. /* Send a copy immediately */
  3384. if ((f->callno == callno) && iaxs[f->callno] &&
  3385. (f->seqno >= last)) {
  3386. send_packet(f);
  3387. }
  3388. f = f->next;
  3389. }
  3390. ast_mutex_unlock(&iaxq.lock);
  3391. }
  3392. static int iax_poke_peer_s(void *data)
  3393. {
  3394. struct iax_peer *peer = data;
  3395. peer->pokeexpire = -1;
  3396. iax_poke_peer(peer);
  3397. return 0;
  3398. }
  3399. static int socket_read(int *id, int fd, short events, void *cbdata)
  3400. {
  3401. struct sockaddr_in sin;
  3402. int res;
  3403. int new = NEW_PREVENT;
  3404. char buf[4096];
  3405. char src[80];
  3406. int len = sizeof(sin);
  3407. int dcallno = -1;
  3408. struct ast_iax_full_hdr *fh = (struct ast_iax_full_hdr *)buf;
  3409. struct ast_iax_mini_hdr *mh = (struct ast_iax_mini_hdr *)buf;
  3410. struct ast_iax_frame fr, *cur;
  3411. struct ast_frame f;
  3412. struct ast_channel *c;
  3413. struct iax_dpcache *dp;
  3414. struct iax_peer *peer;
  3415. int format;
  3416. int exists;
  3417. int mm;
  3418. char iabuf[INET_ADDRSTRLEN];
  3419. char rel0[256] = "";
  3420. char rel1[255];
  3421. char empty[32]=""; /* Safety measure */
  3422. res = recvfrom(netsocket, buf, sizeof(buf), 0,(struct sockaddr *) &sin, &len);
  3423. if (res < 0) {
  3424. if (errno != ECONNREFUSED)
  3425. ast_log(LOG_WARNING, "Error: %s\n", strerror(errno));
  3426. handle_error();
  3427. return 1;
  3428. }
  3429. if (res < sizeof(struct ast_iax_mini_hdr)) {
  3430. ast_log(LOG_WARNING, "midget packet received (%d of %d min)\n", res, (int)sizeof(struct ast_iax_mini_hdr));
  3431. return 1;
  3432. }
  3433. #ifdef DEBUG_SUPPORT
  3434. if (iaxdebug)
  3435. showframe(NULL, fh, 1, &sin);
  3436. #endif
  3437. if (ntohs(mh->callno) & AST_FLAG_FULL) {
  3438. /* Get the destination call number */
  3439. dcallno = ntohs(fh->dcallno);
  3440. /* Retrieve the type and subclass */
  3441. f.frametype = fh->type;
  3442. f.subclass = uncompress_subclass(fh->csub);
  3443. #if 0
  3444. f.subclass = fh->subclasshigh << 16;
  3445. f.subclass += ntohs(fh->subclasslow);
  3446. #endif
  3447. if ((f.frametype == AST_FRAME_IAX) && ((f.subclass == AST_IAX_COMMAND_NEW) || (f.subclass == AST_IAX_COMMAND_REGREQ)
  3448. || (f.subclass == AST_IAX_COMMAND_POKE)))
  3449. new = NEW_ALLOW;
  3450. } else {
  3451. /* Don't know anything about it yet */
  3452. f.frametype = AST_FRAME_NULL;
  3453. f.subclass = 0;
  3454. }
  3455. fr.callno = find_callno(ntohs(mh->callno) & ~AST_FLAG_FULL, dcallno, &sin, new);
  3456. if (fr.callno > 0)
  3457. ast_mutex_lock(&iaxsl[fr.callno]);
  3458. if ((fr.callno < 0) || !iaxs[fr.callno]) {
  3459. /* A call arrived for a non-existant destination. Unless it's an "inval"
  3460. frame, reply with an inval */
  3461. if (ntohs(mh->callno) & AST_FLAG_FULL) {
  3462. /* We can only raw hangup control frames */
  3463. if (((f.subclass != AST_IAX_COMMAND_INVAL) &&
  3464. (f.subclass != AST_IAX_COMMAND_TXCNT) &&
  3465. (f.subclass != AST_IAX_COMMAND_TXACC))||
  3466. (f.frametype != AST_FRAME_IAX))
  3467. raw_hangup(&sin, ntohs(fh->dcallno), ntohs(mh->callno) & ~AST_FLAG_FULL
  3468. );
  3469. }
  3470. if (fr.callno > 0)
  3471. ast_mutex_unlock(&iaxsl[fr.callno]);
  3472. return 1;
  3473. }
  3474. if (((f.subclass != AST_IAX_COMMAND_TXCNT) &&
  3475. (f.subclass != AST_IAX_COMMAND_TXACC)) || (f.frametype != AST_FRAME_IAX))
  3476. iaxs[fr.callno]->peercallno = (short)(ntohs(mh->callno) & ~AST_FLAG_FULL);
  3477. if (ntohs(mh->callno) & AST_FLAG_FULL) {
  3478. if (option_debug)
  3479. ast_log(LOG_DEBUG, "Received packet %d, (%d, %d)\n", ntohs(fh->seqno), f.frametype, f.subclass);
  3480. /* Check if it's out of order (and not an ACK or INVAL) */
  3481. fr.seqno = ntohs(fh->seqno);
  3482. fr.ts = ntohl(fh->ts);
  3483. if ((iaxs[fr.callno]->iseqno != fr.seqno) &&
  3484. (iaxs[fr.callno]->iseqno ||
  3485. ((f.subclass != AST_IAX_COMMAND_TXCNT) &&
  3486. (f.subclass != AST_IAX_COMMAND_TXACC)) ||
  3487. (f.subclass != AST_FRAME_IAX))) {
  3488. if (
  3489. ((f.subclass != AST_IAX_COMMAND_ACK) &&
  3490. (f.subclass != AST_IAX_COMMAND_INVAL) &&
  3491. (f.subclass != AST_IAX_COMMAND_TXCNT) &&
  3492. (f.subclass != AST_IAX_COMMAND_TXACC) &&
  3493. (f.subclass != AST_IAX_COMMAND_VNAK)) ||
  3494. (f.frametype != AST_FRAME_IAX)) {
  3495. /* If it's not an ACK packet, it's out of order. */
  3496. if (option_debug)
  3497. ast_log(LOG_DEBUG, "Packet arrived out of order (expecting %d, got %d) (frametype = %d, subclass = %d)\n",
  3498. iaxs[fr.callno]->iseqno, fr.seqno, f.frametype, f.subclass);
  3499. if (iaxs[fr.callno]->iseqno > fr.seqno) {
  3500. /* If we've already seen it, ack it XXX There's a border condition here XXX */
  3501. if ((f.frametype != AST_FRAME_IAX) ||
  3502. ((f.subclass != AST_IAX_COMMAND_ACK) && (f.subclass != AST_IAX_COMMAND_INVAL))) {
  3503. if (option_debug)
  3504. ast_log(LOG_DEBUG, "Acking anyway\n");
  3505. send_command_immediate(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_ACK, fr.ts, NULL, 0,fr.seqno);
  3506. }
  3507. } else {
  3508. /* Send a VNAK requesting retransmission */
  3509. iax_vnak(fr.callno);
  3510. }
  3511. ast_mutex_unlock(&iaxsl[fr.callno]);
  3512. return 1;
  3513. }
  3514. } else {
  3515. /* Increment unless it's an ACK or VNAK */
  3516. if (((f.subclass != AST_IAX_COMMAND_ACK) &&
  3517. (f.subclass != AST_IAX_COMMAND_INVAL) &&
  3518. (f.subclass != AST_IAX_COMMAND_TXCNT) &&
  3519. (f.subclass != AST_IAX_COMMAND_TXACC) &&
  3520. (f.subclass != AST_IAX_COMMAND_VNAK)) ||
  3521. (f.frametype != AST_FRAME_IAX))
  3522. iaxs[fr.callno]->iseqno++;
  3523. }
  3524. /* A full frame */
  3525. if (res < sizeof(struct ast_iax_full_hdr)) {
  3526. ast_log(LOG_WARNING, "midget packet received (%d of %d min)\n", res, (int)sizeof(struct ast_iax_full_hdr));
  3527. ast_mutex_unlock(&iaxsl[fr.callno]);
  3528. return 1;
  3529. }
  3530. f.datalen = res - sizeof(struct ast_iax_full_hdr);
  3531. if (f.datalen)
  3532. f.data = buf + sizeof(struct ast_iax_full_hdr);
  3533. else
  3534. f.data = empty;
  3535. /* Unless this is an ACK or INVAL frame, ack it */
  3536. if ((f.frametype != AST_FRAME_IAX) ||
  3537. ((f.subclass != AST_IAX_COMMAND_ACK) &&
  3538. (f.subclass != AST_IAX_COMMAND_TXCNT) &&
  3539. (f.subclass != AST_IAX_COMMAND_TXACC) &&
  3540. (f.subclass != AST_IAX_COMMAND_INVAL) &&
  3541. (f.subclass != AST_IAX_COMMAND_VNAK)))
  3542. send_command_immediate(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_ACK, fr.ts, NULL, 0,fr.seqno);
  3543. if (f.frametype == AST_FRAME_VOICE) {
  3544. if (f.subclass != iaxs[fr.callno]->voiceformat) {
  3545. iaxs[fr.callno]->voiceformat = f.subclass;
  3546. ast_log(LOG_DEBUG, "Ooh, voice format changed to %s\n", ast_getformatname(f.subclass));
  3547. if (iaxs[fr.callno]->owner) {
  3548. int orignative;
  3549. retryowner:
  3550. if (ast_mutex_trylock(&iaxs[fr.callno]->owner->lock)) {
  3551. ast_mutex_unlock(&iaxsl[fr.callno]);
  3552. usleep(1);
  3553. ast_mutex_lock(&iaxsl[fr.callno]);
  3554. if (iaxs[fr.callno] && iaxs[fr.callno]->owner) goto retryowner;
  3555. }
  3556. orignative = iaxs[fr.callno]->owner->nativeformats;
  3557. iaxs[fr.callno]->owner->nativeformats = f.subclass;
  3558. if (iaxs[fr.callno]->owner->readformat)
  3559. ast_set_read_format(iaxs[fr.callno]->owner, iaxs[fr.callno]->owner->readformat);
  3560. iaxs[fr.callno]->owner->nativeformats = orignative;
  3561. ast_mutex_unlock(&iaxs[fr.callno]->owner->lock);
  3562. }
  3563. }
  3564. }
  3565. if (f.frametype == AST_FRAME_IAX) {
  3566. if (iaxs[fr.callno]->initid > -1) {
  3567. /* Don't auto congest anymore since we've gotten something usefulb ack */
  3568. ast_sched_del(sched, iaxs[fr.callno]->initid);
  3569. iaxs[fr.callno]->initid = -1;
  3570. }
  3571. /* Handle the IAX pseudo frame itself */
  3572. if (option_debug)
  3573. ast_log(LOG_DEBUG, "IAX subclass %d received\n", f.subclass);
  3574. /* Go through the motions of delivering the packet without actually doing so,
  3575. unless this is a lag request since it will be done for real */
  3576. if (f.subclass != AST_IAX_COMMAND_LAGRQ)
  3577. schedule_delivery(&fr, 0);
  3578. switch(f.subclass) {
  3579. case AST_IAX_COMMAND_ACK:
  3580. /* Ack the packet with the given timestamp */
  3581. ast_mutex_lock(&iaxq.lock);
  3582. for (cur = iaxq.head; cur ; cur = cur->next) {
  3583. /* If it's our call, and our timestamp, mark -1 retries */
  3584. if ((fr.callno == cur->callno) && (fr.seqno == cur->seqno)) {
  3585. cur->retries = -1;
  3586. /* Destroy call if this is the end */
  3587. if (cur->final) {
  3588. if (option_debug)
  3589. ast_log(LOG_DEBUG, "Really destroying %d, having been acked on final message\n", fr.callno);
  3590. iax_destroy_nolock(fr.callno);
  3591. }
  3592. }
  3593. }
  3594. ast_mutex_unlock(&iaxq.lock);
  3595. break;
  3596. case AST_IAX_COMMAND_QUELCH:
  3597. if (iaxs[fr.callno]->state & IAX_STATE_STARTED)
  3598. iaxs[fr.callno]->quelch = 1;
  3599. break;
  3600. case AST_IAX_COMMAND_UNQUELCH:
  3601. if (iaxs[fr.callno]->state & IAX_STATE_STARTED)
  3602. iaxs[fr.callno]->quelch = 0;
  3603. break;
  3604. case AST_IAX_COMMAND_TXACC:
  3605. if (iaxs[fr.callno]->transferring == TRANSFER_BEGIN) {
  3606. /* Ack the packet with the given timestamp */
  3607. ast_mutex_lock(&iaxq.lock);
  3608. for (cur = iaxq.head; cur ; cur = cur->next) {
  3609. /* Cancel any outstanding txcnt's */
  3610. if ((fr.callno == cur->callno) && (cur->transfer))
  3611. cur->retries = -1;
  3612. }
  3613. ast_mutex_unlock(&iaxq.lock);
  3614. snprintf(rel1, sizeof(rel1), "callno=%d;", iaxs[fr.callno]->callno);
  3615. send_command(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_TXREADY, 0, rel1, strlen(rel1) + 1, -1);
  3616. iaxs[fr.callno]->transferring = TRANSFER_READY;
  3617. }
  3618. break;
  3619. case AST_IAX_COMMAND_NEW:
  3620. /* Ignore if it's already up */
  3621. if (iaxs[fr.callno]->state & (IAX_STATE_STARTED | IAX_STATE_TBD))
  3622. break;
  3623. ((char *)f.data)[f.datalen] = '\0';
  3624. if (check_access(fr.callno, &sin, f.data, f.datalen)) {
  3625. /* They're not allowed on */
  3626. send_command_final(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_REJECT, 0, "No authority found", strlen("No authority found"), -1);
  3627. ast_log(LOG_NOTICE, "Rejected connect attempt from %s, request '%s'\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), (char *)f.data);
  3628. break;
  3629. }
  3630. /* This might re-enter the IAX code and need the lock */
  3631. exists = ast_exists_extension(NULL, iaxs[fr.callno]->context, iaxs[fr.callno]->exten, 1, iaxs[fr.callno]->callerid);
  3632. if (!strlen(iaxs[fr.callno]->secret) && !strlen(iaxs[fr.callno]->inkeys)) {
  3633. if (strcmp(iaxs[fr.callno]->exten, "TBD") && !exists) {
  3634. send_command_final(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_REJECT, 0, "No such context/extension", strlen("No such context/extension"), -1);
  3635. ast_log(LOG_NOTICE, "Rejected connect attempt from %s, request '%s@%s' does not exist\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), iaxs[fr.callno]->exten, iaxs[fr.callno]->context);
  3636. } else {
  3637. /* Select an appropriate format */
  3638. format = iaxs[fr.callno]->peerformat & iax_capability;
  3639. if (!format) {
  3640. format = iaxs[fr.callno]->peercapability & iax_capability;
  3641. if (!format) {
  3642. send_command_final(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_REJECT, 0, "Unable to negotiate codec", strlen("Unable to negotiate codec"), -1);
  3643. ast_log(LOG_NOTICE, "Rejected connect attempt from %s, requested/capability 0x%x/0x%x incompatible with our capability 0x%x.\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), iaxs[fr.callno]->peerformat, iaxs[fr.callno]->peercapability, iax_capability);
  3644. } else {
  3645. /* Pick one... */
  3646. format = ast_best_codec(iaxs[fr.callno]->peercapability & iax_capability);
  3647. if (!format) {
  3648. ast_log(LOG_ERROR, "No best format in 0x%x???\n", iaxs[fr.callno]->peercapability & iax_capability);
  3649. send_command_final(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_REJECT, 0, "Unable to negotiate codec", strlen("Unable to negotiate codec"), -1);
  3650. ast_log(LOG_NOTICE, "Rejected connect attempt from %s, requested/capability 0x%x/0x%x incompatible with our capability 0x%x.\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), iaxs[fr.callno]->peerformat, iaxs[fr.callno]->peercapability, iax_capability);
  3651. iaxs[fr.callno]->alreadygone = 1;
  3652. break;
  3653. }
  3654. }
  3655. }
  3656. if (format) {
  3657. /* No authentication required, let them in */
  3658. snprintf(rel1, sizeof(rel1), "formats=%d;", format);
  3659. send_command(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_ACCEPT, 0, rel1, strlen(rel1) + 1, -1);
  3660. if (strcmp(iaxs[fr.callno]->exten, "TBD")) {
  3661. iaxs[fr.callno]->state |= IAX_STATE_STARTED;
  3662. if (option_verbose > 2)
  3663. ast_verbose(VERBOSE_PREFIX_3 "Accepting unauthenticated call from %s, requested format = %d, actual format = %d\n",
  3664. ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), iaxs[fr.callno]->peerformat,format);
  3665. if(!(c = ast_iax_new(iaxs[fr.callno], AST_STATE_RING, format)))
  3666. iax_destroy_nolock(fr.callno);
  3667. } else {
  3668. iaxs[fr.callno]->state |= IAX_STATE_TBD;
  3669. /* If this is a TBD call, we're ready but now what... */
  3670. if (option_verbose > 2)
  3671. ast_verbose(VERBOSE_PREFIX_3 "Accepted unauthenticated TBD call from %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr));
  3672. }
  3673. }
  3674. }
  3675. break;
  3676. }
  3677. authenticate_request(iaxs[fr.callno]);
  3678. iaxs[fr.callno]->state |= IAX_STATE_AUTHENTICATED;
  3679. break;
  3680. case AST_IAX_COMMAND_DPREQ:
  3681. /* Request status in the dialplan */
  3682. ((char *)f.data)[f.datalen] = '\0';
  3683. if ((iaxs[fr.callno]->state & IAX_STATE_TBD) &&
  3684. !(iaxs[fr.callno]->state & IAX_STATE_STARTED) && f.datalen) {
  3685. mm = ast_matchmore_extension(NULL, iaxs[fr.callno]->context, (char *)f.data, 1, iaxs[fr.callno]->callerid);
  3686. /* Must be started */
  3687. if (ast_exists_extension(NULL, iaxs[fr.callno]->context, (char *)f.data, 1, iaxs[fr.callno]->callerid)) {
  3688. strncpy(rel0, "exists", sizeof(rel0) - 1);
  3689. } else if (ast_canmatch_extension(NULL, iaxs[fr.callno]->context, (char *)f.data, 1, iaxs[fr.callno]->callerid)) {
  3690. strncpy(rel0, "canexist", sizeof(rel0) - 1);
  3691. } else {
  3692. strncpy(rel0, "nonexistant", sizeof(rel0) - 1);
  3693. }
  3694. snprintf(rel1, sizeof(rel1), "number=%s;status=%s;ignorepat=%s;expirey=%d;matchmore=%s;",
  3695. (char *)f.data, rel0,
  3696. ast_ignore_pattern(iaxs[fr.callno]->context, (char *)f.data) ? "yes" : "no",
  3697. iaxdefaultdpcache, mm ? "yes" : "no");
  3698. send_command(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_DPREP, 0, rel1, strlen(rel1) + 1, -1);
  3699. }
  3700. break;
  3701. case AST_IAX_COMMAND_HANGUP:
  3702. iaxs[fr.callno]->alreadygone = 1;
  3703. ast_log(LOG_DEBUG, "Immediately destroying %d, having received hangup\n", fr.callno);
  3704. iax_destroy_nolock(fr.callno);
  3705. break;
  3706. case AST_IAX_COMMAND_REJECT:
  3707. if (f.data)
  3708. ((char *)f.data)[f.datalen] = '\0';
  3709. if (iaxs[fr.callno]->owner)
  3710. ast_log(LOG_WARNING, "Call rejected by %s: %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), iaxs[fr.callno]->addr.sin_addr), (char *)f.data);
  3711. iaxs[fr.callno]->error = EPERM;
  3712. ast_log(LOG_DEBUG, "Immediately destroying %d, having received reject\n", fr.callno);
  3713. iax_destroy_nolock(fr.callno);
  3714. break;
  3715. case AST_IAX_COMMAND_ACCEPT:
  3716. /* Ignore if call is already up or needs authentication or is a TBD */
  3717. if (iaxs[fr.callno]->state & (IAX_STATE_STARTED | IAX_STATE_TBD | IAX_STATE_AUTHENTICATED))
  3718. break;
  3719. if (f.data && f.datalen) {
  3720. ((char *)f.data)[f.datalen]='\0';
  3721. iax_getformats(fr.callno, (char *)f.data);
  3722. } else {
  3723. if (iaxs[fr.callno]->owner)
  3724. iaxs[fr.callno]->peerformat = iaxs[fr.callno]->owner->nativeformats;
  3725. else
  3726. iaxs[fr.callno]->peerformat = iax_capability;
  3727. }
  3728. if (option_verbose > 2)
  3729. ast_verbose(VERBOSE_PREFIX_3 "Call accepted by %s (format %s)\n", ast_inet_ntoa(iabuf, sizeof(iabuf), iaxs[fr.callno]->addr.sin_addr), ast_getformatname(iaxs[fr.callno]->peerformat));
  3730. if (!(iaxs[fr.callno]->peerformat & iaxs[fr.callno]->capability)) {
  3731. send_command_final(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_REJECT, 0, "Unable to negotiate codec", strlen("Unable to negotiate codec"), -1);
  3732. ast_log(LOG_NOTICE, "Rejected call to %s, format 0x%x incompatible with our capability 0x%x.\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), iaxs[fr.callno]->peerformat, iaxs[fr.callno]->capability);
  3733. } else {
  3734. iaxs[fr.callno]->state |= IAX_STATE_STARTED;
  3735. if (iaxs[fr.callno]->owner) {
  3736. /* Switch us to use a compatible format */
  3737. iaxs[fr.callno]->owner->nativeformats = iaxs[fr.callno]->peerformat;
  3738. if (option_verbose > 2)
  3739. ast_verbose(VERBOSE_PREFIX_3 "Format for call is %s\n", ast_getformatname(iaxs[fr.callno]->owner->nativeformats));
  3740. /* Setup read/write formats properly. */
  3741. if (iaxs[fr.callno]->owner->writeformat)
  3742. ast_set_write_format(iaxs[fr.callno]->owner, iaxs[fr.callno]->owner->writeformat);
  3743. if (iaxs[fr.callno]->owner->readformat)
  3744. ast_set_read_format(iaxs[fr.callno]->owner, iaxs[fr.callno]->owner->readformat);
  3745. }
  3746. }
  3747. ast_mutex_lock(&dpcache_lock);
  3748. dp = iaxs[fr.callno]->dpentries;
  3749. while(dp) {
  3750. if (!(dp->flags & CACHE_FLAG_TRANSMITTED)) {
  3751. iax_dprequest(dp, fr.callno);
  3752. }
  3753. dp = dp->peer;
  3754. }
  3755. ast_mutex_unlock(&dpcache_lock);
  3756. break;
  3757. case AST_IAX_COMMAND_POKE:
  3758. /* Send back a pong packet with the original timestamp */
  3759. send_command_final(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_PONG, fr.ts, NULL, 0, -1);
  3760. break;
  3761. case AST_IAX_COMMAND_PING:
  3762. #ifdef BRIDGE_OPTIMIZATION
  3763. if (iaxs[fr.callno]->bridgecallno > -1) {
  3764. /* If we're in a bridged call, just forward this */
  3765. forward_command(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_PING, fr.ts, NULL, 0, -1);
  3766. } else {
  3767. /* Send back a pong packet with the original timestamp */
  3768. send_command(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_PONG, fr.ts, NULL, 0, -1);
  3769. }
  3770. #else
  3771. /* Send back a pong packet with the original timestamp */
  3772. send_command(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_PONG, fr.ts, NULL, 0, -1);
  3773. #endif
  3774. break;
  3775. case AST_IAX_COMMAND_PONG:
  3776. #ifdef BRIDGE_OPTIMIZATION
  3777. if (iaxs[fr.callno]->bridgecallno > -1) {
  3778. /* Forward to the other side of the bridge */
  3779. forward_command(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_PONG, fr.ts, NULL, 0, -1);
  3780. } else {
  3781. /* Calculate ping time */
  3782. iaxs[fr.callno]->pingtime = calc_timestamp(iaxs[fr.callno], 0) - fr.ts;
  3783. }
  3784. #else
  3785. /* Calculate ping time */
  3786. iaxs[fr.callno]->pingtime = calc_timestamp(iaxs[fr.callno], 0) - fr.ts;
  3787. #endif
  3788. if (iaxs[fr.callno]->peerpoke) {
  3789. peer = iaxs[fr.callno]->peerpoke;
  3790. if ((peer->lastms < 0) || (peer->lastms > peer->maxms)) {
  3791. if (iaxs[fr.callno]->pingtime <= peer->maxms)
  3792. ast_log(LOG_NOTICE, "Peer '%s' is now REACHABLE!\n", peer->name);
  3793. } else if ((peer->lastms > 0) && (peer->lastms <= peer->maxms)) {
  3794. if (iaxs[fr.callno]->pingtime > peer->maxms)
  3795. ast_log(LOG_NOTICE, "Peer '%s' is now TOO LAGGED (%d ms)!\n", peer->name, iaxs[fr.callno]->pingtime);
  3796. }
  3797. peer->lastms = iaxs[fr.callno]->pingtime;
  3798. peer->callno = -1;
  3799. if (peer->pokeexpire > -1)
  3800. ast_sched_del(sched, peer->pokeexpire);
  3801. iax_destroy_nolock(fr.callno);
  3802. /* Try again eventually */
  3803. if ((peer->lastms < 0) || (peer->lastms > peer->maxms))
  3804. peer->pokeexpire = ast_sched_add(sched, DEFAULT_FREQ_NOTOK, iax_poke_peer_s, peer);
  3805. else
  3806. peer->pokeexpire = ast_sched_add(sched, DEFAULT_FREQ_OK, iax_poke_peer_s, peer);
  3807. }
  3808. break;
  3809. case AST_IAX_COMMAND_LAGRQ:
  3810. case AST_IAX_COMMAND_LAGRP:
  3811. #ifdef BRIDGE_OPTIMIZATION
  3812. if (iaxs[fr.callno]->bridgecallno > -1) {
  3813. forward_command(iaxs[fr.callno], AST_FRAME_IAX, f.subclass, fr.ts, NULL, 0, -1);
  3814. } else {
  3815. #endif
  3816. /* A little strange -- We have to actually go through the motions of
  3817. delivering the packet. In the very last step, it will be properly
  3818. handled by do_deliver */
  3819. snprintf(src, sizeof(src), "LAGRQ-IAX/%s/%d", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr),fr.callno);
  3820. f.src = src;
  3821. f.mallocd = 0;
  3822. f.offset = 0;
  3823. fr.f = &f;
  3824. f.samples = 0;
  3825. schedule_delivery(iaxfrdup2(&fr, 0), 1);
  3826. #ifdef BRIDGE_OPTIMIZATION
  3827. }
  3828. #endif
  3829. break;
  3830. case AST_IAX_COMMAND_AUTHREQ:
  3831. if (iaxs[fr.callno]->state & (IAX_STATE_STARTED | IAX_STATE_TBD)) {
  3832. ast_log(LOG_WARNING, "Call on %s is already up, can't start on it\n", iaxs[fr.callno]->owner ? iaxs[fr.callno]->owner->name : "<Unknown>");
  3833. break;
  3834. }
  3835. ((char *)f.data)[f.datalen] = '\0';
  3836. if (authenticate_reply(iaxs[fr.callno], &iaxs[fr.callno]->addr, (char *)f.data, iaxs[fr.callno]->secret, iaxs[fr.callno]->outkey)) {
  3837. ast_log(LOG_WARNING,
  3838. "I don't know how to authenticate %s to %s\n",
  3839. (char *)f.data, ast_inet_ntoa(iabuf, sizeof(iabuf), iaxs[fr.callno]->addr.sin_addr));
  3840. }
  3841. break;
  3842. case AST_IAX_COMMAND_AUTHREP:
  3843. /* Ignore once we've started */
  3844. if (iaxs[fr.callno]->state & (IAX_STATE_STARTED | IAX_STATE_TBD)) {
  3845. ast_log(LOG_WARNING, "Call on %s is already up, can't start on it\n", iaxs[fr.callno]->owner ? iaxs[fr.callno]->owner->name : "<Unknown>");
  3846. break;
  3847. }
  3848. ((char *)f.data)[f.datalen] = '\0';
  3849. if (authenticate_verify(iaxs[fr.callno], (char *)f.data)) {
  3850. ast_log(LOG_NOTICE, "Host %s failed to authenticate as %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), iaxs[fr.callno]->addr.sin_addr), iaxs[fr.callno]->username);
  3851. send_command_final(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_REJECT, 0, "No authority found", strlen("No authority found"), -1);
  3852. break;
  3853. }
  3854. /* This might re-enter the IAX code and need the lock */
  3855. exists = ast_exists_extension(NULL, iaxs[fr.callno]->context, iaxs[fr.callno]->exten, 1, iaxs[fr.callno]->callerid);
  3856. if (strcmp(iaxs[fr.callno]->exten, "TBD") && !exists) {
  3857. ast_log(LOG_NOTICE, "Rejected connect attempt from %s, request '%s@%s' does not exist\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), iaxs[fr.callno]->exten, iaxs[fr.callno]->context);
  3858. send_command_final(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_REJECT, 0, "No such context/extension", strlen("No such context/extension"), -1);
  3859. } else {
  3860. /* Select an appropriate format */
  3861. format = iaxs[fr.callno]->peerformat & iax_capability;
  3862. if (!format) {
  3863. ast_log(LOG_DEBUG, "We don't do requested format %s, falling back to peer capability %d\n", ast_getformatname(iaxs[fr.callno]->peerformat), iaxs[fr.callno]->peercapability);
  3864. format = iaxs[fr.callno]->peercapability & iax_capability;
  3865. if (!format) {
  3866. ast_log(LOG_NOTICE, "Rejected connect attempt from %s, requested/capability 0x%x/0x%x incompatible with our capability 0x%x.\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), iaxs[fr.callno]->peerformat, iaxs[fr.callno]->peercapability, iax_capability);
  3867. send_command_final(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_REJECT, 0, "Unable to negotiate codec", strlen("Unable to negotiate codec"), -1);
  3868. } else {
  3869. /* Pick one... */
  3870. format = ast_best_codec(iaxs[fr.callno]->peercapability & iax_capability);
  3871. if (!format) {
  3872. ast_log(LOG_ERROR, "No best format in 0x%x???\n", iaxs[fr.callno]->peercapability & iax_capability);
  3873. ast_log(LOG_NOTICE, "Rejected connect attempt from %s, requested/capability 0x%x/0x%x incompatible with our capability 0x%x.\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), iaxs[fr.callno]->peerformat, iaxs[fr.callno]->peercapability, iax_capability);
  3874. send_command_final(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_REJECT, 0, "Unable to negotiate codec", strlen("Unable to negotiate codec"), -1);
  3875. }
  3876. }
  3877. }
  3878. if (format) {
  3879. /* Authentication received */
  3880. snprintf(rel1, sizeof(rel1), "formats=%d;", format);
  3881. send_command(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_ACCEPT, 0, rel1, strlen(rel1) + 1, -1);
  3882. if (strcmp(iaxs[fr.callno]->exten, "TBD")) {
  3883. iaxs[fr.callno]->state |= IAX_STATE_STARTED;
  3884. if (option_verbose > 2)
  3885. ast_verbose(VERBOSE_PREFIX_3 "Accepting AUTHENTICATED call from %s, requested format = %d, actual format = %d\n",
  3886. ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), iaxs[fr.callno]->peerformat,format);
  3887. iaxs[fr.callno]->state |= IAX_STATE_STARTED;
  3888. if(!(c = ast_iax_new(iaxs[fr.callno], AST_STATE_RING, format)))
  3889. iax_destroy_nolock(fr.callno);
  3890. } else {
  3891. iaxs[fr.callno]->state |= IAX_STATE_TBD;
  3892. /* If this is a TBD call, we're ready but now what... */
  3893. if (option_verbose > 2)
  3894. ast_verbose(VERBOSE_PREFIX_3 "Accepted AUTHENTICATED TBD call from %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr));
  3895. }
  3896. }
  3897. }
  3898. break;
  3899. case AST_IAX_COMMAND_DIAL:
  3900. ((char *)f.data)[f.datalen] = '\0';
  3901. if (iaxs[fr.callno]->state & IAX_STATE_TBD) {
  3902. iaxs[fr.callno]->state &= ~IAX_STATE_TBD;
  3903. strncpy(iaxs[fr.callno]->exten, (char *)f.data, sizeof(iaxs[fr.callno]->exten)-1);
  3904. if (!ast_exists_extension(NULL, iaxs[fr.callno]->context, iaxs[fr.callno]->exten, 1, iaxs[fr.callno]->callerid)) {
  3905. ast_log(LOG_NOTICE, "Rejected dial attempt from %s, request '%s@%s' does not exist\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), iaxs[fr.callno]->exten, iaxs[fr.callno]->context);
  3906. send_command_final(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_REJECT, 0, "No such context/extension", strlen("No such context/extension"), -1);
  3907. } else {
  3908. iaxs[fr.callno]->state |= IAX_STATE_STARTED;
  3909. if (option_verbose > 2)
  3910. ast_verbose(VERBOSE_PREFIX_3 "Accepting DIAL from %s, formats = 0x%x\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), iaxs[fr.callno]->peerformat);
  3911. iaxs[fr.callno]->state |= IAX_STATE_STARTED;
  3912. if(!(c = ast_iax_new(iaxs[fr.callno], AST_STATE_RING, iaxs[fr.callno]->peerformat)))
  3913. iax_destroy_nolock(fr.callno);
  3914. }
  3915. }
  3916. break;
  3917. case AST_IAX_COMMAND_INVAL:
  3918. iaxs[fr.callno]->error = ENOTCONN;
  3919. ast_log(LOG_DEBUG, "Immediately destroying %d, having received INVAL\n", fr.callno);
  3920. iax_destroy_nolock(fr.callno);
  3921. if (option_debug)
  3922. ast_log(LOG_DEBUG, "Destroying call %d\n", fr.callno);
  3923. break;
  3924. case AST_IAX_COMMAND_VNAK:
  3925. ast_log(LOG_DEBUG, "Sending VNAK\n");
  3926. /* Force retransmission */
  3927. vnak_retransmit(fr.callno, fr.seqno);
  3928. break;
  3929. case AST_IAX_COMMAND_REGREQ:
  3930. if (f.data)
  3931. ((char *)f.data)[f.datalen]='\0';
  3932. if (register_verify(fr.callno, &sin, (char *)f.data)) {
  3933. send_command_final(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_REGREJ, 0, "Registration Refused", strlen("Registration Refused"), -1);
  3934. break;
  3935. }
  3936. if ((!strlen(iaxs[fr.callno]->secret) && !strlen(iaxs[fr.callno]->inkeys)) || (iaxs[fr.callno]->state & IAX_STATE_AUTHENTICATED)) {
  3937. if (update_registry(iaxs[fr.callno]->peer, &sin, fr.callno))
  3938. ast_log(LOG_WARNING, "Registry error\n");
  3939. break;
  3940. }
  3941. registry_authrequest(iaxs[fr.callno]->peer, fr.callno);
  3942. break;
  3943. case AST_IAX_COMMAND_REGACK:
  3944. if (f.data)
  3945. ((char *)f.data)[f.datalen] = '\0';
  3946. if (iax_ack_registry(f.data, &sin, fr.callno))
  3947. ast_log(LOG_WARNING, "Registration failure\n");
  3948. iax_destroy_nolock(fr.callno);
  3949. break;
  3950. case AST_IAX_COMMAND_REGREJ:
  3951. if (f.data)
  3952. ((char *)f.data)[f.datalen] = '\0';
  3953. if (iaxs[fr.callno]->reg) {
  3954. ast_log(LOG_NOTICE, "Registration of '%s' rejected: %s\n", iaxs[fr.callno]->reg->username, (char *)f.data);
  3955. iaxs[fr.callno]->reg->regstate = REG_STATE_REJECTED;
  3956. }
  3957. iax_destroy_nolock(fr.callno);
  3958. break;
  3959. case AST_IAX_COMMAND_REGAUTH:
  3960. /* Authentication request */
  3961. if (f.data)
  3962. ((char *)f.data)[f.datalen] = '\0';
  3963. if (registry_rerequest(f.data, fr.callno, &sin))
  3964. send_command_final(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_REJECT, 0, "No authority found", strlen("No authority found"), -1);
  3965. break;
  3966. case AST_IAX_COMMAND_TXREJ:
  3967. iaxs[fr.callno]->transferring = 0;
  3968. if (option_verbose > 2)
  3969. ast_verbose(VERBOSE_PREFIX_3 "Channel '%s' unable to transfer\n", iaxs[fr.callno]->owner ? iaxs[fr.callno]->owner->name : "<Unknown>");
  3970. memset(&iaxs[fr.callno]->transfer, 0, sizeof(iaxs[fr.callno]->transfer));
  3971. if (iaxs[fr.callno]->bridgecallno > -1) {
  3972. if (iaxs[iaxs[fr.callno]->bridgecallno]->transferring) {
  3973. iaxs[iaxs[fr.callno]->bridgecallno]->transferring = 0;
  3974. send_command(iaxs[iaxs[fr.callno]->bridgecallno], AST_FRAME_IAX, AST_IAX_COMMAND_TXREJ, 0, NULL, 0, -1);
  3975. }
  3976. }
  3977. break;
  3978. case AST_IAX_COMMAND_TXREADY:
  3979. if (iaxs[fr.callno]->transferring == TRANSFER_BEGIN) {
  3980. iaxs[fr.callno]->transferring = TRANSFER_READY;
  3981. if (option_verbose > 2)
  3982. ast_verbose(VERBOSE_PREFIX_3 "Channel '%s' ready to transfer\n", iaxs[fr.callno]->owner ? iaxs[fr.callno]->owner->name : "<Unknown>");
  3983. if (iaxs[fr.callno]->bridgecallno > -1) {
  3984. if (iaxs[iaxs[fr.callno]->bridgecallno]->transferring == TRANSFER_READY) {
  3985. if (option_verbose > 2)
  3986. ast_verbose(VERBOSE_PREFIX_3 "Releasing %s and %s\n", iaxs[fr.callno]->owner ? iaxs[fr.callno]->owner->name : "<Unknown>",
  3987. iaxs[iaxs[fr.callno]->bridgecallno]->owner ? iaxs[iaxs[fr.callno]->bridgecallno]->owner->name : "<Unknown>");
  3988. /* They're both ready, now release them. */
  3989. iaxs[iaxs[fr.callno]->bridgecallno]->transferring = TRANSFER_RELEASED;
  3990. iaxs[fr.callno]->transferring = TRANSFER_RELEASED;
  3991. iaxs[iaxs[fr.callno]->bridgecallno]->alreadygone = 1;
  3992. iaxs[fr.callno]->alreadygone = 1;
  3993. /* Stop doing lag & ping requests */
  3994. stop_stuff(fr.callno);
  3995. stop_stuff(iaxs[fr.callno]->bridgecallno);
  3996. /* Send the release message */
  3997. snprintf(rel0, sizeof(rel0), "peercallno=%d;", iaxs[iaxs[fr.callno]->bridgecallno]->peercallno);
  3998. snprintf(rel1, sizeof(rel1), "peercallno=%d;", iaxs[fr.callno]->peercallno);
  3999. send_command(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_TXREL, 0, rel0, strlen(rel0)+1, -1);
  4000. send_command(iaxs[iaxs[fr.callno]->bridgecallno], AST_FRAME_IAX, AST_IAX_COMMAND_TXREL, 0, rel1, strlen(rel1)+1, -1);
  4001. }
  4002. }
  4003. }
  4004. break;
  4005. case AST_IAX_COMMAND_TXREQ:
  4006. if (f.data)
  4007. ((char *)f.data)[f.datalen] = '\0';
  4008. try_transfer(iaxs[fr.callno], (char *)f.data);
  4009. break;
  4010. case AST_IAX_COMMAND_TXCNT:
  4011. if (iaxs[fr.callno]->transferring)
  4012. send_command_transfer(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_TXACC, 0, NULL, 0);
  4013. break;
  4014. case AST_IAX_COMMAND_TXREL:
  4015. if (f.data)
  4016. ((char *)f.data)[f.datalen] = '\0';
  4017. complete_transfer(fr.callno, (char *)f.data);
  4018. break;
  4019. case AST_IAX_COMMAND_DPREP:
  4020. if (f.data)
  4021. ((char *)f.data)[f.datalen] = '\0';
  4022. complete_dpreply(iaxs[fr.callno], (char *)f.data);
  4023. break;
  4024. default:
  4025. ast_log(LOG_DEBUG, "Unknown IAX command %d on %d/%d\n", f.subclass, fr.callno, iaxs[fr.callno]->peercallno);
  4026. }
  4027. /* Don't actually pass these frames along */
  4028. ast_mutex_unlock(&iaxsl[fr.callno]);
  4029. return 1;
  4030. }
  4031. } else {
  4032. /* A mini frame */
  4033. f.frametype = AST_FRAME_VOICE;
  4034. if (iaxs[fr.callno]->voiceformat > 0)
  4035. f.subclass = iaxs[fr.callno]->voiceformat;
  4036. else {
  4037. ast_log(LOG_WARNING, "Received mini frame before first full voice frame\n ");
  4038. iax_vnak(fr.callno);
  4039. ast_mutex_unlock(&iaxsl[fr.callno]);
  4040. return 1;
  4041. }
  4042. f.datalen = res - sizeof(struct ast_iax_mini_hdr);
  4043. if (f.datalen < 0) {
  4044. ast_log(LOG_WARNING, "Datalen < 0?\n");
  4045. ast_mutex_unlock(&iaxsl[fr.callno]);
  4046. return 1;
  4047. }
  4048. if (f.datalen)
  4049. f.data = buf + sizeof(struct ast_iax_mini_hdr);
  4050. else
  4051. f.data = NULL;
  4052. fr.ts = (iaxs[fr.callno]->last & 0xFFFF0000L) | ntohs(mh->ts);
  4053. }
  4054. /* Don't pass any packets until we're started */
  4055. if (!(iaxs[fr.callno]->state & IAX_STATE_STARTED)) {
  4056. ast_mutex_unlock(&iaxsl[fr.callno]);
  4057. return 1;
  4058. }
  4059. /* Common things */
  4060. snprintf(src, sizeof(src), "IAX/%s/%d", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr),fr.callno); f.src = src;
  4061. f.mallocd = 0;
  4062. f.offset = 0;
  4063. fr.f = &f;
  4064. if (f.datalen && (f.frametype == AST_FRAME_VOICE))
  4065. f.samples = get_samples(&f);
  4066. else
  4067. f.samples = 0;
  4068. /* If this is our most recent packet, use it as our basis for timestamping */
  4069. if (iaxs[fr.callno]->last < fr.ts) {
  4070. iaxs[fr.callno]->last = fr.ts;
  4071. fr.outoforder = 0;
  4072. } else {
  4073. if (option_debug)
  4074. ast_log(LOG_DEBUG, "Received out of order packet... (type=%d, subclass %d, ts = %d, last = %d)\n", f.frametype, f.subclass, fr.ts, iaxs[fr.callno]->last);
  4075. fr.outoforder = -1;
  4076. }
  4077. #ifdef BRIDGE_OPTIMIZATION
  4078. if (iaxs[fr.callno]->bridgecallno > -1) {
  4079. forward_delivery(&fr);
  4080. } else {
  4081. schedule_delivery(iaxfrdup2(&fr, 0), 1);
  4082. }
  4083. #else
  4084. schedule_delivery(iaxfrdup2(&fr, 0), 1);
  4085. #endif
  4086. /* Always run again */
  4087. ast_mutex_unlock(&iaxsl[fr.callno]);
  4088. return 1;
  4089. }
  4090. static int iax_do_register(struct iax_registry *reg)
  4091. {
  4092. char requeststr[256] = "";
  4093. if (option_debug)
  4094. ast_log(LOG_DEBUG, "Sending registration request for '%s'\n", reg->username);
  4095. if (reg->callno < 0) {
  4096. if (option_debug)
  4097. ast_log(LOG_DEBUG, "Allocate call number\n");
  4098. reg->callno = find_callno(-1, -1, &reg->addr, NEW_FORCE);
  4099. if (reg->callno < 0) {
  4100. ast_log(LOG_WARNING, "Unable to create call for registration\n");
  4101. return -1;
  4102. } else if (option_debug)
  4103. ast_log(LOG_DEBUG, "Registration created on call %d\n", reg->callno);
  4104. iaxs[reg->callno]->reg = reg;
  4105. }
  4106. /* Schedule the next registration attempt */
  4107. if (reg->expire > -1)
  4108. ast_sched_del(sched, reg->expire);
  4109. /* Setup the registration a little early */
  4110. reg->expire = ast_sched_add(sched, (5 * reg->refresh / 6) * 1000, iax_do_register_s, reg);
  4111. /* Send the request */
  4112. MYSNPRINTF "peer=%s;refresh=%d;", reg->username, reg->refresh);
  4113. send_command(iaxs[reg->callno],AST_FRAME_IAX, AST_IAX_COMMAND_REGREQ, 0, requeststr, strlen(requeststr) + 1, -1);
  4114. reg->regstate = REG_STATE_REGSENT;
  4115. return 0;
  4116. }
  4117. static int iax_poke_noanswer(void *data)
  4118. {
  4119. struct iax_peer *peer = data;
  4120. peer->pokeexpire = -1;
  4121. if (peer->lastms > -1)
  4122. ast_log(LOG_NOTICE, "Peer '%s' is now UNREACHABLE!\n", peer->name);
  4123. if (peer->callno > 0)
  4124. iax_destroy(peer->callno);
  4125. peer->callno = 0;
  4126. peer->lastms = -1;
  4127. /* Try again quickly */
  4128. peer->pokeexpire = ast_sched_add(sched, DEFAULT_FREQ_NOTOK, iax_poke_peer_s, peer);
  4129. return 0;
  4130. }
  4131. static int iax_poke_peer(struct iax_peer *peer)
  4132. {
  4133. if (!peer->maxms || !peer->addr.sin_addr.s_addr) {
  4134. /* IF we have no IP, or this isn't to be monitored, return
  4135. imeediately after clearing things out */
  4136. peer->lastms = 0;
  4137. peer->pokeexpire = -1;
  4138. peer->callno = 0;
  4139. return 0;
  4140. }
  4141. if (peer->callno > 0) {
  4142. ast_log(LOG_NOTICE, "Still have a callno...\n");
  4143. iax_destroy(peer->callno);
  4144. }
  4145. peer->callno = find_callno(-1, -1, &peer->addr, NEW_FORCE);
  4146. if (peer->callno < 0) {
  4147. ast_log(LOG_WARNING, "Unable to allocate call for poking peer '%s'\n", peer->name);
  4148. return -1;
  4149. }
  4150. if (peer->pokeexpire > -1)
  4151. ast_sched_del(sched, peer->pokeexpire);
  4152. /* Speed up retransmission times */
  4153. iaxs[peer->callno]->pingtime = peer->maxms / 4 + 1;
  4154. iaxs[peer->callno]->peerpoke = peer;
  4155. send_command(iaxs[peer->callno], AST_FRAME_IAX, AST_IAX_COMMAND_POKE, 0, NULL, 0, -1);
  4156. peer->pokeexpire = ast_sched_add(sched, DEFAULT_MAXMS * 2, iax_poke_noanswer, peer);
  4157. return 0;
  4158. }
  4159. static void free_context(struct iax_context *con)
  4160. {
  4161. struct iax_context *conl;
  4162. while(con) {
  4163. conl = con;
  4164. con = con->next;
  4165. free(conl);
  4166. }
  4167. }
  4168. static struct ast_channel *iax_request(char *type, int format, void *data)
  4169. {
  4170. int callno;
  4171. int res;
  4172. int sendani;
  4173. int maxtime;
  4174. int fmt, native;
  4175. struct sockaddr_in sin;
  4176. char s[256];
  4177. char *st;
  4178. struct ast_channel *c;
  4179. char *stringp=NULL;
  4180. int capability = iax_capability;
  4181. strncpy(s, (char *)data, sizeof(s)-1);
  4182. /* FIXME The next two lines seem useless */
  4183. stringp=s;
  4184. strsep(&stringp, "/");
  4185. stringp=s;
  4186. strsep(&stringp, "@");
  4187. st = strsep(&stringp, "@");
  4188. if (!st)
  4189. st = s;
  4190. /* Populate our address from the given */
  4191. if (create_addr(&sin, &capability, &sendani, &maxtime, st, NULL)) {
  4192. return NULL;
  4193. }
  4194. callno = find_callno(-1, -1, &sin, NEW_FORCE);
  4195. if (callno < 0) {
  4196. ast_log(LOG_WARNING, "Unable to create call\n");
  4197. return NULL;
  4198. }
  4199. ast_mutex_lock(&iaxsl[callno]);
  4200. /* Keep track of sendani flag */
  4201. iaxs[callno]->sendani = sendani;
  4202. iaxs[callno]->maxtime = maxtime;
  4203. c = ast_iax_new(iaxs[callno], AST_STATE_DOWN, capability);
  4204. ast_mutex_unlock(&iaxsl[callno]);
  4205. if (c) {
  4206. /* Choose a format we can live with */
  4207. if (c->nativeformats & format)
  4208. c->nativeformats &= format;
  4209. else {
  4210. native = c->nativeformats;
  4211. fmt = format;
  4212. res = ast_translator_best_choice(&fmt, &native);
  4213. if (res < 0) {
  4214. ast_log(LOG_WARNING, "Unable to create translator path for %s to %s on %s\n", ast_getformatname(c->nativeformats), ast_getformatname(fmt), c->name);
  4215. ast_hangup(c);
  4216. return NULL;
  4217. }
  4218. c->nativeformats = native;
  4219. }
  4220. }
  4221. return c;
  4222. }
  4223. static void *network_thread(void *ignore)
  4224. {
  4225. /* Our job is simple: Send queued messages, retrying if necessary. Read frames
  4226. from the network, and queue them for delivery to the channels */
  4227. int res;
  4228. struct ast_iax_frame *f, *freeme;
  4229. /* Establish I/O callback for socket read */
  4230. ast_io_add(io, netsocket, socket_read, AST_IO_IN, NULL);
  4231. for(;;) {
  4232. /* Go through the queue, sending messages which have not yet been
  4233. sent, and scheduling retransmissions if appropriate */
  4234. ast_mutex_lock(&iaxq.lock);
  4235. f = iaxq.head;
  4236. while(f) {
  4237. freeme = NULL;
  4238. if (!f->sentyet) {
  4239. f->sentyet++;
  4240. /* Send a copy immediately -- errors here are ok, so don't bother locking */
  4241. if (iaxs[f->callno]) {
  4242. send_packet(f);
  4243. }
  4244. if (f->retries < 0) {
  4245. /* This is not supposed to be retransmitted */
  4246. if (f->prev)
  4247. f->prev->next = f->next;
  4248. else
  4249. iaxq.head = f->next;
  4250. if (f->next)
  4251. f->next->prev = f->prev;
  4252. else
  4253. iaxq.tail = f->prev;
  4254. iaxq.count--;
  4255. /* Free the frame */
  4256. ast_frfree(f->f);
  4257. f->f = NULL;
  4258. /* Free the iax frame */
  4259. freeme = f;
  4260. } else {
  4261. /* We need reliable delivery. Schedule a retransmission */
  4262. f->retries++;
  4263. f->retrans = ast_sched_add(sched, f->retrytime, attempt_transmit, f);
  4264. }
  4265. }
  4266. f = f->next;
  4267. if (freeme)
  4268. ast_iax_frame_free(freeme);
  4269. }
  4270. ast_mutex_unlock(&iaxq.lock);
  4271. res = ast_sched_wait(sched);
  4272. if ((res > 1000) || (res < 0))
  4273. res = 1000;
  4274. res = ast_io_wait(io, res);
  4275. if (res >= 0) {
  4276. ast_sched_runq(sched);
  4277. }
  4278. }
  4279. return NULL;
  4280. }
  4281. static int start_network_thread(void)
  4282. {
  4283. return ast_pthread_create(&netthreadid, NULL, network_thread, NULL);
  4284. }
  4285. static struct iax_context *build_context(char *context)
  4286. {
  4287. struct iax_context *con = malloc(sizeof(struct iax_context));
  4288. if (con) {
  4289. strncpy(con->context, context, sizeof(con->context)-1);
  4290. con->next = NULL;
  4291. }
  4292. return con;
  4293. }
  4294. static struct iax_peer *build_peer(char *name, struct ast_variable *v)
  4295. {
  4296. struct iax_peer *peer;
  4297. struct iax_peer *prev;
  4298. int maskfound=0;
  4299. int format;
  4300. int found=0;
  4301. prev = NULL;
  4302. ast_mutex_lock(&peerl.lock);
  4303. peer = peerl.peers;
  4304. while(peer) {
  4305. if (!strcasecmp(peer->name, name)) {
  4306. break;
  4307. }
  4308. prev = peer;
  4309. peer = peer->next;
  4310. }
  4311. if (peer) {
  4312. found++;
  4313. /* Already in the list, remove it and it will be added back (or FREE'd) */
  4314. if (prev) {
  4315. prev->next = peer->next;
  4316. } else {
  4317. peerl.peers = peer->next;
  4318. }
  4319. ast_mutex_unlock(&peerl.lock);
  4320. } else {
  4321. ast_mutex_unlock(&peerl.lock);
  4322. peer = malloc(sizeof(struct iax_peer));
  4323. memset(peer, 0, sizeof(struct iax_peer));
  4324. peer->expire = -1;
  4325. peer->pokeexpire = -1;
  4326. }
  4327. if (peer) {
  4328. if (!found) {
  4329. strncpy(peer->name, name, sizeof(peer->name)-1);
  4330. peer->addr.sin_port = htons(AST_DEFAULT_IAX_PORTNO);
  4331. peer->expirey = expirey;
  4332. }
  4333. peer->capability = iax_capability;
  4334. while(v) {
  4335. if (!strcasecmp(v->name, "secret"))
  4336. strncpy(peer->secret, v->value, sizeof(peer->secret)-1);
  4337. else if (!strcasecmp(v->name, "auth"))
  4338. strncpy(peer->methods, v->value, sizeof(peer->methods)-1);
  4339. else if (!strcasecmp(v->name, "host")) {
  4340. if (!strcasecmp(v->value, "dynamic")) {
  4341. /* They'll register with us */
  4342. peer->dynamic = 1;
  4343. if (!found) {
  4344. /* Initialize stuff iff we're not found, otherwise
  4345. we keep going with what we had */
  4346. memset(&peer->addr.sin_addr, 0, 4);
  4347. if (peer->addr.sin_port) {
  4348. /* If we've already got a port, make it the default rather than absolute */
  4349. peer->defaddr.sin_port = peer->addr.sin_port;
  4350. peer->addr.sin_port = 0;
  4351. }
  4352. }
  4353. } else {
  4354. /* Non-dynamic. Make sure we become that way if we're not */
  4355. if (peer->expire > -1)
  4356. ast_sched_del(sched, peer->expire);
  4357. peer->expire = -1;
  4358. peer->dynamic = 0;
  4359. if (ast_get_ip(&peer->addr, v->value)) {
  4360. free(peer);
  4361. return NULL;
  4362. }
  4363. }
  4364. if (!maskfound)
  4365. inet_aton("255.255.255.255", &peer->mask);
  4366. } else if (!strcasecmp(v->name, "defaultip")) {
  4367. if (ast_get_ip(&peer->defaddr, v->value)) {
  4368. free(peer);
  4369. return NULL;
  4370. }
  4371. } else if (!strcasecmp(v->name, "permit") ||
  4372. !strcasecmp(v->name, "deny")) {
  4373. peer->ha = ast_append_ha(v->name, v->value, peer->ha);
  4374. } else if (!strcasecmp(v->name, "mask")) {
  4375. maskfound++;
  4376. inet_aton(v->value, &peer->mask);
  4377. } else if (!strcasecmp(v->name, "context")) {
  4378. if (!strlen(peer->context))
  4379. strncpy(peer->context, v->value, sizeof(peer->context) - 1);
  4380. } else if (!strcasecmp(v->name, "port")) {
  4381. if (peer->dynamic)
  4382. peer->defaddr.sin_port = htons(atoi(v->value));
  4383. else
  4384. peer->addr.sin_port = htons(atoi(v->value));
  4385. } else if (!strcasecmp(v->name, "username")) {
  4386. strncpy(peer->username, v->value, sizeof(peer->username)-1);
  4387. } else if (!strcasecmp(v->name, "allow")) {
  4388. format = ast_getformatbyname(v->value);
  4389. if (format < 1)
  4390. ast_log(LOG_WARNING, "Cannot allow unknown format '%s'\n", v->value);
  4391. else
  4392. peer->capability |= format;
  4393. } else if (!strcasecmp(v->name, "disallow")) {
  4394. format = ast_getformatbyname(v->value);
  4395. if (format < 1)
  4396. ast_log(LOG_WARNING, "Cannot disallow unknown format '%s'\n", v->value);
  4397. else
  4398. peer->capability &= ~format;
  4399. } else if (!strcasecmp(v->name, "callerid")) {
  4400. strncpy(peer->callerid, v->value, sizeof(peer->callerid)-1);
  4401. peer->hascallerid=1;
  4402. } else if (!strcasecmp(v->name, "sendani")) {
  4403. peer->sendani = ast_true(v->value);
  4404. } else if (!strcasecmp(v->name, "inkeys")) {
  4405. strncpy(peer->inkeys, v->value, sizeof(peer->inkeys) - 1);
  4406. } else if (!strcasecmp(v->name, "outkey")) {
  4407. strncpy(peer->outkey, v->value, sizeof(peer->outkey) - 1);
  4408. } else if (!strcasecmp(v->name, "qualify")) {
  4409. if (!strcasecmp(v->value, "no")) {
  4410. peer->maxms = 0;
  4411. } else if (!strcasecmp(v->value, "yes")) {
  4412. peer->maxms = DEFAULT_MAXMS;
  4413. } else if (sscanf(v->value, "%d", &peer->maxms) != 1) {
  4414. ast_log(LOG_WARNING, "Qualification of peer '%s' should be 'yes', 'no', or a number of milliseconds at line %d of " IAX_CONF_FILE "\n", peer->name, v->lineno);
  4415. peer->maxms = 0;
  4416. }
  4417. } //else if (strcasecmp(v->name,"type"))
  4418. // ast_log(LOG_WARNING, "Ignoring %s\n", v->name);
  4419. v=v->next;
  4420. }
  4421. if (!strlen(peer->methods))
  4422. strncpy(peer->methods, "md5,plaintext", sizeof(peer->methods) - 1);
  4423. peer->delme = 0;
  4424. }
  4425. return peer;
  4426. }
  4427. static struct iax_user *build_user(char *name, struct ast_variable *v)
  4428. {
  4429. struct iax_user *user;
  4430. struct iax_context *con, *conl = NULL;
  4431. int format;
  4432. user = (struct iax_user *)malloc(sizeof(struct iax_user));
  4433. if (user) {
  4434. memset(user, 0, sizeof(struct iax_user));
  4435. strncpy(user->name, name, sizeof(user->name)-1);
  4436. while(v) {
  4437. if (!strcasecmp(v->name, "context")) {
  4438. con = build_context(v->value);
  4439. if (con) {
  4440. if (conl)
  4441. conl->next = con;
  4442. else
  4443. user->contexts = con;
  4444. conl = con;
  4445. }
  4446. } else if (!strcasecmp(v->name, "permit") ||
  4447. !strcasecmp(v->name, "deny")) {
  4448. user->ha = ast_append_ha(v->name, v->value, user->ha);
  4449. } else if (!strcasecmp(v->name, "auth")) {
  4450. strncpy(user->methods, v->value, sizeof(user->methods)-1);
  4451. } else if (!strcasecmp(v->name, "secret")) {
  4452. strncpy(user->secret, v->value, sizeof(user->secret)-1);
  4453. } else if (!strcasecmp(v->name, "callerid")) {
  4454. strncpy(user->callerid, v->value, sizeof(user->callerid)-1);
  4455. user->hascallerid=1;
  4456. } else if (!strcasecmp(v->name, "accountcode")) {
  4457. strncpy(user->accountcode, v->value, sizeof(user->accountcode)-1);
  4458. } else if (!strcasecmp(v->name, "amaflags")) {
  4459. format = ast_cdr_amaflags2int(v->value);
  4460. if (format < 0) {
  4461. ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
  4462. } else {
  4463. user->amaflags = format;
  4464. }
  4465. } else if (!strcasecmp(v->name, "inkeys")) {
  4466. strncpy(user->inkeys, v->value, sizeof(user->inkeys) - 1);
  4467. } //else if (strcasecmp(v->name,"type"))
  4468. // ast_log(LOG_WARNING, "Ignoring %s\n", v->name);
  4469. v = v->next;
  4470. }
  4471. }
  4472. if (!strlen(user->methods)) {
  4473. if (strlen(user->secret)) {
  4474. strncpy(user->methods, "md5,plaintext", sizeof(user->methods) - 1);
  4475. if (strlen(user->inkeys))
  4476. strncat(user->methods, ",rsa", sizeof(user->methods) - 1);
  4477. } else if (strlen(user->inkeys)) {
  4478. strncpy(user->methods, "rsa", sizeof(user->methods) - 1);
  4479. } else
  4480. strncpy(user->methods, "md5,plaintext", sizeof(user->methods) -1);
  4481. }
  4482. return user;
  4483. }
  4484. static void delete_users(void){
  4485. struct iax_user *user, *userlast;
  4486. struct iax_peer *peer;
  4487. struct iax_registry *reg, *regl;
  4488. /* Delete all users */
  4489. ast_mutex_lock(&userl.lock);
  4490. for (user=userl.users;user;) {
  4491. ast_free_ha(user->ha);
  4492. free_context(user->contexts);
  4493. userlast = user;
  4494. user=user->next;
  4495. free(userlast);
  4496. }
  4497. userl.users=NULL;
  4498. ast_mutex_unlock(&userl.lock);
  4499. for (reg = registrations;reg;) {
  4500. regl = reg;
  4501. reg = reg->next;
  4502. if (regl->expire > -1)
  4503. ast_sched_del(sched, regl->expire);
  4504. free(regl);
  4505. }
  4506. registrations = NULL;
  4507. ast_mutex_lock(&peerl.lock);
  4508. for (peer=peerl.peers;peer;) {
  4509. /* Assume all will be deleted, and we'll find out for sure later */
  4510. peer->delme = 1;
  4511. peer = peer->next;
  4512. }
  4513. ast_mutex_unlock(&peerl.lock);
  4514. }
  4515. static void prune_peers(void){
  4516. /* Prune peers who still are supposed to be deleted */
  4517. struct iax_peer *peer, *peerlast, *peernext;
  4518. int x;
  4519. ast_mutex_lock(&peerl.lock);
  4520. peerlast = NULL;
  4521. for (peer=peerl.peers;peer;) {
  4522. peernext = peer->next;
  4523. if (peer->delme) {
  4524. for (x=0;x<AST_IAX_MAX_CALLS;x++) {
  4525. ast_mutex_lock(&iaxsl[x]);
  4526. if (iaxs[x] && (iaxs[x]->peerpoke == peer)) {
  4527. iax_destroy(x);
  4528. }
  4529. ast_mutex_unlock(&iaxsl[x]);
  4530. }
  4531. /* Delete it, it needs to disappear */
  4532. if (peer->expire > -1)
  4533. ast_sched_del(sched, peer->expire);
  4534. if (peer->pokeexpire > -1)
  4535. ast_sched_del(sched, peer->pokeexpire);
  4536. if (peer->callno > 0)
  4537. iax_destroy(peer->callno);
  4538. free(peer);
  4539. if (peerlast)
  4540. peerlast->next = peernext;
  4541. else
  4542. peerl.peers = peernext;
  4543. } else
  4544. peerlast = peer;
  4545. peer=peernext;
  4546. }
  4547. ast_mutex_unlock(&peerl.lock);
  4548. }
  4549. static int set_config(char *config_file, struct sockaddr_in* sin){
  4550. struct ast_config *cfg;
  4551. int capability=iax_capability;
  4552. struct ast_variable *v;
  4553. char *cat;
  4554. char *utype;
  4555. int format;
  4556. struct iax_user *user;
  4557. struct iax_peer *peer;
  4558. static unsigned short int last_port=0;
  4559. cfg = ast_load(config_file);
  4560. if (!cfg) {
  4561. ast_log(LOG_ERROR, "Unable to load config %s\n", config_file);
  4562. return -1;
  4563. }
  4564. v = ast_variable_browse(cfg, "general");
  4565. while(v) {
  4566. if (!strcasecmp(v->name, "port")){
  4567. sin->sin_port = ntohs(atoi(v->value));
  4568. if(last_port==0){
  4569. last_port=sin->sin_port;
  4570. #if 0
  4571. ast_verbose("setting last port\n");
  4572. #endif
  4573. }
  4574. else if(sin->sin_port != last_port)
  4575. ast_log(LOG_WARNING, "change to port ignored until next asterisk re-start\n");
  4576. }
  4577. else if (!strcasecmp(v->name, "pingtime"))
  4578. ping_time = atoi(v->value);
  4579. else if (!strcasecmp(v->name, "maxjitterbuffer"))
  4580. maxjitterbuffer = atoi(v->value);
  4581. else if (!strcasecmp(v->name, "maxexcessbuffer"))
  4582. max_jitter_buffer = atoi(v->value);
  4583. else if (!strcasecmp(v->name, "lagrqtime"))
  4584. lagrq_time = atoi(v->value);
  4585. else if (!strcasecmp(v->name, "dropcount"))
  4586. iax_dropcount = atoi(v->value);
  4587. else if (!strcasecmp(v->name, "bindaddr"))
  4588. inet_aton(v->value, &sin->sin_addr);
  4589. else if (!strcasecmp(v->name, "jitterbuffer"))
  4590. use_jitterbuffer = ast_true(v->value);
  4591. else if (!strcasecmp(v->name, "bandwidth")) {
  4592. if (!strcasecmp(v->value, "low")) {
  4593. capability = IAX_CAPABILITY_LOWBANDWIDTH;
  4594. } else if (!strcasecmp(v->value, "medium")) {
  4595. capability = IAX_CAPABILITY_MEDBANDWIDTH;
  4596. } else if (!strcasecmp(v->value, "high")) {
  4597. capability = IAX_CAPABILITY_FULLBANDWIDTH;
  4598. } else
  4599. ast_log(LOG_WARNING, "bandwidth must be either low, medium, or high\n");
  4600. } else if (!strcasecmp(v->name, "allow")) {
  4601. format = ast_getformatbyname(v->value);
  4602. if (format < 1)
  4603. ast_log(LOG_WARNING, "Cannot allow unknown format '%s'\n", v->value);
  4604. else
  4605. capability |= format;
  4606. } else if (!strcasecmp(v->name, "disallow")) {
  4607. format = ast_getformatbyname(v->value);
  4608. if (format < 1)
  4609. ast_log(LOG_WARNING, "Cannot disallow unknown format '%s'\n", v->value);
  4610. else
  4611. capability &= ~format;
  4612. } else if (!strcasecmp(v->name, "register")) {
  4613. iax_register(v->value, v->lineno);
  4614. } else if (!strcasecmp(v->name, "tos")) {
  4615. if (sscanf(v->value, "%i", &format) == 1)
  4616. tos = format & 0xff;
  4617. else if (!strcasecmp(v->value, "lowdelay"))
  4618. tos = IPTOS_LOWDELAY;
  4619. else if (!strcasecmp(v->value, "throughput"))
  4620. tos = IPTOS_THROUGHPUT;
  4621. else if (!strcasecmp(v->value, "reliability"))
  4622. tos = IPTOS_RELIABILITY;
  4623. else if (!strcasecmp(v->value, "mincost"))
  4624. tos = IPTOS_MINCOST;
  4625. else if (!strcasecmp(v->value, "none"))
  4626. tos = 0;
  4627. else
  4628. ast_log(LOG_WARNING, "Invalid tos value at line %d, should be 'lowdelay', 'throughput', 'reliability', 'mincost', or 'none'\n", v->lineno);
  4629. } else if (!strcasecmp(v->name, "accountcode")) {
  4630. strncpy(accountcode, v->value, sizeof(accountcode)-1);
  4631. } else if (!strcasecmp(v->name, "amaflags")) {
  4632. format = ast_cdr_amaflags2int(v->value);
  4633. if (format < 0) {
  4634. ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
  4635. } else {
  4636. amaflags = format;
  4637. }
  4638. #ifdef MYSQL_FRIENDS
  4639. } else if (!strcasecmp(v->name, "dbuser")) {
  4640. strncpy(mydbuser, v->value, sizeof(mydbuser) - 1);
  4641. } else if (!strcasecmp(v->name, "dbpass")) {
  4642. strncpy(mydbpass, v->value, sizeof(mydbpass) - 1);
  4643. } else if (!strcasecmp(v->name, "dbhost")) {
  4644. strncpy(mydbhost, v->value, sizeof(mydbhost) - 1);
  4645. } else if (!strcasecmp(v->name, "dbname")) {
  4646. strncpy(mydbname, v->value, sizeof(mydbname) - 1);
  4647. #endif
  4648. } //else if (strcasecmp(v->name,"type"))
  4649. // ast_log(LOG_WARNING, "Ignoring %s\n", v->name);
  4650. v = v->next;
  4651. }
  4652. iax_capability = capability;
  4653. cat = ast_category_browse(cfg, NULL);
  4654. while(cat) {
  4655. if (strcasecmp(cat, "general")) {
  4656. utype = ast_variable_retrieve(cfg, cat, "type");
  4657. if (utype) {
  4658. if (!strcasecmp(utype, "user") || !strcasecmp(utype, "friend")) {
  4659. user = build_user(cat, ast_variable_browse(cfg, cat));
  4660. if (user) {
  4661. ast_mutex_lock(&userl.lock);
  4662. user->next = userl.users;
  4663. userl.users = user;
  4664. ast_mutex_unlock(&userl.lock);
  4665. }
  4666. }
  4667. if (!strcasecmp(utype, "peer") || !strcasecmp(utype, "friend")) {
  4668. peer = build_peer(cat, ast_variable_browse(cfg, cat));
  4669. if (peer) {
  4670. ast_mutex_lock(&peerl.lock);
  4671. peer->next = peerl.peers;
  4672. peerl.peers = peer;
  4673. ast_mutex_unlock(&peerl.lock);
  4674. }
  4675. } else if (strcasecmp(utype, "user")) {
  4676. ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config_file);
  4677. }
  4678. } else
  4679. ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
  4680. }
  4681. cat = ast_category_browse(cfg, cat);
  4682. }
  4683. ast_destroy(cfg);
  4684. #ifdef MYSQL_FRIENDS
  4685. /* Connect to db if appropriate */
  4686. if (!mysql && strlen(mydbname)) {
  4687. mysql = mysql_init(NULL);
  4688. if (!mysql_real_connect(mysql, mydbhost[0] ? mydbhost : NULL, mydbuser, mydbpass, mydbname, 0, NULL, 0)) {
  4689. memset(mydbpass, '*', strlen(mydbpass));
  4690. ast_log(LOG_WARNING, "Database connection failed (db=%s, host=%s, user=%s, pass=%s)!\n",
  4691. mydbname, mydbhost, mydbuser, mydbpass);
  4692. free(mysql);
  4693. mysql = NULL;
  4694. } else
  4695. ast_verbose(VERBOSE_PREFIX_1 "Connected to database '%s' on '%s' as '%s'\n",
  4696. mydbname, mydbhost, mydbuser);
  4697. }
  4698. #endif
  4699. return capability;
  4700. }
  4701. static int reload_config(void)
  4702. {
  4703. char *config = IAX_CONF_FILE;
  4704. struct iax_registry *reg;
  4705. struct sockaddr_in dead_sin;
  4706. strncpy(accountcode, "", sizeof(accountcode)-1);
  4707. amaflags = 0;
  4708. srand(time(NULL));
  4709. delete_users();
  4710. if (set_config(config,&dead_sin) == -1) {
  4711. config = IAX_OLD_CONF_FILE;
  4712. set_config(config,&dead_sin);
  4713. }
  4714. prune_peers();
  4715. for (reg = registrations; reg; reg = reg->next)
  4716. iax_do_register(reg);
  4717. return 0;
  4718. }
  4719. int reload(void)
  4720. {
  4721. return reload_config();
  4722. }
  4723. static int cache_get_callno(char *data)
  4724. {
  4725. struct sockaddr_in sin;
  4726. int x;
  4727. char st[256], *s;
  4728. char *host;
  4729. char *username=NULL;
  4730. char *password=NULL;
  4731. char *context=NULL;
  4732. char requeststr[256] = "";
  4733. int callno;
  4734. for (x=0;x<AST_IAX_MAX_CALLS; x++) {
  4735. /* Look for an *exact match* call. Once a call is negotiated, it can only
  4736. look up entries for a single context */
  4737. if (!ast_mutex_trylock(&iaxsl[x])) {
  4738. if (iaxs[x] && !strcasecmp(data, iaxs[x]->dproot)) {
  4739. ast_mutex_unlock(&iaxsl[x]);
  4740. return x;
  4741. }
  4742. ast_mutex_unlock(&iaxsl[x]);
  4743. }
  4744. }
  4745. /* No match found, we need to create a new one */
  4746. strncpy(st, data, sizeof(st)-1);
  4747. /* Grab the host */
  4748. s = strchr(st, '/');
  4749. if (s) {
  4750. *s = '\0';
  4751. s++;
  4752. context = s;
  4753. }
  4754. s = strchr(st, '@');
  4755. if (s) {
  4756. /* Get username/password if there is one */
  4757. *s='\0';
  4758. username=st;
  4759. password = strchr(username, ':');
  4760. if (password) {
  4761. *password = '\0';
  4762. password++;
  4763. }
  4764. s++;
  4765. host = s;
  4766. } else {
  4767. /* Just a hostname */
  4768. host = st;
  4769. }
  4770. /* Populate our address from the given */
  4771. if (create_addr(&sin, NULL, NULL, NULL, host, NULL)) {
  4772. return -1;
  4773. }
  4774. ast_log(LOG_DEBUG, "host: %s, user: %s, password: %s, context: %s\n", host, username, password, context);
  4775. callno = find_callno(-1, -1, &sin, NEW_FORCE);
  4776. if (callno < 0) {
  4777. ast_log(LOG_WARNING, "Unable to create call\n");
  4778. return -1;
  4779. }
  4780. ast_mutex_lock(&iaxsl[callno]);
  4781. strncpy(iaxs[callno]->dproot, data, sizeof(iaxs[callno]->dproot)-1);
  4782. iaxs[callno]->capability = IAX_CAPABILITY_FULLBANDWIDTH;
  4783. MYSNPRINTF "exten=TBD;");
  4784. if (context)
  4785. MYSNPRINTF "context=%s;", context);
  4786. if (username)
  4787. MYSNPRINTF "username=%s;", username);
  4788. /* Remember, codec is irrelevent */
  4789. MYSNPRINTF "formats=%d;", IAX_CAPABILITY_FULLBANDWIDTH);
  4790. MYSNPRINTF "capability=%d;", IAX_CAPABILITY_FULLBANDWIDTH);
  4791. MYSNPRINTF "version=%d;", AST_IAX_PROTO_VERSION);
  4792. if (strlen(requeststr))
  4793. requeststr[strlen(requeststr) -1 ] = '\0';
  4794. /* Keep password handy */
  4795. if (password)
  4796. strncpy(iaxs[callno]->secret, password, sizeof(iaxs[callno]->secret)-1);
  4797. if (option_verbose > 2)
  4798. ast_verbose(VERBOSE_PREFIX_3 "Calling TBD using options '%s'\n", requeststr);
  4799. /* Start the call going */
  4800. send_command(iaxs[callno], AST_FRAME_IAX, AST_IAX_COMMAND_NEW, 0, requeststr, strlen(requeststr) + 1, -1);
  4801. ast_mutex_unlock(&iaxsl[callno]);
  4802. return callno;
  4803. }
  4804. static struct iax_dpcache *find_cache(struct ast_channel *chan, char *data, char *context, char *exten, int priority)
  4805. {
  4806. struct iax_dpcache *dp, *prev = NULL, *next;
  4807. struct timeval tv;
  4808. int x;
  4809. int com[2];
  4810. int timeout;
  4811. int old=0;
  4812. int outfd;
  4813. int abort;
  4814. int callno;
  4815. struct ast_channel *c;
  4816. struct ast_frame *f;
  4817. gettimeofday(&tv, NULL);
  4818. dp = dpcache;
  4819. while(dp) {
  4820. next = dp->next;
  4821. /* Expire old caches */
  4822. if ((tv.tv_sec > dp->expirey.tv_sec) ||
  4823. ((tv.tv_sec == dp->expirey.tv_sec) && (tv.tv_usec > dp->expirey.tv_usec))) {
  4824. /* It's expired, let it disappear */
  4825. if (prev)
  4826. prev->next = dp->next;
  4827. else
  4828. dpcache = dp->next;
  4829. if (!dp->peer && !(dp->flags & CACHE_FLAG_PENDING) && (dp->callno == -1)) {
  4830. /* Free memory and go again */
  4831. free(dp);
  4832. } else {
  4833. ast_log(LOG_WARNING, "DP still has peer field or pending or callno (flags = %d, peer = %p callno = %d)\n", dp->flags, dp->peer, dp->callno);
  4834. }
  4835. dp = next;
  4836. continue;
  4837. }
  4838. /* We found an entry that matches us! */
  4839. if (!strcmp(dp->peercontext, data) && !strcmp(dp->exten, exten))
  4840. break;
  4841. prev = dp;
  4842. dp = next;
  4843. }
  4844. if (!dp) {
  4845. /* No matching entry. Create a new one. */
  4846. /* First, can we make a callno? */
  4847. callno = cache_get_callno(data);
  4848. if (callno < 0) {
  4849. ast_log(LOG_WARNING, "Unable to generate call for '%s'\n", data);
  4850. return NULL;
  4851. }
  4852. dp = malloc(sizeof(struct iax_dpcache));
  4853. if (!dp)
  4854. return NULL;
  4855. memset(dp, 0, sizeof(struct iax_dpcache));
  4856. dp->callno = -1;
  4857. strncpy(dp->peercontext, data, sizeof(dp->peercontext)-1);
  4858. strncpy(dp->exten, exten, sizeof(dp->exten)-1);
  4859. gettimeofday(&dp->expirey, NULL);
  4860. dp->orig = dp->expirey;
  4861. /* Expires in 30 mins by default */
  4862. dp->expirey.tv_sec += iaxdefaultdpcache;
  4863. dp->next = dpcache;
  4864. dp->flags = CACHE_FLAG_PENDING;
  4865. for (x=0;x<sizeof(dp->waiters) / sizeof(dp->waiters[0]); x++)
  4866. dp->waiters[x] = -1;
  4867. dpcache = dp;
  4868. dp->peer = iaxs[callno]->dpentries;
  4869. iaxs[callno]->dpentries = dp;
  4870. /* Send the request if we're already up */
  4871. if (iaxs[callno]->state & IAX_STATE_STARTED)
  4872. iax_dprequest(dp, callno);
  4873. }
  4874. /* By here we must have a dp */
  4875. if (dp->flags & CACHE_FLAG_PENDING) {
  4876. /* Okay, here it starts to get nasty. We need a pipe now to wait
  4877. for a reply to come back so long as it's pending */
  4878. for (x=0;x<sizeof(dp->waiters) / sizeof(dp->waiters[0]); x++) {
  4879. /* Find an empty slot */
  4880. if (dp->waiters[x] < 0)
  4881. break;
  4882. }
  4883. if (x >= sizeof(dp->waiters) / sizeof(dp->waiters[0])) {
  4884. ast_log(LOG_WARNING, "No more waiter positions available\n");
  4885. return NULL;
  4886. }
  4887. if (pipe(com)) {
  4888. ast_log(LOG_WARNING, "Unable to create pipe for comm\n");
  4889. return NULL;
  4890. }
  4891. dp->waiters[x] = com[1];
  4892. /* Okay, now we wait */
  4893. timeout = iaxdefaulttimeout * 1000;
  4894. /* Temporarily unlock */
  4895. ast_mutex_unlock(&dpcache_lock);
  4896. /* Defer any dtmf */
  4897. if (chan)
  4898. old = ast_channel_defer_dtmf(chan);
  4899. abort = 0;
  4900. while(timeout) {
  4901. c = ast_waitfor_nandfds(&chan, chan ? 1 : 0, &com[0], 1, NULL, &outfd, &timeout);
  4902. if (outfd > -1) {
  4903. break;
  4904. }
  4905. if (c) {
  4906. f = ast_read(c);
  4907. if (f)
  4908. ast_frfree(f);
  4909. else {
  4910. /* Got hung up on, abort! */
  4911. break;
  4912. abort = 1;
  4913. }
  4914. }
  4915. }
  4916. if (!timeout) {
  4917. ast_log(LOG_WARNING, "Timeout waiting for %s exten %s\n", data, exten);
  4918. }
  4919. ast_mutex_lock(&dpcache_lock);
  4920. dp->waiters[x] = -1;
  4921. close(com[1]);
  4922. close(com[0]);
  4923. if (abort) {
  4924. /* Don't interpret anything, just abort. Not sure what th epoint
  4925. of undeferring dtmf on a hung up channel is but hey whatever */
  4926. if (!old && chan)
  4927. ast_channel_undefer_dtmf(chan);
  4928. return NULL;
  4929. }
  4930. if (!(dp->flags & CACHE_FLAG_TIMEOUT)) {
  4931. /* Now to do non-independent analysis the results of our wait */
  4932. if (dp->flags & CACHE_FLAG_PENDING) {
  4933. /* Still pending... It's a timeout. Wake everybody up. Consider it no longer
  4934. pending. Don't let it take as long to timeout. */
  4935. dp->flags &= ~CACHE_FLAG_PENDING;
  4936. dp->flags |= CACHE_FLAG_TIMEOUT;
  4937. /* Expire after only 60 seconds now. This is designed to help reduce backlog in heavily loaded
  4938. systems without leaving it unavailable once the server comes back online */
  4939. dp->expirey.tv_sec = dp->orig.tv_sec + 60;
  4940. for (x=0;x<sizeof(dp->waiters) / sizeof(dp->waiters[0]); x++)
  4941. if (dp->waiters[x] > -1)
  4942. write(dp->waiters[x], "asdf", 4);
  4943. }
  4944. }
  4945. /* Our caller will obtain the rest */
  4946. if (!old && chan)
  4947. ast_channel_undefer_dtmf(chan);
  4948. }
  4949. return dp;
  4950. }
  4951. static int iax_exists(struct ast_channel *chan, char *context, char *exten, int priority, char *callerid, char *data)
  4952. {
  4953. struct iax_dpcache *dp;
  4954. int res = 0;
  4955. #if 0
  4956. ast_log(LOG_NOTICE, "iax_exists: con: %s, exten: %s, pri: %d, cid: %s, data: %s\n", context, exten, priority, callerid ? callerid : "<unknown>", data);
  4957. #endif
  4958. if (priority != 1)
  4959. return 0;
  4960. ast_mutex_lock(&dpcache_lock);
  4961. dp = find_cache(chan, data, context, exten, priority);
  4962. if (dp) {
  4963. if (dp->flags & CACHE_FLAG_EXISTS)
  4964. res= 1;
  4965. }
  4966. ast_mutex_unlock(&dpcache_lock);
  4967. if (!dp) {
  4968. ast_log(LOG_WARNING, "Unable to make DP cache\n");
  4969. }
  4970. return res;
  4971. }
  4972. static int iax_canmatch(struct ast_channel *chan, char *context, char *exten, int priority, char *callerid, char *data)
  4973. {
  4974. int res = 0;
  4975. struct iax_dpcache *dp;
  4976. #if 0
  4977. ast_log(LOG_NOTICE, "iax_canmatch: con: %s, exten: %s, pri: %d, cid: %s, data: %s\n", context, exten, priority, callerid ? callerid : "<unknown>", data);
  4978. #endif
  4979. if (priority != 1)
  4980. return 0;
  4981. ast_mutex_lock(&dpcache_lock);
  4982. dp = find_cache(chan, data, context, exten, priority);
  4983. if (dp) {
  4984. if (dp->flags & CACHE_FLAG_CANEXIST)
  4985. res= 1;
  4986. }
  4987. ast_mutex_unlock(&dpcache_lock);
  4988. if (!dp) {
  4989. ast_log(LOG_WARNING, "Unable to make DP cache\n");
  4990. }
  4991. return res;
  4992. }
  4993. static int iax_matchmore(struct ast_channel *chan, char *context, char *exten, int priority, char *callerid, char *data)
  4994. {
  4995. int res = 0;
  4996. struct iax_dpcache *dp;
  4997. #if 0
  4998. ast_log(LOG_NOTICE, "iax_matchmore: con: %s, exten: %s, pri: %d, cid: %s, data: %s\n", context, exten, priority, callerid ? callerid : "<unknown>", data);
  4999. #endif
  5000. if (priority != 1)
  5001. return 0;
  5002. ast_mutex_lock(&dpcache_lock);
  5003. dp = find_cache(chan, data, context, exten, priority);
  5004. if (dp) {
  5005. if (dp->flags & CACHE_FLAG_MATCHMORE)
  5006. res= 1;
  5007. }
  5008. ast_mutex_unlock(&dpcache_lock);
  5009. if (!dp) {
  5010. ast_log(LOG_WARNING, "Unable to make DP cache\n");
  5011. }
  5012. return res;
  5013. }
  5014. static int iax_exec(struct ast_channel *chan, char *context, char *exten, int priority, char *callerid, int newstack, char *data)
  5015. {
  5016. char odata[256];
  5017. char req[256];
  5018. char *ncontext;
  5019. struct iax_dpcache *dp;
  5020. struct ast_app *dial;
  5021. #if 0
  5022. ast_log(LOG_NOTICE, "iax_exec: con: %s, exten: %s, pri: %d, cid: %s, data: %s, newstack: %d\n", context, exten, priority, callerid ? callerid : "<unknown>", data, newstack);
  5023. #endif
  5024. if (priority != 1)
  5025. return -1;
  5026. ast_mutex_lock(&dpcache_lock);
  5027. dp = find_cache(chan, data, context, exten, priority);
  5028. if (dp) {
  5029. if (dp->flags & CACHE_FLAG_EXISTS) {
  5030. strncpy(odata, data, sizeof(odata)-1);
  5031. ncontext = strchr(odata, '/');
  5032. if (ncontext) {
  5033. *ncontext = '\0';
  5034. ncontext++;
  5035. snprintf(req, sizeof(req), "IAX/%s/%s@%s", odata, exten, ncontext);
  5036. } else {
  5037. snprintf(req, sizeof(req), "IAX/%s/%s", odata, exten);
  5038. }
  5039. if (option_verbose > 2)
  5040. ast_verbose(VERBOSE_PREFIX_3 "Executing Dial('%s')\n", req);
  5041. } else {
  5042. ast_mutex_unlock(&dpcache_lock);
  5043. ast_log(LOG_WARNING, "Can't execute non-existant extension '%s[@%s]' in data '%s'\n", exten, context, data);
  5044. return -1;
  5045. }
  5046. }
  5047. ast_mutex_unlock(&dpcache_lock);
  5048. dial = pbx_findapp("Dial");
  5049. if (dial) {
  5050. pbx_exec(chan, dial, req, newstack);
  5051. } else {
  5052. ast_log(LOG_WARNING, "No dial application registered\n");
  5053. }
  5054. return -1;
  5055. }
  5056. static struct ast_switch iax_switch =
  5057. {
  5058. name: "IAX",
  5059. description: "IAX Remote Dialplan Switch",
  5060. exists: iax_exists,
  5061. canmatch: iax_canmatch,
  5062. exec: iax_exec,
  5063. matchmore: iax_matchmore,
  5064. };
  5065. static int __unload_module(void)
  5066. {
  5067. int x;
  5068. /* Cancel the network thread, close the net socket */
  5069. if (netthreadid != AST_PTHREADT_NULL) {
  5070. pthread_cancel(netthreadid);
  5071. pthread_join(netthreadid, NULL);
  5072. }
  5073. close(netsocket);
  5074. for (x=0;x<AST_IAX_MAX_CALLS;x++)
  5075. if (iaxs[x])
  5076. iax_destroy(x);
  5077. ast_manager_unregister( "IAX1peers" );
  5078. ast_cli_unregister(&cli_show_users);
  5079. ast_cli_unregister(&cli_show_channels);
  5080. ast_cli_unregister(&cli_show_peers);
  5081. ast_cli_unregister(&cli_show_registry);
  5082. ast_cli_unregister(&cli_debug);
  5083. ast_cli_unregister(&cli_no_debug);
  5084. ast_cli_unregister(&cli_set_jitter);
  5085. ast_cli_unregister(&cli_show_stats);
  5086. ast_cli_unregister(&cli_show_cache);
  5087. ast_unregister_switch(&iax_switch);
  5088. ast_channel_unregister(type);
  5089. ast_channel_unregister(ctype);
  5090. delete_users();
  5091. return 0;
  5092. }
  5093. int unload_module()
  5094. {
  5095. int x;
  5096. for (x=0;x<AST_IAX_MAX_CALLS;x++)
  5097. ast_mutex_destroy(&iaxsl[x]);
  5098. ast_mutex_destroy(&iaxq.lock);
  5099. ast_mutex_destroy(&userl.lock);
  5100. ast_mutex_destroy(&peerl.lock);
  5101. return __unload_module();
  5102. }
  5103. int load_module(void)
  5104. {
  5105. char *config = IAX_CONF_FILE;
  5106. int res = 0;
  5107. int x;
  5108. struct iax_registry *reg;
  5109. struct iax_peer *peer;
  5110. char iabuf[INET_ADDRSTRLEN];
  5111. struct sockaddr_in sin;
  5112. /* Seed random number generator */
  5113. srand(time(NULL));
  5114. sin.sin_family = AF_INET;
  5115. sin.sin_port = ntohs(AST_DEFAULT_IAX_PORTNO);
  5116. sin.sin_addr.s_addr = INADDR_ANY;
  5117. for (x=0;x<AST_IAX_MAX_CALLS;x++)
  5118. ast_mutex_init(&iaxsl[x]);
  5119. io = io_context_create();
  5120. sched = sched_context_create();
  5121. if (!io || !sched) {
  5122. ast_log(LOG_ERROR, "Out of memory\n");
  5123. return -1;
  5124. }
  5125. ast_mutex_init(&iaxq.lock);
  5126. ast_mutex_init(&userl.lock);
  5127. ast_mutex_init(&peerl.lock);
  5128. ast_cli_register(&cli_show_users);
  5129. ast_cli_register(&cli_show_channels);
  5130. ast_cli_register(&cli_show_peers);
  5131. ast_cli_register(&cli_show_registry);
  5132. ast_cli_register(&cli_debug);
  5133. ast_cli_register(&cli_no_debug);
  5134. ast_cli_register(&cli_set_jitter);
  5135. ast_cli_register(&cli_show_stats);
  5136. ast_cli_register(&cli_show_cache);
  5137. ast_manager_register( "IAX1peers", 0, manager_iax_show_peers, "List IAX Peers" );
  5138. if (set_config(config,&sin) == -1) {
  5139. config = IAX_OLD_CONF_FILE;
  5140. set_config(config,&sin);
  5141. }
  5142. /* Make a UDP socket */
  5143. netsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
  5144. if (netsocket < 0) {
  5145. ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno));
  5146. return -1;
  5147. }
  5148. if (bind(netsocket,(struct sockaddr *)&sin, sizeof(sin))) {
  5149. ast_log(LOG_ERROR, "Unable to bind to %s port %d: %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), ntohs(sin.sin_port), strerror(errno));
  5150. close(netsocket);
  5151. netsocket = -1;
  5152. return -1;
  5153. }
  5154. if (option_verbose > 1)
  5155. ast_verbose(VERBOSE_PREFIX_2 "Using TOS bits %d\n", tos);
  5156. if (setsockopt(netsocket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)))
  5157. ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
  5158. if (ast_channel_register(type, tdesc, iax_capability, iax_request)) {
  5159. ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
  5160. __unload_module();
  5161. return -1;
  5162. }
  5163. if (ast_channel_register(ctype, tdesc, iax_capability, iax_request)) {
  5164. ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
  5165. __unload_module();
  5166. return -1;
  5167. }
  5168. if (ast_register_switch(&iax_switch))
  5169. ast_log(LOG_ERROR, "Unable to register IAX switch\n");
  5170. if (!res) {
  5171. res = start_network_thread();
  5172. if (option_verbose > 1)
  5173. ast_verbose(VERBOSE_PREFIX_2 "IAX Ready and Listening on %s port %d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), ntohs(sin.sin_port));
  5174. } else {
  5175. ast_log(LOG_ERROR, "Unable to start network thread\n");
  5176. close(netsocket);
  5177. }
  5178. for (reg = registrations; reg; reg = reg->next)
  5179. iax_do_register(reg);
  5180. ast_mutex_lock(&peerl.lock);
  5181. for (peer = peerl.peers; peer; peer = peer->next)
  5182. iax_poke_peer(peer);
  5183. ast_mutex_unlock(&peerl.lock);
  5184. return res;
  5185. }
  5186. char *description()
  5187. {
  5188. return desc;
  5189. }
  5190. int usecount()
  5191. {
  5192. int res;
  5193. ast_mutex_lock(&usecnt_lock);
  5194. res = usecnt;
  5195. ast_mutex_unlock(&usecnt_lock);
  5196. return res;
  5197. }
  5198. char *key()
  5199. {
  5200. return ASTERISK_GPL_KEY;
  5201. }