chan_ooh323.c 134 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844
  1. /*
  2. * Copyright (C) 2004-2005 by Objective Systems, Inc.
  3. *
  4. * This software is furnished under an open source license and may be
  5. * used and copied only in accordance with the terms of this license.
  6. * The text of the license may generally be found in the root
  7. * directory of this installation in the COPYING file. It
  8. * can also be viewed online at the following URL:
  9. *
  10. * http://www.obj-sys.com/open/license.html
  11. *
  12. * Any redistributions of this file including modified versions must
  13. * maintain this copyright notice.
  14. *
  15. *****************************************************************************/
  16. /* Reworked version I, Nov-2009, by Alexandr Anikin, may@telecom-service.ru */
  17. /*** MODULEINFO
  18. <defaultenabled>no</defaultenabled>
  19. <support_level>extended</support_level>
  20. ***/
  21. #include "chan_ooh323.h"
  22. #include <math.h>
  23. /*** DOCUMENTATION
  24. <function name="OOH323" language="en_US">
  25. <synopsis>
  26. Allow Setting / Reading OOH323 Settings
  27. </synopsis>
  28. <syntax>
  29. <parameter name="name" required="true">
  30. <enumlist>
  31. <enum name="faxdetect">
  32. <para>Fax Detect [R/W]</para>
  33. <para>Returns 0 or 1</para>
  34. <para>Write yes or no</para>
  35. </enum>
  36. </enumlist>
  37. <enumlist>
  38. <enum name="t38support">
  39. <para>t38support [R/W]</para>
  40. <para>Returns 0 or 1</para>
  41. <para>Write yes or no</para>
  42. </enum>
  43. </enumlist>
  44. <enumlist>
  45. <enum name="h323id">
  46. <para>Returns h323id [R]</para>
  47. </enum>
  48. </enumlist>
  49. </parameter>
  50. </syntax>
  51. <description>
  52. <para>Read and set channel parameters in the dialplan.
  53. <replaceable>name</replaceable> is one of the above only those with a [W] can be writen to.
  54. </para>
  55. </description>
  56. </function>
  57. ***/
  58. #define FORMAT_STRING_SIZE 512
  59. /* Defaults */
  60. #define DEFAULT_CONTEXT "default"
  61. #define DEFAULT_H323ID "Asterisk PBX"
  62. #define DEFAULT_LOGFILE "/var/log/asterisk/h323_log"
  63. #define DEFAULT_H323ACCNT "ast_h323"
  64. /* Flags */
  65. #define H323_SILENCESUPPRESSION (1<<0)
  66. #define H323_GKROUTED (1<<1)
  67. #define H323_TUNNELING (1<<2)
  68. #define H323_FASTSTART (1<<3)
  69. #define H323_OUTGOING (1<<4)
  70. #define H323_ALREADYGONE (1<<5)
  71. #define H323_NEEDDESTROY (1<<6)
  72. #define H323_DISABLEGK (1<<7)
  73. #define H323_NEEDSTART (1<<8)
  74. #define MAXT30 240
  75. #define T38TOAUDIOTIMEOUT 30
  76. #define T38_DISABLED 0
  77. #define T38_ENABLED 1
  78. #define T38_FAXGW 1
  79. #define FAXDETECT_CNG 1
  80. #define FAXDETECT_T38 2
  81. /* Channel description */
  82. static const char type[] = "OOH323";
  83. static const char tdesc[] = "Objective Systems H323 Channel Driver";
  84. static const char config[] = "ooh323.conf";
  85. struct ast_module *myself;
  86. static struct ast_jb_conf default_jbconf =
  87. {
  88. .flags = 0,
  89. .max_size = -1,
  90. .resync_threshold = -1,
  91. .impl = ""
  92. };
  93. static struct ast_jb_conf global_jbconf;
  94. /* Channel Definition */
  95. static struct ast_channel *ooh323_request(const char *type, struct ast_format_cap *cap,
  96. const struct ast_channel *requestor, void *data, int *cause);
  97. static int ooh323_digit_begin(struct ast_channel *ast, char digit);
  98. static int ooh323_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
  99. static int ooh323_call(struct ast_channel *ast, char *dest, int timeout);
  100. static int ooh323_hangup(struct ast_channel *ast);
  101. static int ooh323_answer(struct ast_channel *ast);
  102. static struct ast_frame *ooh323_read(struct ast_channel *ast);
  103. static int ooh323_write(struct ast_channel *ast, struct ast_frame *f);
  104. static int ooh323_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
  105. static int ooh323_queryoption(struct ast_channel *ast, int option, void *data, int *datalen);
  106. static int ooh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
  107. static enum ast_rtp_glue_result ooh323_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **rtp);
  108. static enum ast_rtp_glue_result ooh323_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp_instance **rtp);
  109. static int ooh323_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *rtp,
  110. struct ast_rtp_instance *vrtp, struct ast_rtp_instance *trtp, const struct ast_format_cap *codecs, int nat_active);
  111. static struct ast_udptl *ooh323_get_udptl_peer(struct ast_channel *chan);
  112. static int ooh323_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl);
  113. static void print_codec_to_cli(int fd, struct ast_codec_pref *pref);
  114. struct ooh323_peer *find_friend(const char *name, int port);
  115. static struct ast_channel_tech ooh323_tech = {
  116. .type = type,
  117. .description = tdesc,
  118. .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
  119. .requester = ooh323_request,
  120. .send_digit_begin = ooh323_digit_begin,
  121. .send_digit_end = ooh323_digit_end,
  122. .call = ooh323_call,
  123. .hangup = ooh323_hangup,
  124. .answer = ooh323_answer,
  125. .read = ooh323_read,
  126. .write = ooh323_write,
  127. .exception = ooh323_read,
  128. .indicate = ooh323_indicate,
  129. .fixup = ooh323_fixup,
  130. .send_html = 0,
  131. .queryoption = ooh323_queryoption,
  132. .bridge = ast_rtp_instance_bridge, /* XXX chan unlocked ? */
  133. .early_bridge = ast_rtp_instance_early_bridge,
  134. };
  135. static struct ast_rtp_glue ooh323_rtp = {
  136. .type = type,
  137. .get_rtp_info = ooh323_get_rtp_peer,
  138. .get_vrtp_info = ooh323_get_vrtp_peer,
  139. .update_peer = ooh323_set_rtp_peer,
  140. };
  141. static struct ast_udptl_protocol ooh323_udptl = {
  142. type: "H323",
  143. get_udptl_info: ooh323_get_udptl_peer,
  144. set_udptl_peer: ooh323_set_udptl_peer,
  145. };
  146. struct ooh323_user;
  147. /* H.323 channel private structure */
  148. static struct ooh323_pvt {
  149. ast_mutex_t lock; /* Channel private lock */
  150. struct ast_rtp_instance *rtp;
  151. struct ast_rtp_instance *vrtp; /* Placeholder for now */
  152. int t38support; /* T.38 mode - disable, transparent, faxgw */
  153. int faxdetect;
  154. int faxdetected;
  155. int rtptimeout;
  156. struct ast_udptl *udptl;
  157. int faxmode;
  158. int t38_tx_enable;
  159. int t38_init;
  160. struct ast_sockaddr udptlredirip;
  161. time_t lastTxT38;
  162. int chmodepend;
  163. struct ast_channel *owner; /* Master Channel */
  164. union {
  165. char *user; /* cooperating user/peer */
  166. char *peer;
  167. } neighbor;
  168. time_t lastrtptx;
  169. time_t lastrtprx;
  170. unsigned int flags;
  171. unsigned int call_reference;
  172. char *callToken;
  173. char *username;
  174. char *host;
  175. char *callerid_name;
  176. char *callerid_num;
  177. char caller_h323id[AST_MAX_EXTENSION];
  178. char caller_dialedDigits[AST_MAX_EXTENSION];
  179. char caller_email[AST_MAX_EXTENSION];
  180. char caller_url[256];
  181. char callee_h323id[AST_MAX_EXTENSION];
  182. char callee_dialedDigits[AST_MAX_EXTENSION];
  183. char callee_email[AST_MAX_EXTENSION];
  184. char callee_url[AST_MAX_EXTENSION];
  185. int port;
  186. struct ast_format readformat; /* negotiated read format */
  187. struct ast_format writeformat; /* negotiated write format */
  188. struct ast_format_cap *cap;
  189. struct ast_codec_pref prefs;
  190. int dtmfmode;
  191. int dtmfcodec;
  192. char exten[AST_MAX_EXTENSION]; /* Requested extension */
  193. char context[AST_MAX_EXTENSION]; /* Context where to start */
  194. char accountcode[256]; /* Account code */
  195. int nat;
  196. int amaflags;
  197. int progsent; /* progress is sent */
  198. int alertsent; /* alerting is sent */
  199. int g729onlyA; /* G.729 only A */
  200. struct ast_dsp *vad;
  201. struct OOH323Regex *rtpmask; /* rtp ip regexp */
  202. char rtpmaskstr[120];
  203. int rtdrcount, rtdrinterval; /* roundtripdelayreq */
  204. int faststart, h245tunneling; /* faststart & h245 tunneling */
  205. struct ooh323_pvt *next; /* Next entity */
  206. } *iflist = NULL;
  207. /* Protect the channel/interface list (ooh323_pvt) */
  208. AST_MUTEX_DEFINE_STATIC(iflock);
  209. /* Profile of H.323 user registered with PBX*/
  210. struct ooh323_user{
  211. ast_mutex_t lock;
  212. char name[256];
  213. char context[AST_MAX_EXTENSION];
  214. int incominglimit;
  215. unsigned inUse;
  216. char accountcode[20];
  217. int amaflags;
  218. struct ast_format_cap *cap;
  219. struct ast_codec_pref prefs;
  220. int dtmfmode;
  221. int dtmfcodec;
  222. int faxdetect;
  223. int t38support;
  224. int rtptimeout;
  225. int mUseIP; /* Use IP address or H323-ID to search user */
  226. char mIP[4*8+7+2]; /* Max for IPv6 - 2 brackets, 8 4hex, 7 - : */
  227. struct OOH323Regex *rtpmask;
  228. char rtpmaskstr[120];
  229. int rtdrcount, rtdrinterval;
  230. int faststart, h245tunneling;
  231. int g729onlyA;
  232. struct ooh323_user *next;
  233. };
  234. /* Profile of valid asterisk peers */
  235. struct ooh323_peer{
  236. ast_mutex_t lock;
  237. char name[256];
  238. unsigned outgoinglimit;
  239. unsigned outUse;
  240. struct ast_format_cap *cap;
  241. struct ast_codec_pref prefs;
  242. char accountcode[20];
  243. int amaflags;
  244. int dtmfmode;
  245. int dtmfcodec;
  246. int faxdetect;
  247. int t38support;
  248. int mFriend; /* indicates defined as friend */
  249. char ip[4*8+7+2]; /* Max for IPv6 - 2 brackets, 8 4hex, 7 - : */
  250. int port;
  251. char *h323id; /* H323-ID alias, which asterisk will register with gk to reach this peer*/
  252. char *email; /* Email alias, which asterisk will register with gk to reach this peer*/
  253. char *url; /* url alias, which asterisk will register with gk to reach this peer*/
  254. char *e164; /* e164 alias, which asterisk will register with gk to reach this peer*/
  255. int rtptimeout;
  256. struct OOH323Regex *rtpmask;
  257. char rtpmaskstr[120];
  258. int rtdrcount,rtdrinterval;
  259. int faststart, h245tunneling;
  260. int g729onlyA;
  261. struct ooh323_peer *next;
  262. };
  263. /* List of H.323 users known to PBX */
  264. static struct ast_user_list {
  265. struct ooh323_user *users;
  266. ast_mutex_t lock;
  267. } userl;
  268. static struct ast_peer_list {
  269. struct ooh323_peer *peers;
  270. ast_mutex_t lock;
  271. } peerl;
  272. /* Mutex to protect H.323 reload process */
  273. static int h323_reloading = 0;
  274. AST_MUTEX_DEFINE_STATIC(h323_reload_lock);
  275. /* Mutex to protect usage counter */
  276. static int usecnt = 0;
  277. AST_MUTEX_DEFINE_STATIC(usecnt_lock);
  278. AST_MUTEX_DEFINE_STATIC(ooh323c_cmd_lock);
  279. static long callnumber = 0;
  280. AST_MUTEX_DEFINE_STATIC(ooh323c_cn_lock);
  281. /* stack callbacks */
  282. int onAlerting(ooCallData *call);
  283. int onProgress(ooCallData *call);
  284. int onNewCallCreated(ooCallData *call);
  285. int onOutgoingCall(ooCallData *call);
  286. int onCallEstablished(ooCallData *call);
  287. int onCallCleared(ooCallData *call);
  288. void onModeChanged(ooCallData *call, int t38mode);
  289. static char gLogFile[256] = DEFAULT_LOGFILE;
  290. static int gPort = 1720;
  291. static char gIP[2+8*4+7]; /* Max for IPv6 addr */
  292. struct ast_sockaddr bindaddr;
  293. int v6mode = 0;
  294. static char gCallerID[AST_MAX_EXTENSION] = "";
  295. static struct ooAliases *gAliasList;
  296. static struct ast_format_cap *gCap;
  297. static struct ast_codec_pref gPrefs;
  298. static int gDTMFMode = H323_DTMF_RFC2833;
  299. static int gDTMFCodec = 101;
  300. static int gFAXdetect = FAXDETECT_CNG;
  301. static int gT38Support = T38_FAXGW;
  302. static char gGatekeeper[100];
  303. static enum RasGatekeeperMode gRasGkMode = RasNoGatekeeper;
  304. static int gIsGateway = 0;
  305. static int gFastStart = 1;
  306. static int gTunneling = 1;
  307. static int gBeMaster = 0;
  308. static int gMediaWaitForConnect = 0;
  309. static int gTOS = 0;
  310. static int gRTPTimeout = 60;
  311. static int g729onlyA = 0;
  312. static char gAccountcode[80] = DEFAULT_H323ACCNT;
  313. static int gAMAFLAGS;
  314. static char gContext[AST_MAX_EXTENSION] = DEFAULT_CONTEXT;
  315. static int gIncomingLimit = 1024;
  316. static int gOutgoingLimit = 1024;
  317. OOBOOL gH323Debug = FALSE;
  318. static int gTRCLVL = OOTRCLVLERR;
  319. static int gRTDRCount = 0, gRTDRInterval = 0;
  320. static int t35countrycode = 0;
  321. static int t35extensions = 0;
  322. static int manufacturer = 0;
  323. static char vendor[AST_MAX_EXTENSION] = "";
  324. static char version[AST_MAX_EXTENSION] = "";
  325. static struct ooh323_config
  326. {
  327. int mTCPPortStart;
  328. int mTCPPortEnd;
  329. } ooconfig;
  330. /** Asterisk RTP stuff*/
  331. static struct ast_sched_context *sched;
  332. static struct io_context *io;
  333. /* Protect the monitoring thread, so only one process can kill or start it,
  334. and not when it's doing something critical. */
  335. AST_MUTEX_DEFINE_STATIC(monlock);
  336. /* This is the thread for the monitor which checks for input on the channels
  337. which are not currently in use. */
  338. static pthread_t monitor_thread = AST_PTHREADT_NULL;
  339. static struct ast_channel *ooh323_new(struct ooh323_pvt *i, int state,
  340. const char *host, struct ast_format_cap *cap, const char *linkedid)
  341. {
  342. struct ast_channel *ch = NULL;
  343. struct ast_format tmpfmt;
  344. int features = 0;
  345. if (gH323Debug)
  346. ast_verbose("--- ooh323_new - %s\n", host);
  347. ast_format_clear(&tmpfmt);
  348. /* Don't hold a h323 pvt lock while we allocate a channel */
  349. ast_mutex_unlock(&i->lock);
  350. ch = ast_channel_alloc(1, state, i->callerid_num, i->callerid_name,
  351. i->accountcode, i->exten, i->context, linkedid, i->amaflags,
  352. "OOH323/%s-%ld", host, callnumber);
  353. ast_mutex_lock(&ooh323c_cn_lock);
  354. callnumber++;
  355. ast_mutex_unlock(&ooh323c_cn_lock);
  356. ast_mutex_lock(&i->lock);
  357. if (ch) {
  358. ast_channel_lock(ch);
  359. ch->tech = &ooh323_tech;
  360. if (cap)
  361. ast_best_codec(cap, &tmpfmt);
  362. if (!tmpfmt.id)
  363. ast_codec_pref_index(&i->prefs, 0, &tmpfmt);
  364. ast_format_cap_add(ch->nativeformats, &tmpfmt);
  365. ast_format_copy(&ch->rawwriteformat, &tmpfmt);
  366. ast_format_copy(&ch->rawreadformat, &tmpfmt);
  367. ast_jb_configure(ch, &global_jbconf);
  368. if (state == AST_STATE_RING)
  369. ch->rings = 1;
  370. ch->adsicpe = AST_ADSI_UNAVAILABLE;
  371. ast_set_write_format(ch, &tmpfmt);
  372. ast_set_read_format(ch, &tmpfmt);
  373. ch->tech_pvt = i;
  374. i->owner = ch;
  375. ast_module_ref(myself);
  376. /* Allocate dsp for in-band DTMF support */
  377. if ((i->dtmfmode & H323_DTMF_INBAND) || (i->faxdetect & FAXDETECT_CNG)) {
  378. i->vad = ast_dsp_new();
  379. }
  380. /* inband DTMF*/
  381. if (i->dtmfmode & H323_DTMF_INBAND) {
  382. features |= DSP_FEATURE_DIGIT_DETECT;
  383. if (i->dtmfmode & H323_DTMF_INBANDRELAX) {
  384. ast_dsp_set_digitmode(i->vad, DSP_DIGITMODE_DTMF | DSP_DIGITMODE_RELAXDTMF);
  385. }
  386. }
  387. /* fax detection*/
  388. if (i->faxdetect & FAXDETECT_CNG) {
  389. features |= DSP_FEATURE_FAX_DETECT;
  390. ast_dsp_set_faxmode(i->vad,
  391. DSP_FAXMODE_DETECT_CNG | DSP_FAXMODE_DETECT_CED);
  392. }
  393. if (features) {
  394. ast_dsp_set_features(i->vad, features);
  395. }
  396. ast_mutex_lock(&usecnt_lock);
  397. usecnt++;
  398. ast_mutex_unlock(&usecnt_lock);
  399. /* Notify the module monitors that use count for resource has changed*/
  400. ast_update_use_count();
  401. ast_copy_string(ch->context, i->context, sizeof(ch->context));
  402. ast_copy_string(ch->exten, i->exten, sizeof(ch->exten));
  403. ch->priority = 1;
  404. if(!ast_test_flag(i, H323_OUTGOING)) {
  405. if (!ast_strlen_zero(i->caller_h323id)) {
  406. pbx_builtin_setvar_helper(ch, "_CALLER_H323ID", i->caller_h323id);
  407. }
  408. if (!ast_strlen_zero(i->caller_dialedDigits)) {
  409. pbx_builtin_setvar_helper(ch, "_CALLER_H323DIALEDDIGITS",
  410. i->caller_dialedDigits);
  411. }
  412. if (!ast_strlen_zero(i->caller_email)) {
  413. pbx_builtin_setvar_helper(ch, "_CALLER_H323EMAIL",
  414. i->caller_email);
  415. }
  416. if (!ast_strlen_zero(i->caller_url)) {
  417. pbx_builtin_setvar_helper(ch, "_CALLER_H323URL", i->caller_url);
  418. }
  419. }
  420. if (!ast_strlen_zero(i->accountcode))
  421. ast_string_field_set(ch, accountcode, i->accountcode);
  422. if (i->amaflags)
  423. ch->amaflags = i->amaflags;
  424. ast_setstate(ch, state);
  425. if (state != AST_STATE_DOWN) {
  426. if (ast_pbx_start(ch)) {
  427. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ch->name);
  428. ast_channel_unlock(ch);
  429. ast_hangup(ch);
  430. ch = NULL;
  431. }
  432. }
  433. manager_event(EVENT_FLAG_SYSTEM, "ChannelUpdate", "Channel: %s\r\nChanneltype: %s\r\n"
  434. "CallRef: %d\r\n", ch->name, "OOH323", i->call_reference);
  435. } else
  436. ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
  437. if(ch) ast_channel_unlock(ch);
  438. if (gH323Debug)
  439. ast_verbose("+++ h323_new\n");
  440. return ch;
  441. }
  442. static struct ooh323_pvt *ooh323_alloc(int callref, char *callToken)
  443. {
  444. struct ooh323_pvt *pvt = NULL;
  445. if (gH323Debug)
  446. ast_verbose("--- ooh323_alloc\n");
  447. if (!(pvt = ast_calloc(1, sizeof(*pvt)))) {
  448. ast_log(LOG_ERROR, "Couldn't allocate private ooh323 structure\n");
  449. return NULL;
  450. }
  451. if (!(pvt->cap = ast_format_cap_alloc_nolock())) {
  452. ast_free(pvt);
  453. ast_log(LOG_ERROR, "Couldn't allocate private ooh323 structure\n");
  454. return NULL;
  455. }
  456. ast_mutex_init(&pvt->lock);
  457. ast_mutex_lock(&pvt->lock);
  458. pvt->faxmode = 0;
  459. pvt->chmodepend = 0;
  460. pvt->faxdetected = 0;
  461. pvt->faxdetect = gFAXdetect;
  462. pvt->t38support = gT38Support;
  463. pvt->rtptimeout = gRTPTimeout;
  464. pvt->rtdrinterval = gRTDRInterval;
  465. pvt->rtdrcount = gRTDRCount;
  466. pvt->g729onlyA = g729onlyA;
  467. pvt->call_reference = callref;
  468. if (callToken)
  469. pvt->callToken = strdup(callToken);
  470. /* whether to use gk for this call */
  471. if (gRasGkMode == RasNoGatekeeper)
  472. OO_SETFLAG(pvt->flags, H323_DISABLEGK);
  473. pvt->dtmfmode = gDTMFMode;
  474. pvt->dtmfcodec = gDTMFCodec;
  475. ast_copy_string(pvt->context, gContext, sizeof(pvt->context));
  476. ast_copy_string(pvt->accountcode, gAccountcode, sizeof(pvt->accountcode));
  477. pvt->amaflags = gAMAFLAGS;
  478. ast_format_cap_copy(pvt->cap, gCap);
  479. memcpy(&pvt->prefs, &gPrefs, sizeof(pvt->prefs));
  480. ast_mutex_unlock(&pvt->lock);
  481. /* Add to interface list */
  482. ast_mutex_lock(&iflock);
  483. pvt->next = iflist;
  484. iflist = pvt;
  485. ast_mutex_unlock(&iflock);
  486. if (gH323Debug)
  487. ast_verbose("+++ ooh323_alloc\n");
  488. return pvt;
  489. }
  490. /*
  491. Possible data values - peername, exten/peername, exten@ip
  492. */
  493. static struct ast_channel *ooh323_request(const char *type, struct ast_format_cap *cap,
  494. const struct ast_channel *requestor, void *data, int *cause)
  495. {
  496. struct ast_channel *chan = NULL;
  497. struct ooh323_pvt *p = NULL;
  498. struct ooh323_peer *peer = NULL;
  499. char *dest = NULL;
  500. char *ext = NULL;
  501. char tmp[256];
  502. char formats[FORMAT_STRING_SIZE];
  503. int port = 0;
  504. if (gH323Debug)
  505. ast_verbose("--- ooh323_request - data %s format %s\n", (char*)data,
  506. ast_getformatname_multiple(formats,FORMAT_STRING_SIZE,cap));
  507. if (!(ast_format_cap_has_type(cap, AST_FORMAT_TYPE_AUDIO))) {
  508. ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%s'\n", ast_getformatname_multiple(formats,FORMAT_STRING_SIZE,cap));
  509. return NULL;
  510. }
  511. p = ooh323_alloc(0,0); /* Initial callRef is zero */
  512. if (!p) {
  513. ast_log(LOG_WARNING, "Unable to build pvt data for '%s'\n", (char*)data);
  514. return NULL;
  515. }
  516. ast_mutex_lock(&p->lock);
  517. /* This is an outgoing call, since ooh323_request is called */
  518. ast_set_flag(p, H323_OUTGOING);
  519. ast_copy_string(tmp, data, sizeof(tmp));
  520. dest = strchr(tmp, '/');
  521. if (dest) {
  522. *dest = '\0';
  523. dest++;
  524. ext = dest;
  525. dest = tmp;
  526. } else if ((dest = strchr(tmp, '@'))) {
  527. *dest = '\0';
  528. dest++;
  529. ext = tmp;
  530. } else {
  531. dest = tmp;
  532. ext = NULL;
  533. }
  534. #if 0
  535. if ((sport = strchr(dest, ':'))) {
  536. *sport = '\0';
  537. sport++;
  538. port = atoi(sport);
  539. }
  540. #endif
  541. if (dest) {
  542. peer = find_peer(dest, port);
  543. } else{
  544. ast_mutex_lock(&iflock);
  545. ast_mutex_unlock(&p->lock);
  546. ooh323_destroy(p);
  547. ast_mutex_unlock(&iflock);
  548. ast_log(LOG_ERROR, "Destination format is not supported\n");
  549. return NULL;
  550. }
  551. if (peer) {
  552. p->username = strdup(peer->name);
  553. p->host = strdup(peer->ip);
  554. p->port = peer->port;
  555. /* Disable gk as we are going to call a known peer*/
  556. /* OO_SETFLAG(p->flags, H323_DISABLEGK); */
  557. if (ext)
  558. ast_copy_string(p->exten, ext, sizeof(p->exten));
  559. ast_format_cap_copy(p->cap, peer->cap);
  560. memcpy(&p->prefs, &peer->prefs, sizeof(struct ast_codec_pref));
  561. p->g729onlyA = peer->g729onlyA;
  562. p->dtmfmode |= peer->dtmfmode;
  563. p->dtmfcodec = peer->dtmfcodec;
  564. p->faxdetect = peer->faxdetect;
  565. p->t38support = peer->t38support;
  566. p->rtptimeout = peer->rtptimeout;
  567. p->faststart = peer->faststart;
  568. p->h245tunneling = peer->h245tunneling;
  569. if (peer->rtpmask && peer->rtpmaskstr[0]) {
  570. p->rtpmask = peer->rtpmask;
  571. ast_copy_string(p->rtpmaskstr, peer->rtpmaskstr, sizeof(p->rtpmaskstr));
  572. }
  573. if (peer->rtdrinterval) {
  574. p->rtdrinterval = peer->rtdrinterval;
  575. p->rtdrcount = peer->rtdrcount;
  576. }
  577. ast_copy_string(p->accountcode, peer->accountcode, sizeof(p->accountcode));
  578. p->amaflags = peer->amaflags;
  579. } else {
  580. if (gRasGkMode == RasNoGatekeeper) {
  581. /* no gk and no peer */
  582. ast_log(LOG_ERROR, "Call to undefined peer %s", dest);
  583. ast_mutex_lock(&iflock);
  584. ast_mutex_unlock(&p->lock);
  585. ooh323_destroy(p);
  586. ast_mutex_unlock(&iflock);
  587. return NULL;
  588. }
  589. p->g729onlyA = g729onlyA;
  590. p->dtmfmode = gDTMFMode;
  591. p->dtmfcodec = gDTMFCodec;
  592. p->faxdetect = gFAXdetect;
  593. p->t38support = gT38Support;
  594. p->rtptimeout = gRTPTimeout;
  595. ast_format_cap_copy(p->cap, gCap);
  596. p->rtdrinterval = gRTDRInterval;
  597. p->rtdrcount = gRTDRCount;
  598. p->faststart = gFastStart;
  599. p->h245tunneling = gTunneling;
  600. memcpy(&p->prefs, &gPrefs, sizeof(struct ast_codec_pref));
  601. p->username = strdup(dest);
  602. p->host = strdup(dest);
  603. if (port > 0) {
  604. p->port = port;
  605. }
  606. if (ext) {
  607. ast_copy_string(p->exten, ext, sizeof(p->exten));
  608. }
  609. }
  610. chan = ooh323_new(p, AST_STATE_DOWN, p->username, cap,
  611. requestor ? requestor->linkedid : NULL);
  612. ast_mutex_unlock(&p->lock);
  613. if (!chan) {
  614. ast_mutex_lock(&iflock);
  615. ooh323_destroy(p);
  616. ast_mutex_unlock(&iflock);
  617. } else {
  618. ast_mutex_lock(&p->lock);
  619. p->callToken = (char*)ast_calloc(1, AST_MAX_EXTENSION);
  620. if(!p->callToken) {
  621. ast_mutex_unlock(&p->lock);
  622. ast_mutex_lock(&iflock);
  623. ooh323_destroy(p);
  624. ast_mutex_unlock(&iflock);
  625. ast_log(LOG_ERROR, "Failed to allocate memory for callToken\n");
  626. return NULL;
  627. }
  628. ast_mutex_unlock(&p->lock);
  629. ast_mutex_lock(&ooh323c_cmd_lock);
  630. ooMakeCall(data, p->callToken, AST_MAX_EXTENSION, NULL);
  631. ast_mutex_unlock(&ooh323c_cmd_lock);
  632. }
  633. restart_monitor();
  634. if (gH323Debug)
  635. ast_verbose("+++ ooh323_request\n");
  636. return chan;
  637. }
  638. static struct ooh323_pvt* find_call(ooCallData *call)
  639. {
  640. struct ooh323_pvt *p;
  641. if (gH323Debug)
  642. ast_verbose("--- find_call\n");
  643. ast_mutex_lock(&iflock);
  644. for (p = iflist; p; p = p->next) {
  645. if (p->callToken && !strcmp(p->callToken, call->callToken)) {
  646. break;
  647. }
  648. }
  649. ast_mutex_unlock(&iflock);
  650. if (gH323Debug)
  651. ast_verbose("+++ find_call\n");
  652. return p;
  653. }
  654. struct ooh323_user *find_user(const char * name, const char* ip)
  655. {
  656. struct ooh323_user *user;
  657. if (gH323Debug)
  658. ast_verbose("--- find_user: %s, %s\n",name,ip);
  659. ast_mutex_lock(&userl.lock);
  660. for (user = userl.users; user; user = user->next) {
  661. if (ip && user->mUseIP && !strcmp(user->mIP, ip)) {
  662. break;
  663. }
  664. if (name && !strcmp(user->name, name)) {
  665. break;
  666. }
  667. }
  668. ast_mutex_unlock(&userl.lock);
  669. if (gH323Debug)
  670. ast_verbose("+++ find_user\n");
  671. return user;
  672. }
  673. struct ooh323_peer *find_friend(const char *name, int port)
  674. {
  675. struct ooh323_peer *peer;
  676. if (gH323Debug)
  677. ast_verbose("--- find_friend \"%s\"\n", name);
  678. ast_mutex_lock(&peerl.lock);
  679. for (peer = peerl.peers; peer; peer = peer->next) {
  680. if (gH323Debug) {
  681. ast_verbose(" comparing with \"%s\"\n", peer->ip);
  682. }
  683. if (!strcmp(peer->ip, name)) {
  684. if (port <= 0 || (port > 0 && peer->port == port)) {
  685. break;
  686. }
  687. }
  688. }
  689. ast_mutex_unlock(&peerl.lock);
  690. if (gH323Debug) {
  691. if (peer) {
  692. ast_verbose(" found matching friend\n");
  693. }
  694. ast_verbose("+++ find_friend \"%s\"\n", name);
  695. }
  696. return peer;
  697. }
  698. struct ooh323_peer *find_peer(const char * name, int port)
  699. {
  700. struct ooh323_peer *peer;
  701. if (gH323Debug)
  702. ast_verbose("--- find_peer \"%s\"\n", name);
  703. ast_mutex_lock(&peerl.lock);
  704. for (peer = peerl.peers; peer; peer = peer->next) {
  705. if (gH323Debug) {
  706. ast_verbose(" comparing with \"%s\"\n", peer->ip);
  707. }
  708. if (!strcasecmp(peer->name, name))
  709. break;
  710. if (peer->h323id && !strcasecmp(peer->h323id, name))
  711. break;
  712. if (peer->e164 && !strcasecmp(peer->e164, name))
  713. break;
  714. /*
  715. if (!strcmp(peer->ip, name)) {
  716. if (port > 0 && peer->port == port) { break; }
  717. else if (port <= 0) { break; }
  718. }
  719. */
  720. }
  721. ast_mutex_unlock(&peerl.lock);
  722. if (gH323Debug) {
  723. if (peer) {
  724. ast_verbose(" found matching peer\n");
  725. }
  726. ast_verbose("+++ find_peer \"%s\"\n", name);
  727. }
  728. return peer;
  729. }
  730. static int ooh323_digit_begin(struct ast_channel *chan, char digit)
  731. {
  732. char dtmf[2];
  733. struct ooh323_pvt *p = (struct ooh323_pvt *) chan->tech_pvt;
  734. if (gH323Debug)
  735. ast_verbose("--- ooh323_digit_begin\n");
  736. if (!p) {
  737. ast_log(LOG_ERROR, "No private structure for call\n");
  738. return -1;
  739. }
  740. ast_mutex_lock(&p->lock);
  741. if (p->rtp && ((p->dtmfmode & H323_DTMF_RFC2833) || (p->dtmfmode & H323_DTMF_CISCO))) {
  742. ast_rtp_instance_dtmf_begin(p->rtp, digit);
  743. } else if (((p->dtmfmode & H323_DTMF_Q931) ||
  744. (p->dtmfmode & H323_DTMF_H245ALPHANUMERIC) ||
  745. (p->dtmfmode & H323_DTMF_H245SIGNAL))) {
  746. dtmf[0] = digit;
  747. dtmf[1] = '\0';
  748. ooSendDTMFDigit(p->callToken, dtmf);
  749. }
  750. ast_mutex_unlock(&p->lock);
  751. if (gH323Debug)
  752. ast_verbose("+++ ooh323_digit_begin\n");
  753. return 0;
  754. }
  755. static int ooh323_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
  756. {
  757. struct ooh323_pvt *p = (struct ooh323_pvt *) chan->tech_pvt;
  758. if (gH323Debug)
  759. ast_verbose("--- ooh323_digit_end\n");
  760. if (!p) {
  761. ast_log(LOG_ERROR, "No private structure for call\n");
  762. return -1;
  763. }
  764. ast_mutex_lock(&p->lock);
  765. if (p->rtp && ((p->dtmfmode & H323_DTMF_RFC2833) || (p->dtmfmode & H323_DTMF_CISCO)) )
  766. ast_rtp_instance_dtmf_end(p->rtp, digit);
  767. ast_mutex_unlock(&p->lock);
  768. if (gH323Debug)
  769. ast_verbose("+++ ooh323_digit_end\n");
  770. return 0;
  771. }
  772. static int ooh323_call(struct ast_channel *ast, char *dest, int timeout)
  773. {
  774. struct ooh323_pvt *p = ast->tech_pvt;
  775. char destination[256];
  776. int res=0, i;
  777. const char *val = NULL;
  778. ooCallOptions opts = {
  779. .fastStart = TRUE,
  780. .tunneling = TRUE,
  781. .disableGk = TRUE,
  782. .callMode = OO_CALLMODE_AUDIOCALL,
  783. .transfercap = 0
  784. };
  785. if (gH323Debug)
  786. ast_verbose("--- ooh323_call- %s\n", dest);
  787. if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
  788. ast_log(LOG_WARNING, "ooh323_call called on %s, neither down nor "
  789. "reserved\n", ast->name);
  790. return -1;
  791. }
  792. ast_mutex_lock(&p->lock);
  793. ast_set_flag(p, H323_OUTGOING);
  794. if (ast->connected.id.number.valid && ast->connected.id.number.str) {
  795. free(p->callerid_num);
  796. p->callerid_num = strdup(ast->connected.id.number.str);
  797. }
  798. if (ast->connected.id.name.valid && ast->connected.id.name.str) {
  799. free(p->callerid_name);
  800. p->callerid_name = strdup(ast->connected.id.name.str);
  801. } else if (ast->connected.id.number.valid && ast->connected.id.number.str) {
  802. free(p->callerid_name);
  803. p->callerid_name = strdup(ast->connected.id.number.str);
  804. } else {
  805. ast->connected.id.name.valid = 1;
  806. free(ast->connected.id.name.str);
  807. ast->connected.id.name.str = strdup(gCallerID);
  808. free(p->callerid_name);
  809. p->callerid_name = strdup(ast->connected.id.name.str);
  810. }
  811. /* Retrieve vars */
  812. if ((val = pbx_builtin_getvar_helper(ast, "CALLER_H323ID"))) {
  813. ast_copy_string(p->caller_h323id, val, sizeof(p->caller_h323id));
  814. }
  815. if ((val = pbx_builtin_getvar_helper(ast, "CALLER_H323DIALEDDIGITS"))) {
  816. ast_copy_string(p->caller_dialedDigits, val, sizeof(p->caller_dialedDigits));
  817. if(!p->callerid_num)
  818. p->callerid_num = strdup(val);
  819. }
  820. if ((val = pbx_builtin_getvar_helper(ast, "CALLER_H323EMAIL"))) {
  821. ast_copy_string(p->caller_email, val, sizeof(p->caller_email));
  822. }
  823. if ((val = pbx_builtin_getvar_helper(ast, "CALLER_H323URL"))) {
  824. ast_copy_string(p->caller_url, val, sizeof(p->caller_url));
  825. }
  826. if (p->host && p->port != 0)
  827. snprintf(destination, sizeof(destination), "%s:%d", p->host, p->port);
  828. else if (p->host)
  829. snprintf(destination, sizeof(destination), "%s", p->host);
  830. else
  831. ast_copy_string(destination, dest, sizeof(destination));
  832. destination[sizeof(destination)-1]='\0';
  833. opts.transfercap = ast->transfercapability;
  834. opts.fastStart = p->faststart;
  835. opts.tunneling = p->h245tunneling;
  836. for (i=0;i<480 && !isRunning(p->callToken);i++) usleep(12000);
  837. if(OO_TESTFLAG(p->flags, H323_DISABLEGK)) {
  838. res = ooRunCall(destination, p->callToken, AST_MAX_EXTENSION, &opts);
  839. } else {
  840. res = ooRunCall(destination, p->callToken, AST_MAX_EXTENSION, NULL);
  841. }
  842. ast_mutex_unlock(&p->lock);
  843. if (res != OO_OK) {
  844. ast_log(LOG_ERROR, "Failed to make call\n");
  845. return -1; /* ToDO: cleanup */
  846. }
  847. if (gH323Debug)
  848. ast_verbose("+++ ooh323_call\n");
  849. return 0;
  850. }
  851. static int ooh323_hangup(struct ast_channel *ast)
  852. {
  853. struct ooh323_pvt *p = ast->tech_pvt;
  854. int q931cause = AST_CAUSE_NORMAL_CLEARING;
  855. if (gH323Debug)
  856. ast_verbose("--- ooh323_hangup\n");
  857. if (p) {
  858. ast_mutex_lock(&p->lock);
  859. if (ast->hangupcause) {
  860. q931cause = ast->hangupcause;
  861. } else {
  862. const char *cause = pbx_builtin_getvar_helper(ast, "DIALSTATUS");
  863. if (cause) {
  864. if (!strcmp(cause, "CONGESTION")) {
  865. q931cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
  866. } else if (!strcmp(cause, "BUSY")) {
  867. q931cause = AST_CAUSE_USER_BUSY;
  868. } else if (!strcmp(cause, "CHANISUNVAIL")) {
  869. q931cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
  870. } else if (!strcmp(cause, "NOANSWER")) {
  871. q931cause = AST_CAUSE_NO_ANSWER;
  872. } else if (!strcmp(cause, "CANCEL")) {
  873. q931cause = AST_CAUSE_CALL_REJECTED;
  874. }
  875. }
  876. }
  877. if (gH323Debug)
  878. ast_verbose(" hanging %s with cause: %d\n", p->username, q931cause);
  879. ast->tech_pvt = NULL;
  880. if (!ast_test_flag(p, H323_ALREADYGONE)) {
  881. ooHangCall(p->callToken,
  882. ooh323_convert_hangupcause_asteriskToH323(q931cause), q931cause);
  883. ast_set_flag(p, H323_ALREADYGONE);
  884. /* ast_mutex_unlock(&p->lock); */
  885. } else
  886. ast_set_flag(p, H323_NEEDDESTROY);
  887. /* detach channel here */
  888. if (p->owner) {
  889. p->owner->tech_pvt = NULL;
  890. p->owner = NULL;
  891. ast_module_unref(myself);
  892. }
  893. ast_mutex_unlock(&p->lock);
  894. ast_mutex_lock(&usecnt_lock);
  895. usecnt--;
  896. ast_mutex_unlock(&usecnt_lock);
  897. /* Notify the module monitors that use count for resource has changed */
  898. ast_update_use_count();
  899. } else {
  900. ast_debug(1, "No call to hangup\n" );
  901. }
  902. if (gH323Debug)
  903. ast_verbose("+++ ooh323_hangup\n");
  904. return 0;
  905. }
  906. static int ooh323_answer(struct ast_channel *ast)
  907. {
  908. struct ooh323_pvt *p = ast->tech_pvt;
  909. char *callToken = (char *)NULL;
  910. if (gH323Debug)
  911. ast_verbose("--- ooh323_answer\n");
  912. if (p) {
  913. ast_mutex_lock(&p->lock);
  914. callToken = (p->callToken ? strdup(p->callToken) : NULL);
  915. if (ast->_state != AST_STATE_UP) {
  916. ast_channel_lock(ast);
  917. if (!p->alertsent) {
  918. if (gH323Debug) {
  919. ast_debug(1, "Sending forced ringback for %s, res = %d\n",
  920. callToken, ooManualRingback(callToken));
  921. } else {
  922. ooManualRingback(callToken);
  923. }
  924. p->alertsent = 1;
  925. }
  926. ast_setstate(ast, AST_STATE_UP);
  927. if (option_debug)
  928. ast_debug(1, "ooh323_answer(%s)\n", ast->name);
  929. ast_channel_unlock(ast);
  930. ooAnswerCall(p->callToken);
  931. }
  932. ast_mutex_unlock(&p->lock);
  933. }
  934. if (gH323Debug)
  935. ast_verbose("+++ ooh323_answer\n");
  936. return 0;
  937. }
  938. static struct ast_frame *ooh323_read(struct ast_channel *ast)
  939. {
  940. struct ast_frame *fr;
  941. static struct ast_frame null_frame = { AST_FRAME_NULL, };
  942. struct ooh323_pvt *p = ast->tech_pvt;
  943. if (!p) return &null_frame;
  944. ast_mutex_lock(&p->lock);
  945. if (p->rtp)
  946. fr = ooh323_rtp_read(ast, p);
  947. else
  948. fr = &null_frame;
  949. /* time(&p->lastrtprx); */
  950. ast_mutex_unlock(&p->lock);
  951. return fr;
  952. }
  953. static int ooh323_write(struct ast_channel *ast, struct ast_frame *f)
  954. {
  955. struct ooh323_pvt *p = ast->tech_pvt;
  956. int res = 0;
  957. char buf[256];
  958. if (p) {
  959. ast_mutex_lock(&p->lock);
  960. if (f->frametype == AST_FRAME_MODEM) {
  961. ast_debug(1, "Send UDPTL %d/%d len %d for %s\n",
  962. f->frametype, f->subclass.integer, f->datalen, ast->name);
  963. if (p->udptl)
  964. res = ast_udptl_write(p->udptl, f);
  965. ast_mutex_unlock(&p->lock);
  966. return res;
  967. }
  968. if (f->frametype == AST_FRAME_VOICE) {
  969. /* sending progress for first */
  970. if (!ast_test_flag(p, H323_OUTGOING) && !p->progsent &&
  971. p->callToken) {
  972. ooManualProgress(p->callToken);
  973. p->progsent = 1;
  974. }
  975. if (!(ast_format_cap_iscompatible(ast->nativeformats, &f->subclass.format))) {
  976. if (!(ast_format_cap_is_empty(ast->nativeformats))) {
  977. ast_log(LOG_WARNING,
  978. "Asked to transmit frame type %s, while native formats is %s (read/write = %s/%s)\n",
  979. ast_getformatname(&f->subclass.format),
  980. ast_getformatname_multiple(buf, sizeof(buf), ast->nativeformats),
  981. ast_getformatname(&ast->readformat),
  982. ast_getformatname(&ast->writeformat));
  983. ast_set_write_format(ast, &f->subclass.format);
  984. } else {
  985. /* ast_set_write_format(ast, f->subclass);
  986. ast->nativeformats = f->subclass; */
  987. }
  988. ast_mutex_unlock(&p->lock);
  989. return 0;
  990. }
  991. if (p->rtp)
  992. res = ast_rtp_instance_write(p->rtp, f);
  993. ast_mutex_unlock(&p->lock);
  994. } else if (f->frametype == AST_FRAME_IMAGE) {
  995. ast_mutex_unlock(&p->lock);
  996. return 0;
  997. } else {
  998. ast_log(LOG_WARNING, "Can't send %d type frames with OOH323 write\n",
  999. f->frametype);
  1000. ast_mutex_unlock(&p->lock);
  1001. return 0;
  1002. }
  1003. }
  1004. return res;
  1005. }
  1006. static int ooh323_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
  1007. {
  1008. struct ooh323_pvt *p = (struct ooh323_pvt *) ast->tech_pvt;
  1009. char *callToken = (char *)NULL;
  1010. int res = -1;
  1011. if (!p) return -1;
  1012. ast_mutex_lock(&p->lock);
  1013. callToken = (p->callToken ? strdup(p->callToken) : NULL);
  1014. ast_mutex_unlock(&p->lock);
  1015. if (!callToken) {
  1016. if (gH323Debug)
  1017. ast_verbose(" ooh323_indicate - No callToken\n");
  1018. return -1;
  1019. }
  1020. if (gH323Debug)
  1021. ast_verbose("----- ooh323_indicate %d on call %s\n", condition, callToken);
  1022. ast_mutex_lock(&p->lock);
  1023. switch (condition) {
  1024. case AST_CONTROL_CONGESTION:
  1025. if (!ast_test_flag(p, H323_ALREADYGONE)) {
  1026. ooHangCall(callToken, OO_REASON_LOCAL_CONGESTED,
  1027. AST_CAUSE_SWITCH_CONGESTION);
  1028. ast_set_flag(p, H323_ALREADYGONE);
  1029. }
  1030. break;
  1031. case AST_CONTROL_BUSY:
  1032. if (!ast_test_flag(p, H323_ALREADYGONE)) {
  1033. ooHangCall(callToken, OO_REASON_LOCAL_BUSY, AST_CAUSE_USER_BUSY);
  1034. ast_set_flag(p, H323_ALREADYGONE);
  1035. }
  1036. break;
  1037. case AST_CONTROL_HOLD:
  1038. ast_moh_start(ast, data, NULL);
  1039. break;
  1040. case AST_CONTROL_UNHOLD:
  1041. ast_moh_stop(ast);
  1042. break;
  1043. case AST_CONTROL_PROGRESS:
  1044. if (ast->_state != AST_STATE_UP) {
  1045. if (!p->progsent) {
  1046. if (gH323Debug) {
  1047. ast_debug(1, "Sending manual progress for %s, res = %d\n", callToken,
  1048. ooManualProgress(callToken));
  1049. } else {
  1050. ooManualProgress(callToken);
  1051. }
  1052. p->progsent = 1;
  1053. }
  1054. }
  1055. break;
  1056. case AST_CONTROL_RINGING:
  1057. if (ast->_state == AST_STATE_RING || ast->_state == AST_STATE_RINGING) {
  1058. if (!p->alertsent) {
  1059. if (gH323Debug) {
  1060. ast_debug(1, "Sending manual ringback for %s, res = %d\n",
  1061. callToken,
  1062. ooManualRingback(callToken));
  1063. } else {
  1064. ooManualRingback(callToken);
  1065. }
  1066. p->alertsent = 1;
  1067. }
  1068. p->alertsent = 1;
  1069. }
  1070. break;
  1071. case AST_CONTROL_SRCUPDATE:
  1072. if (p->rtp) {
  1073. ast_rtp_instance_update_source(p->rtp);
  1074. }
  1075. break;
  1076. case AST_CONTROL_SRCCHANGE:
  1077. if (p->rtp) {
  1078. ast_rtp_instance_change_source(p->rtp);
  1079. }
  1080. break;
  1081. case AST_CONTROL_CONNECTED_LINE:
  1082. if (!ast->connected.id.name.valid
  1083. || ast_strlen_zero(ast->connected.id.name.str)) {
  1084. break;
  1085. }
  1086. if (gH323Debug) {
  1087. ast_debug(1, "Sending connected line info for %s (%s)\n",
  1088. callToken, ast->connected.id.name.str);
  1089. }
  1090. ooSetANI(callToken, ast->connected.id.name.str);
  1091. break;
  1092. case AST_CONTROL_T38_PARAMETERS:
  1093. if (p->t38support != T38_ENABLED) {
  1094. struct ast_control_t38_parameters parameters = { .request_response = 0 };
  1095. parameters.request_response = AST_T38_REFUSED;
  1096. ast_queue_control_data(ast, AST_CONTROL_T38_PARAMETERS,
  1097. &parameters, sizeof(parameters));
  1098. break;
  1099. }
  1100. if (datalen != sizeof(struct ast_control_t38_parameters)) {
  1101. ast_log(LOG_ERROR, "Invalid datalen for AST_CONTROL_T38. "
  1102. "Expected %d, got %d\n",
  1103. (int)sizeof(enum ast_control_t38), (int)datalen);
  1104. } else {
  1105. const struct ast_control_t38_parameters *parameters = data;
  1106. struct ast_control_t38_parameters our_parameters;
  1107. enum ast_control_t38 message = parameters->request_response;
  1108. switch (message) {
  1109. case AST_T38_NEGOTIATED:
  1110. if (p->faxmode) {
  1111. res = 0;
  1112. break;
  1113. }
  1114. case AST_T38_REQUEST_NEGOTIATE:
  1115. if (p->faxmode) {
  1116. /* T.38 already negotiated */
  1117. our_parameters.request_response = AST_T38_NEGOTIATED;
  1118. our_parameters.max_ifp = ast_udptl_get_far_max_ifp(p->udptl);
  1119. our_parameters.rate = AST_T38_RATE_14400;
  1120. ast_queue_control_data(p->owner, AST_CONTROL_T38_PARAMETERS, &our_parameters, sizeof(our_parameters));
  1121. } else if (!p->chmodepend) {
  1122. p->chmodepend = 1;
  1123. ooRequestChangeMode(p->callToken, 1);
  1124. res = 0;
  1125. }
  1126. break;
  1127. case AST_T38_REQUEST_TERMINATE:
  1128. if (!p->faxmode) {
  1129. /* T.38 already terminated */
  1130. our_parameters.request_response = AST_T38_TERMINATED;
  1131. ast_queue_control_data(p->owner, AST_CONTROL_T38_PARAMETERS, &our_parameters, sizeof(our_parameters));
  1132. } else if (!p->chmodepend) {
  1133. p->chmodepend = 1;
  1134. ooRequestChangeMode(p->callToken, 0);
  1135. res = 0;
  1136. }
  1137. break;
  1138. case AST_T38_REQUEST_PARMS:
  1139. our_parameters.request_response = AST_T38_REQUEST_PARMS;
  1140. our_parameters.max_ifp = ast_udptl_get_far_max_ifp(p->udptl);
  1141. our_parameters.rate = AST_T38_RATE_14400;
  1142. ast_queue_control_data(p->owner, AST_CONTROL_T38_PARAMETERS, &our_parameters, sizeof(our_parameters));
  1143. res = AST_T38_REQUEST_PARMS;
  1144. break;
  1145. default:
  1146. ;
  1147. }
  1148. }
  1149. break;
  1150. case AST_CONTROL_PROCEEDING:
  1151. case -1:
  1152. break;
  1153. default:
  1154. ast_log(LOG_WARNING, "Don't know how to indicate condition %d on %s\n",
  1155. condition, callToken);
  1156. }
  1157. ast_mutex_unlock(&p->lock);
  1158. if (gH323Debug)
  1159. ast_verbose("++++ ooh323_indicate %d on %s\n", condition, callToken);
  1160. free(callToken);
  1161. return res;
  1162. }
  1163. static int ooh323_queryoption(struct ast_channel *ast, int option, void *data, int *datalen)
  1164. {
  1165. struct ooh323_pvt *p = (struct ooh323_pvt *) ast->tech_pvt;
  1166. int res = -1;
  1167. enum ast_t38_state state = T38_STATE_UNAVAILABLE;
  1168. char* cp;
  1169. if (!p) return -1;
  1170. ast_mutex_lock(&p->lock);
  1171. if (gH323Debug)
  1172. ast_verbose("----- ooh323_queryoption %d on channel %s\n", option, ast->name);
  1173. switch (option) {
  1174. case AST_OPTION_T38_STATE:
  1175. if (*datalen != sizeof(enum ast_t38_state)) {
  1176. ast_log(LOG_ERROR, "Invalid datalen for AST_OPTION_T38_STATE option."
  1177. " Expected %d, got %d\n", (int)sizeof(enum ast_t38_state), *datalen);
  1178. break;
  1179. }
  1180. if (p->t38support != T38_DISABLED) {
  1181. if (p->faxmode) {
  1182. state = (p->chmodepend) ? T38_STATE_NEGOTIATING : T38_STATE_NEGOTIATED;
  1183. } else {
  1184. state = T38_STATE_UNKNOWN;
  1185. }
  1186. }
  1187. *((enum ast_t38_state *) data) = state;
  1188. res = 0;
  1189. break;
  1190. case AST_OPTION_DIGIT_DETECT:
  1191. cp = (char *) data;
  1192. *cp = p->vad ? 1 : 0;
  1193. ast_debug(1, "Reporting digit detection %sabled on %s\n",
  1194. *cp ? "en" : "dis", ast->name);
  1195. res = 0;
  1196. break;
  1197. default: ;
  1198. }
  1199. if (gH323Debug)
  1200. ast_verbose("+++++ ooh323_queryoption %d on channel %s\n", option, ast->name);
  1201. ast_mutex_unlock(&p->lock);
  1202. return res;
  1203. }
  1204. static int ooh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  1205. {
  1206. struct ooh323_pvt *p = newchan->tech_pvt;
  1207. if (!p) return -1;
  1208. if (gH323Debug)
  1209. ast_verbose("--- ooh323c ooh323_fixup\n");
  1210. ast_mutex_lock(&p->lock);
  1211. if (p->owner != oldchan) {
  1212. ast_log(LOG_WARNING, "Old channel wasn't %p but was %p\n", oldchan, p->owner);
  1213. ast_mutex_unlock(&p->lock);
  1214. return -1;
  1215. }
  1216. if (p->owner == oldchan) {
  1217. p->owner = newchan;
  1218. } else {
  1219. p->owner = oldchan;
  1220. }
  1221. ast_mutex_unlock(&p->lock);
  1222. if (gH323Debug)
  1223. ast_verbose("+++ ooh323c ooh323_fixup \n");
  1224. return 0;
  1225. }
  1226. void ooh323_set_write_format(ooCallData *call, struct ast_format *fmt, int txframes)
  1227. {
  1228. struct ooh323_pvt *p = NULL;
  1229. char formats[FORMAT_STRING_SIZE];
  1230. if (gH323Debug)
  1231. ast_verbose("--- ooh323_update_writeformat %s/%d\n",
  1232. ast_getformatname(fmt), txframes);
  1233. p = find_call(call);
  1234. if (!p) {
  1235. ast_log(LOG_ERROR, "No matching call found for %s\n", call->callToken);
  1236. return;
  1237. }
  1238. ast_mutex_lock(&p->lock);
  1239. ast_format_copy(&(p->writeformat), fmt);
  1240. if (p->owner) {
  1241. while (p->owner && ast_channel_trylock(p->owner)) {
  1242. ast_debug(1,"Failed to grab lock, trying again\n");
  1243. DEADLOCK_AVOIDANCE(&p->lock);
  1244. }
  1245. if (!p->owner) {
  1246. ast_mutex_unlock(&p->lock);
  1247. ast_log(LOG_ERROR, "Channel has no owner\n");
  1248. return;
  1249. }
  1250. if (gH323Debug)
  1251. ast_verbose("Writeformat before update %s/%s\n",
  1252. ast_getformatname(&p->owner->writeformat),
  1253. ast_getformatname_multiple(formats, sizeof(formats), p->owner->nativeformats));
  1254. if (txframes)
  1255. ast_codec_pref_setsize(&p->prefs, fmt, txframes);
  1256. ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(p->rtp), p->rtp, &p->prefs);
  1257. if (p->dtmfmode & H323_DTMF_RFC2833 && p->dtmfcodec) {
  1258. ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(p->rtp),
  1259. p->rtp, p->dtmfcodec, "audio", "telephone-event", 0);
  1260. }
  1261. if (p->dtmfmode & H323_DTMF_CISCO && p->dtmfcodec) {
  1262. ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(p->rtp),
  1263. p->rtp, p->dtmfcodec, "audio", "cisco-telephone-event", 0);
  1264. }
  1265. ast_format_cap_set(p->owner->nativeformats, fmt);
  1266. ast_set_write_format(p->owner, &p->owner->writeformat);
  1267. ast_set_read_format(p->owner, &p->owner->readformat);
  1268. ast_channel_unlock(p->owner);
  1269. } else
  1270. ast_log(LOG_ERROR, "No owner found\n");
  1271. ast_mutex_unlock(&p->lock);
  1272. if (gH323Debug)
  1273. ast_verbose("+++ ooh323_update_writeformat\n");
  1274. }
  1275. void ooh323_set_read_format(ooCallData *call, struct ast_format *fmt)
  1276. {
  1277. struct ooh323_pvt *p = NULL;
  1278. if (gH323Debug)
  1279. ast_verbose("--- ooh323_update_readformat %s\n",
  1280. ast_getformatname(fmt));
  1281. p = find_call(call);
  1282. if (!p) {
  1283. ast_log(LOG_ERROR, "No matching call found for %s\n", call->callToken);
  1284. return;
  1285. }
  1286. ast_mutex_lock(&p->lock);
  1287. ast_format_copy(&(p->readformat), fmt);
  1288. if (p->owner) {
  1289. while (p->owner && ast_channel_trylock(p->owner)) {
  1290. ast_debug(1,"Failed to grab lock, trying again\n");
  1291. DEADLOCK_AVOIDANCE(&p->lock);
  1292. }
  1293. if (!p->owner) {
  1294. ast_mutex_unlock(&p->lock);
  1295. ast_log(LOG_ERROR, "Channel has no owner\n");
  1296. return;
  1297. }
  1298. if (gH323Debug)
  1299. ast_verbose("Readformat before update %s\n",
  1300. ast_getformatname(&p->owner->readformat));
  1301. ast_format_cap_set(p->owner->nativeformats, fmt);
  1302. ast_set_read_format(p->owner, &p->owner->readformat);
  1303. ast_channel_unlock(p->owner);
  1304. } else
  1305. ast_log(LOG_ERROR, "No owner found\n");
  1306. ast_mutex_unlock(&p->lock);
  1307. if (gH323Debug)
  1308. ast_verbose("+++ ooh323_update_readformat\n");
  1309. }
  1310. int onAlerting(ooCallData *call)
  1311. {
  1312. struct ooh323_pvt *p = NULL;
  1313. struct ast_channel *c = NULL;
  1314. if (gH323Debug)
  1315. ast_verbose("--- onAlerting %s\n", call->callToken);
  1316. p = find_call(call);
  1317. if(!p) {
  1318. ast_log(LOG_ERROR, "No matching call found\n");
  1319. return -1;
  1320. }
  1321. ast_mutex_lock(&p->lock);
  1322. if (!p->owner) {
  1323. ast_mutex_unlock(&p->lock);
  1324. ast_log(LOG_ERROR, "Channel has no owner\n");
  1325. return 0;
  1326. }
  1327. while (p->owner && ast_channel_trylock(p->owner)) {
  1328. ast_debug(1, "Failed to grab lock, trying again\n");
  1329. DEADLOCK_AVOIDANCE(&p->lock);
  1330. }
  1331. if (!p->owner) {
  1332. ast_mutex_unlock(&p->lock);
  1333. ast_log(LOG_ERROR, "Channel has no owner\n");
  1334. return 0;
  1335. }
  1336. c = p->owner;
  1337. if (call->remoteDisplayName) {
  1338. struct ast_party_connected_line connected;
  1339. struct ast_set_party_connected_line update_connected;
  1340. memset(&update_connected, 0, sizeof(update_connected));
  1341. update_connected.id.name = 1;
  1342. ast_party_connected_line_init(&connected);
  1343. connected.id.name.valid = 1;
  1344. connected.id.name.str = (char *) call->remoteDisplayName;
  1345. connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
  1346. ast_channel_queue_connected_line_update(c, &connected, &update_connected);
  1347. }
  1348. if (c->_state != AST_STATE_UP)
  1349. ast_setstate(c, AST_STATE_RINGING);
  1350. ast_queue_control(c, AST_CONTROL_RINGING);
  1351. ast_channel_unlock(c);
  1352. ast_mutex_unlock(&p->lock);
  1353. if (gH323Debug)
  1354. ast_verbose("+++ onAlerting %s\n", call->callToken);
  1355. return OO_OK;
  1356. }
  1357. int onProgress(ooCallData *call)
  1358. {
  1359. struct ooh323_pvt *p = NULL;
  1360. struct ast_channel *c = NULL;
  1361. if (gH323Debug)
  1362. ast_verbose("--- onProgress %s\n", call->callToken);
  1363. p = find_call(call);
  1364. if(!p) {
  1365. ast_log(LOG_ERROR, "No matching call found\n");
  1366. return -1;
  1367. }
  1368. ast_mutex_lock(&p->lock);
  1369. if (!p->owner) {
  1370. ast_mutex_unlock(&p->lock);
  1371. ast_log(LOG_ERROR, "Channel has no owner\n");
  1372. return 0;
  1373. }
  1374. while (p->owner && ast_channel_trylock(p->owner)) {
  1375. ast_debug(1, "Failed to grab lock, trying again\n");
  1376. DEADLOCK_AVOIDANCE(&p->lock);
  1377. }
  1378. if (!p->owner) {
  1379. ast_mutex_unlock(&p->lock);
  1380. ast_log(LOG_ERROR, "Channel has no owner\n");
  1381. return 0;
  1382. }
  1383. c = p->owner;
  1384. if (call->remoteDisplayName) {
  1385. struct ast_party_connected_line connected;
  1386. struct ast_set_party_connected_line update_connected;
  1387. memset(&update_connected, 0, sizeof(update_connected));
  1388. update_connected.id.name = 1;
  1389. ast_party_connected_line_init(&connected);
  1390. connected.id.name.valid = 1;
  1391. connected.id.name.str = (char *) call->remoteDisplayName;
  1392. connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
  1393. ast_channel_queue_connected_line_update(c, &connected, &update_connected);
  1394. }
  1395. if (c->_state != AST_STATE_UP)
  1396. ast_setstate(c, AST_STATE_RINGING);
  1397. ast_queue_control(c, AST_CONTROL_PROGRESS);
  1398. ast_channel_unlock(c);
  1399. ast_mutex_unlock(&p->lock);
  1400. if (gH323Debug)
  1401. ast_verbose("+++ onProgress %s\n", call->callToken);
  1402. return OO_OK;
  1403. }
  1404. /**
  1405. * Callback for sending digits from H.323 up to asterisk
  1406. *
  1407. */
  1408. int ooh323_onReceivedDigit(OOH323CallData *call, const char *digit)
  1409. {
  1410. struct ooh323_pvt *p = NULL;
  1411. struct ast_frame f;
  1412. int res;
  1413. ast_debug(1, "Received Digit: %c\n", digit[0]);
  1414. p = find_call(call);
  1415. if (!p) {
  1416. ast_log(LOG_ERROR, "Failed to find a matching call.\n");
  1417. return -1;
  1418. }
  1419. if (!p->owner) {
  1420. ast_log(LOG_ERROR, "Channel has no owner\n");
  1421. return -1;
  1422. }
  1423. ast_mutex_lock(&p->lock);
  1424. memset(&f, 0, sizeof(f));
  1425. f.frametype = AST_FRAME_DTMF;
  1426. f.subclass.integer = digit[0];
  1427. f.datalen = 0;
  1428. f.samples = 800;
  1429. f.offset = 0;
  1430. f.data.ptr = NULL;
  1431. f.mallocd = 0;
  1432. f.src = "SEND_DIGIT";
  1433. while (p->owner && ast_channel_trylock(p->owner)) {
  1434. ast_debug(1, "Failed to grab lock, trying again\n");
  1435. DEADLOCK_AVOIDANCE(&p->lock);
  1436. }
  1437. if (!p->owner) {
  1438. ast_mutex_unlock(&p->lock);
  1439. ast_log(LOG_ERROR, "Channel has no owner\n");
  1440. return 0;
  1441. }
  1442. res = ast_queue_frame(p->owner, &f);
  1443. ast_channel_unlock(p->owner);
  1444. ast_mutex_unlock(&p->lock);
  1445. return res;
  1446. }
  1447. int ooh323_onReceivedSetup(ooCallData *call, Q931Message *pmsg)
  1448. {
  1449. struct ooh323_pvt *p = NULL;
  1450. struct ooh323_user *user = NULL;
  1451. struct ast_channel *c = NULL;
  1452. ooAliases *alias = NULL;
  1453. char *at = NULL;
  1454. char number [OO_MAX_NUMBER_LENGTH];
  1455. if (gH323Debug)
  1456. ast_verbose("--- ooh323_onReceivedSetup %s\n", call->callToken);
  1457. if (!(p = ooh323_alloc(call->callReference, call->callToken))) {
  1458. ast_log(LOG_ERROR, "Failed to create a new call.\n");
  1459. return -1;
  1460. }
  1461. ast_mutex_lock(&p->lock);
  1462. ast_clear_flag(p, H323_OUTGOING);
  1463. if (call->remoteDisplayName) {
  1464. p->callerid_name = strdup(call->remoteDisplayName);
  1465. }
  1466. if (ooCallGetCallingPartyNumber(call, number, OO_MAX_NUMBER_LENGTH) == OO_OK) {
  1467. p->callerid_num = strdup(number);
  1468. }
  1469. if (call->remoteAliases) {
  1470. for (alias = call->remoteAliases; alias; alias = alias->next) {
  1471. if (alias->type == T_H225AliasAddress_h323_ID) {
  1472. if (!p->callerid_name) {
  1473. p->callerid_name = strdup(alias->value);
  1474. }
  1475. ast_copy_string(p->caller_h323id, alias->value, sizeof(p->caller_h323id));
  1476. }
  1477. else if(alias->type == T_H225AliasAddress_dialedDigits)
  1478. {
  1479. if(!p->callerid_num)
  1480. p->callerid_num = strdup(alias->value);
  1481. ast_copy_string(p->caller_dialedDigits, alias->value,
  1482. sizeof(p->caller_dialedDigits));
  1483. }
  1484. else if(alias->type == T_H225AliasAddress_email_ID)
  1485. {
  1486. ast_copy_string(p->caller_email, alias->value, sizeof(p->caller_email));
  1487. }
  1488. else if(alias->type == T_H225AliasAddress_url_ID)
  1489. {
  1490. ast_copy_string(p->caller_url, alias->value, sizeof(p->caller_url));
  1491. }
  1492. }
  1493. }
  1494. number[0] = '\0';
  1495. if(ooCallGetCalledPartyNumber(call, number, OO_MAX_NUMBER_LENGTH)== OO_OK) {
  1496. strncpy(p->exten, number, sizeof(p->exten)-1);
  1497. } else {
  1498. update_our_aliases(call, p);
  1499. if (!ast_strlen_zero(p->callee_dialedDigits)) {
  1500. ast_copy_string(p->exten, p->callee_dialedDigits, sizeof(p->exten));
  1501. } else if(!ast_strlen_zero(p->callee_h323id)) {
  1502. ast_copy_string(p->exten, p->callee_h323id, sizeof(p->exten));
  1503. } else if(!ast_strlen_zero(p->callee_email)) {
  1504. ast_copy_string(p->exten, p->callee_email, sizeof(p->exten));
  1505. if ((at = strchr(p->exten, '@'))) {
  1506. *at = '\0';
  1507. }
  1508. }
  1509. }
  1510. /* if no extension found, set to default 's' */
  1511. if (ast_strlen_zero(p->exten)) {
  1512. p->exten[0]='s';
  1513. p->exten[1]='\0';
  1514. }
  1515. user = find_user(p->callerid_name, call->remoteIP);
  1516. if(user && (user->incominglimit == 0 || user->inUse < user->incominglimit)) {
  1517. ast_mutex_lock(&user->lock);
  1518. p->username = strdup(user->name);
  1519. p->neighbor.user = user->mUseIP ? ast_strdup(user->mIP) :
  1520. ast_strdup(user->name);
  1521. ast_copy_string(p->context, user->context, sizeof(p->context));
  1522. ast_copy_string(p->accountcode, user->accountcode, sizeof(p->accountcode));
  1523. p->amaflags = user->amaflags;
  1524. ast_format_cap_copy(p->cap, user->cap);
  1525. p->g729onlyA = user->g729onlyA;
  1526. memcpy(&p->prefs, &user->prefs, sizeof(struct ast_codec_pref));
  1527. p->dtmfmode |= user->dtmfmode;
  1528. p->dtmfcodec = user->dtmfcodec;
  1529. p->faxdetect = user->faxdetect;
  1530. p->t38support = user->t38support;
  1531. p->rtptimeout = user->rtptimeout;
  1532. p->h245tunneling = user->h245tunneling;
  1533. p->faststart = user->faststart;
  1534. if (p->faststart)
  1535. OO_SETFLAG(call->flags, OO_M_FASTSTART);
  1536. else
  1537. OO_CLRFLAG(call->flags, OO_M_FASTSTART);
  1538. /* if we disable h245tun for this user then we clear flag */
  1539. /* in any other case we don't must touch this */
  1540. /* ie if we receive setup without h245tun but enabled
  1541. we can't enable it per call */
  1542. if (!p->h245tunneling)
  1543. OO_CLRFLAG(call->flags, OO_M_TUNNELING);
  1544. if (user->rtpmask && user->rtpmaskstr[0]) {
  1545. p->rtpmask = user->rtpmask;
  1546. ast_copy_string(p->rtpmaskstr, user->rtpmaskstr,
  1547. sizeof(p->rtpmaskstr));
  1548. }
  1549. if (user->rtdrcount > 0 && user->rtdrinterval > 0) {
  1550. p->rtdrcount = user->rtdrcount;
  1551. p->rtdrinterval = user->rtdrinterval;
  1552. }
  1553. if (user->incominglimit) user->inUse++;
  1554. ast_mutex_unlock(&user->lock);
  1555. } else {
  1556. if (!OO_TESTFLAG(p->flags,H323_DISABLEGK)) {
  1557. p->username = strdup(call->remoteIP);
  1558. } else {
  1559. ast_mutex_unlock(&p->lock);
  1560. ast_log(LOG_ERROR, "Unacceptable ip %s\n", call->remoteIP);
  1561. if (!user) {
  1562. ooHangCall(call->callToken, ooh323_convert_hangupcause_asteriskToH323(AST_CAUSE_CALL_REJECTED), AST_CAUSE_CALL_REJECTED);
  1563. call->callEndReason = OO_REASON_REMOTE_REJECTED;
  1564. }
  1565. else {
  1566. ooHangCall(call->callToken, ooh323_convert_hangupcause_asteriskToH323(AST_CAUSE_NORMAL_CIRCUIT_CONGESTION), AST_CAUSE_NORMAL_CIRCUIT_CONGESTION);
  1567. call->callEndReason = OO_REASON_REMOTE_REJECTED;
  1568. }
  1569. ast_set_flag(p, H323_NEEDDESTROY);
  1570. return -1;
  1571. }
  1572. }
  1573. ooh323c_set_capability_for_call(call, &p->prefs, p->cap, p->dtmfmode, p->dtmfcodec,
  1574. p->t38support, p->g729onlyA);
  1575. /* Incoming call */
  1576. c = ooh323_new(p, AST_STATE_RING, p->username, 0, NULL);
  1577. if(!c) {
  1578. ast_mutex_unlock(&p->lock);
  1579. ast_log(LOG_ERROR, "Could not create ast_channel\n");
  1580. return -1;
  1581. }
  1582. if (!configure_local_rtp(p, call)) {
  1583. ast_mutex_unlock(&p->lock);
  1584. ast_log(LOG_ERROR, "Couldn't create rtp structure\n");
  1585. return -1;
  1586. }
  1587. ast_mutex_unlock(&p->lock);
  1588. if (gH323Debug)
  1589. ast_verbose("+++ ooh323_onReceivedSetup - Determined context %s, "
  1590. "extension %s\n", p->context, p->exten);
  1591. return OO_OK;
  1592. }
  1593. int onOutgoingCall(ooCallData *call)
  1594. {
  1595. struct ooh323_pvt *p = NULL;
  1596. int i = 0;
  1597. if (gH323Debug)
  1598. ast_verbose("--- onOutgoingCall %lx: %s\n", (long unsigned int) call, call->callToken);
  1599. if (!strcmp(call->callType, "outgoing")) {
  1600. p = find_call(call);
  1601. if (!p) {
  1602. ast_log(LOG_ERROR, "Failed to find a matching call.\n");
  1603. return -1;
  1604. }
  1605. ast_mutex_lock(&p->lock);
  1606. if (!ast_strlen_zero(p->callerid_name)) {
  1607. ooCallSetCallerId(call, p->callerid_name);
  1608. }
  1609. if (!ast_strlen_zero(p->callerid_num)) {
  1610. i = 0;
  1611. while (*(p->callerid_num + i) != '\0') {
  1612. if(!isdigit(*(p->callerid_num+i))) { break; }
  1613. i++;
  1614. }
  1615. if(*(p->callerid_num+i) == '\0')
  1616. ooCallSetCallingPartyNumber(call, p->callerid_num);
  1617. else {
  1618. if(!p->callerid_name)
  1619. ooCallSetCallerId(call, p->callerid_num);
  1620. }
  1621. }
  1622. if (!ast_strlen_zero(p->caller_h323id))
  1623. ooCallAddAliasH323ID(call, p->caller_h323id);
  1624. if (!ast_strlen_zero(p->caller_dialedDigits)) {
  1625. if (gH323Debug) {
  1626. ast_verbose("Setting dialed digits %s\n", p->caller_dialedDigits);
  1627. }
  1628. ooCallAddAliasDialedDigits(call, p->caller_dialedDigits);
  1629. } else if (!ast_strlen_zero(p->callerid_num)) {
  1630. if (ooIsDailedDigit(p->callerid_num)) {
  1631. if (gH323Debug) {
  1632. ast_verbose("setting callid number %s\n", p->callerid_num);
  1633. }
  1634. ooCallAddAliasDialedDigits(call, p->callerid_num);
  1635. } else if (ast_strlen_zero(p->caller_h323id)) {
  1636. ooCallAddAliasH323ID(call, p->callerid_num);
  1637. }
  1638. }
  1639. if (p->rtpmask && p->rtpmaskstr[0]) {
  1640. call->rtpMask = p->rtpmask;
  1641. ast_mutex_lock(&call->rtpMask->lock);
  1642. call->rtpMask->inuse++;
  1643. ast_mutex_unlock(&call->rtpMask->lock);
  1644. ast_copy_string(call->rtpMaskStr, p->rtpmaskstr, sizeof(call->rtpMaskStr));
  1645. }
  1646. if (!configure_local_rtp(p, call)) {
  1647. ast_mutex_unlock(&p->lock);
  1648. return OO_FAILED;
  1649. }
  1650. ast_mutex_unlock(&p->lock);
  1651. }
  1652. if (gH323Debug)
  1653. ast_verbose("+++ onOutgoingCall %s\n", call->callToken);
  1654. return OO_OK;
  1655. }
  1656. int onNewCallCreated(ooCallData *call)
  1657. {
  1658. struct ooh323_pvt *p = NULL;
  1659. int i = 0;
  1660. if (gH323Debug)
  1661. ast_verbose("--- onNewCallCreated %lx: %s\n", (long unsigned int) call, call->callToken);
  1662. ast_mutex_lock(&call->Lock);
  1663. if (ooh323c_start_call_thread(call)) {
  1664. ast_log(LOG_ERROR,"Failed to create call thread.\n");
  1665. ast_mutex_unlock(&call->Lock);
  1666. return -1;
  1667. }
  1668. if (!strcmp(call->callType, "outgoing")) {
  1669. p = find_call(call);
  1670. if (!p) {
  1671. ast_log(LOG_ERROR, "Failed to find a matching call.\n");
  1672. ast_mutex_unlock(&call->Lock);
  1673. return -1;
  1674. }
  1675. ast_mutex_lock(&p->lock);
  1676. if (!ast_strlen_zero(p->callerid_name)) {
  1677. ooCallSetCallerId(call, p->callerid_name);
  1678. }
  1679. if (!ast_strlen_zero(p->callerid_num)) {
  1680. i = 0;
  1681. while (*(p->callerid_num + i) != '\0') {
  1682. if(!isdigit(*(p->callerid_num+i))) { break; }
  1683. i++;
  1684. }
  1685. if(*(p->callerid_num+i) == '\0')
  1686. ooCallSetCallingPartyNumber(call, p->callerid_num);
  1687. else {
  1688. if(ast_strlen_zero(p->callerid_name))
  1689. ooCallSetCallerId(call, p->callerid_num);
  1690. }
  1691. }
  1692. if (!ast_strlen_zero(p->caller_h323id))
  1693. ooCallAddAliasH323ID(call, p->caller_h323id);
  1694. if (!ast_strlen_zero(p->caller_dialedDigits)) {
  1695. if (gH323Debug) {
  1696. ast_verbose("Setting dialed digits %s\n", p->caller_dialedDigits);
  1697. }
  1698. ooCallAddAliasDialedDigits(call, p->caller_dialedDigits);
  1699. } else if (!ast_strlen_zero(p->callerid_num)) {
  1700. if (ooIsDailedDigit(p->callerid_num)) {
  1701. if (gH323Debug) {
  1702. ast_verbose("setting callid number %s\n", p->callerid_num);
  1703. }
  1704. ooCallAddAliasDialedDigits(call, p->callerid_num);
  1705. } else if (ast_strlen_zero(p->caller_h323id)) {
  1706. ooCallAddAliasH323ID(call, p->callerid_num);
  1707. }
  1708. }
  1709. if (!ast_strlen_zero(p->exten)) {
  1710. if (ooIsDailedDigit(p->exten)) {
  1711. ooCallSetCalledPartyNumber(call, p->exten);
  1712. ooCallAddRemoteAliasDialedDigits(call, p->exten);
  1713. } else {
  1714. ooCallAddRemoteAliasH323ID(call, p->exten);
  1715. }
  1716. }
  1717. if (gH323Debug) {
  1718. char prefsBuf[256];
  1719. ast_codec_pref_string(&p->prefs, prefsBuf, sizeof(prefsBuf));
  1720. ast_verbose(" Outgoing call %s(%s) - Codec prefs - %s\n",
  1721. p->username?p->username:"NULL", call->callToken, prefsBuf);
  1722. }
  1723. ooh323c_set_capability_for_call(call, &p->prefs, p->cap,
  1724. p->dtmfmode, p->dtmfcodec, p->t38support, p->g729onlyA);
  1725. /* configure_local_rtp(p, call); */
  1726. ast_mutex_unlock(&p->lock);
  1727. }
  1728. ast_mutex_unlock(&call->Lock);
  1729. if (gH323Debug)
  1730. ast_verbose("+++ onNewCallCreated %s\n", call->callToken);
  1731. return OO_OK;
  1732. }
  1733. int onCallEstablished(ooCallData *call)
  1734. {
  1735. struct ooh323_pvt *p = NULL;
  1736. if (gH323Debug)
  1737. ast_verbose("--- onCallEstablished %s\n", call->callToken);
  1738. if (!(p = find_call(call))) {
  1739. ast_log(LOG_ERROR, "Failed to find a matching call.\n");
  1740. return -1;
  1741. }
  1742. if(ast_test_flag(p, H323_OUTGOING)) {
  1743. ast_mutex_lock(&p->lock);
  1744. if (!p->owner) {
  1745. ast_mutex_unlock(&p->lock);
  1746. ast_log(LOG_ERROR, "Channel has no owner\n");
  1747. return -1;
  1748. }
  1749. while (p->owner && ast_channel_trylock(p->owner)) {
  1750. ast_debug(1, "Failed to grab lock, trying again\n");
  1751. DEADLOCK_AVOIDANCE(&p->lock);
  1752. }
  1753. if (p->owner) {
  1754. struct ast_channel* c = p->owner;
  1755. if (call->remoteDisplayName) {
  1756. struct ast_party_connected_line connected;
  1757. struct ast_set_party_connected_line update_connected;
  1758. memset(&update_connected, 0, sizeof(update_connected));
  1759. update_connected.id.name = 1;
  1760. ast_party_connected_line_init(&connected);
  1761. connected.id.name.valid = 1;
  1762. connected.id.name.str = (char *) call->remoteDisplayName;
  1763. connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
  1764. ast_channel_queue_connected_line_update(c, &connected, &update_connected);
  1765. }
  1766. ast_queue_control(c, AST_CONTROL_ANSWER);
  1767. ast_channel_unlock(p->owner);
  1768. manager_event(EVENT_FLAG_SYSTEM,"ChannelUpdate","Channel: %s\r\nChanneltype: %s\r\n"
  1769. "CallRef: %d\r\n", c->name, "OOH323", p->call_reference);
  1770. }
  1771. ast_mutex_unlock(&p->lock);
  1772. }
  1773. if (gH323Debug)
  1774. ast_verbose("+++ onCallEstablished %s\n", call->callToken);
  1775. return OO_OK;
  1776. }
  1777. int onCallCleared(ooCallData *call)
  1778. {
  1779. struct ooh323_pvt *p = NULL;
  1780. int ownerLock = 0;
  1781. if (gH323Debug)
  1782. ast_verbose("--- onCallCleared %s \n", call->callToken);
  1783. if ((p = find_call(call))) {
  1784. ast_mutex_lock(&p->lock);
  1785. while (p->owner) {
  1786. if (ast_channel_trylock(p->owner)) {
  1787. ooTrace(OOTRCLVLINFO, "Failed to grab lock, trying again\n");
  1788. ast_debug(1, "Failed to grab lock, trying again\n");
  1789. DEADLOCK_AVOIDANCE(&p->lock);
  1790. } else {
  1791. ownerLock = 1; break;
  1792. }
  1793. }
  1794. if (ownerLock) {
  1795. if (!ast_test_flag(p, H323_ALREADYGONE)) {
  1796. ast_set_flag(p, H323_ALREADYGONE);
  1797. p->owner->hangupcause = call->q931cause;
  1798. p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  1799. ast_queue_hangup_with_cause(p->owner,call->q931cause);
  1800. }
  1801. }
  1802. if(p->owner) {
  1803. p->owner->tech_pvt = NULL;
  1804. ast_channel_unlock(p->owner);
  1805. p->owner = NULL;
  1806. ast_module_unref(myself);
  1807. }
  1808. ast_set_flag(p, H323_NEEDDESTROY);
  1809. ooh323c_stop_call_thread(call);
  1810. ast_mutex_unlock(&p->lock);
  1811. ast_mutex_lock(&usecnt_lock);
  1812. usecnt--;
  1813. ast_mutex_unlock(&usecnt_lock);
  1814. }
  1815. if (gH323Debug)
  1816. ast_verbose("+++ onCallCleared\n");
  1817. return OO_OK;
  1818. }
  1819. /* static void ooh323_delete_user(struct ooh323_user *user)
  1820. {
  1821. struct ooh323_user *prev = NULL, *cur = NULL;
  1822. if (gH323Debug)
  1823. ast_verbose("--- ooh323_delete_user\n");
  1824. if (user) {
  1825. cur = userl.users;
  1826. ast_mutex_lock(&userl.lock);
  1827. while (cur) {
  1828. if (cur == user) break;
  1829. prev = cur;
  1830. cur = cur->next;
  1831. }
  1832. if (cur) {
  1833. if (prev)
  1834. prev->next = cur->next;
  1835. else
  1836. userl.users = cur->next;
  1837. }
  1838. ast_mutex_unlock(&userl.lock);
  1839. free(user);
  1840. }
  1841. if (gH323Debug)
  1842. ast_verbose("+++ ooh323_delete_user\n");
  1843. } */
  1844. void ooh323_delete_peer(struct ooh323_peer *peer)
  1845. {
  1846. struct ooh323_peer *prev = NULL, *cur = NULL;
  1847. if (gH323Debug)
  1848. ast_verbose("--- ooh323_delete_peer\n");
  1849. if (peer) {
  1850. cur = peerl.peers;
  1851. ast_mutex_lock(&peerl.lock);
  1852. while(cur) {
  1853. if(cur==peer) break;
  1854. prev = cur;
  1855. cur = cur->next;
  1856. }
  1857. if (cur) {
  1858. if(prev)
  1859. prev->next = cur->next;
  1860. else
  1861. peerl.peers = cur->next;
  1862. }
  1863. ast_mutex_unlock(&peerl.lock);
  1864. if(peer->h323id) free(peer->h323id);
  1865. if(peer->email) free(peer->email);
  1866. if(peer->url) free(peer->url);
  1867. if(peer->e164) free(peer->e164);
  1868. peer->cap = ast_format_cap_destroy(peer->cap);
  1869. free(peer);
  1870. }
  1871. if (gH323Debug)
  1872. ast_verbose("+++ ooh323_delete_peer\n");
  1873. }
  1874. static struct ooh323_user *build_user(const char *name, struct ast_variable *v)
  1875. {
  1876. struct ooh323_user *user = NULL;
  1877. if (gH323Debug)
  1878. ast_verbose("--- build_user\n");
  1879. user = ast_calloc(1,sizeof(struct ooh323_user));
  1880. if (user) {
  1881. memset(user, 0, sizeof(struct ooh323_user));
  1882. if (!(user->cap = ast_format_cap_alloc())) {
  1883. ast_free(user);
  1884. return NULL;
  1885. }
  1886. ast_mutex_init(&user->lock);
  1887. ast_copy_string(user->name, name, sizeof(user->name));
  1888. ast_format_cap_copy(user->cap, gCap);
  1889. memcpy(&user->prefs, &gPrefs, sizeof(user->prefs));
  1890. user->rtptimeout = gRTPTimeout;
  1891. user->dtmfmode = gDTMFMode;
  1892. user->dtmfcodec = gDTMFCodec;
  1893. user->faxdetect = gFAXdetect;
  1894. user->t38support = gT38Support;
  1895. user->faststart = gFastStart;
  1896. user->h245tunneling = gTunneling;
  1897. user->g729onlyA = g729onlyA;
  1898. /* set default context */
  1899. ast_copy_string(user->context, gContext, sizeof(user->context));
  1900. ast_copy_string(user->accountcode, gAccountcode, sizeof(user->accountcode));
  1901. user->amaflags = gAMAFLAGS;
  1902. while (v) {
  1903. if (!strcasecmp(v->name, "context")) {
  1904. ast_copy_string(user->context, v->value, sizeof(user->context));
  1905. } else if (!strcasecmp(v->name, "incominglimit")) {
  1906. user->incominglimit = atoi(v->value);
  1907. if (user->incominglimit < 0)
  1908. user->incominglimit = 0;
  1909. } else if (!strcasecmp(v->name, "accountcode")) {
  1910. strncpy(user->accountcode, v->value,
  1911. sizeof(user->accountcode)-1);
  1912. } else if (!strcasecmp(v->name, "roundtrip")) {
  1913. sscanf(v->value, "%d,%d", &user->rtdrcount, &user->rtdrinterval);
  1914. } else if (!strcasecmp(v->name, "faststart")) {
  1915. user->faststart = ast_true(v->value);
  1916. } else if (!strcasecmp(v->name, "h245tunneling")) {
  1917. user->h245tunneling = ast_true(v->value);
  1918. } else if (!strcasecmp(v->name, "g729onlyA")) {
  1919. user->g729onlyA = ast_true(v->value);
  1920. } else if (!strcasecmp(v->name, "rtptimeout")) {
  1921. user->rtptimeout = atoi(v->value);
  1922. if (user->rtptimeout < 0)
  1923. user->rtptimeout = gRTPTimeout;
  1924. } else if (!strcasecmp(v->name, "rtpmask")) {
  1925. if ((user->rtpmask = ast_calloc(1, sizeof(struct OOH323Regex))) &&
  1926. (regcomp(&user->rtpmask->regex, v->value, REG_EXTENDED)
  1927. == 0)) {
  1928. ast_mutex_init(&user->rtpmask->lock);
  1929. user->rtpmask->inuse = 1;
  1930. ast_copy_string(user->rtpmaskstr, v->value,
  1931. sizeof(user->rtpmaskstr));
  1932. } else user->rtpmask = NULL;
  1933. } else if (!strcasecmp(v->name, "disallow")) {
  1934. ast_parse_allow_disallow(&user->prefs,
  1935. user->cap, v->value, 0);
  1936. } else if (!strcasecmp(v->name, "allow")) {
  1937. const char* tcodecs = v->value;
  1938. if (!strcasecmp(v->value, "all")) {
  1939. tcodecs = "ulaw,alaw,g729,g723,gsm";
  1940. }
  1941. ast_parse_allow_disallow(&user->prefs,
  1942. user->cap, tcodecs, 1);
  1943. } else if (!strcasecmp(v->name, "amaflags")) {
  1944. user->amaflags = ast_cdr_amaflags2int(v->value);
  1945. } else if (!strcasecmp(v->name, "ip") || !strcasecmp(v->name, "host")) {
  1946. struct ast_sockaddr p;
  1947. if (!ast_parse_arg(v->value, PARSE_ADDR, &p)) {
  1948. ast_copy_string(user->mIP, ast_sockaddr_stringify_addr(&p), sizeof(user->mIP)-1);
  1949. } else {
  1950. ast_copy_string(user->mIP, v->value, sizeof(user->mIP)-1);
  1951. }
  1952. user->mUseIP = 1;
  1953. } else if (!strcasecmp(v->name, "dtmfmode")) {
  1954. if (!strcasecmp(v->value, "rfc2833"))
  1955. user->dtmfmode = H323_DTMF_RFC2833;
  1956. if (!strcasecmp(v->value, "cisco"))
  1957. user->dtmfmode = H323_DTMF_CISCO;
  1958. else if (!strcasecmp(v->value, "q931keypad"))
  1959. user->dtmfmode = H323_DTMF_Q931;
  1960. else if (!strcasecmp(v->value, "h245alphanumeric"))
  1961. user->dtmfmode = H323_DTMF_H245ALPHANUMERIC;
  1962. else if (!strcasecmp(v->value, "h245signal"))
  1963. user->dtmfmode = H323_DTMF_H245SIGNAL;
  1964. else if (!strcasecmp(v->value, "inband"))
  1965. user->dtmfmode = H323_DTMF_INBAND;
  1966. } else if (!strcasecmp(v->name, "relaxdtmf")) {
  1967. user->dtmfmode |= ast_true(v->value) ? H323_DTMF_INBANDRELAX : 0;
  1968. } else if (!strcasecmp(v->name, "dtmfcodec") && atoi(v->value)) {
  1969. user->dtmfcodec = atoi(v->value);
  1970. } else if (!strcasecmp(v->name, "faxdetect")) {
  1971. if (ast_true(v->value)) {
  1972. user->faxdetect = FAXDETECT_CNG | FAXDETECT_T38;
  1973. } else if (ast_false(v->value)) {
  1974. user->faxdetect = 0;
  1975. } else {
  1976. char *buf = ast_strdupa(v->value);
  1977. char *word, *next = buf;
  1978. user->faxdetect = 0;
  1979. while ((word = strsep(&next, ","))) {
  1980. if (!strcasecmp(word, "cng")) {
  1981. user->faxdetect |= FAXDETECT_CNG;
  1982. } else if (!strcasecmp(word, "t38")) {
  1983. user->faxdetect |= FAXDETECT_T38;
  1984. } else {
  1985. ast_log(LOG_WARNING, "Unknown faxdetect mode '%s' on line %d.\n", word, v->lineno);
  1986. }
  1987. }
  1988. }
  1989. } else if (!strcasecmp(v->name, "t38support")) {
  1990. if (!strcasecmp(v->value, "disabled"))
  1991. user->t38support = T38_DISABLED;
  1992. if (!strcasecmp(v->value, "no"))
  1993. user->t38support = T38_DISABLED;
  1994. else if (!strcasecmp(v->value, "faxgw"))
  1995. user->t38support = T38_FAXGW;
  1996. else if (!strcasecmp(v->value, "yes"))
  1997. user->t38support = T38_ENABLED;
  1998. }
  1999. v = v->next;
  2000. }
  2001. }
  2002. if (gH323Debug)
  2003. ast_verbose("+++ build_user\n");
  2004. return user;
  2005. }
  2006. static struct ooh323_peer *build_peer(const char *name, struct ast_variable *v, int friend_type)
  2007. {
  2008. struct ooh323_peer *peer = NULL;
  2009. if (gH323Debug)
  2010. ast_verbose("--- build_peer\n");
  2011. peer = ast_calloc(1, sizeof(*peer));
  2012. if (peer) {
  2013. memset(peer, 0, sizeof(struct ooh323_peer));
  2014. if (!(peer->cap = ast_format_cap_alloc())) {
  2015. ast_free(peer);
  2016. return NULL;
  2017. }
  2018. ast_mutex_init(&peer->lock);
  2019. ast_copy_string(peer->name, name, sizeof(peer->name));
  2020. ast_format_cap_copy(peer->cap, gCap);
  2021. memcpy(&peer->prefs, &gPrefs, sizeof(peer->prefs));
  2022. peer->rtptimeout = gRTPTimeout;
  2023. ast_copy_string(peer->accountcode, gAccountcode, sizeof(peer->accountcode));
  2024. peer->amaflags = gAMAFLAGS;
  2025. peer->dtmfmode = gDTMFMode;
  2026. peer->dtmfcodec = gDTMFCodec;
  2027. peer->faxdetect = gFAXdetect;
  2028. peer->t38support = gT38Support;
  2029. peer->faststart = gFastStart;
  2030. peer->h245tunneling = gTunneling;
  2031. peer->g729onlyA = g729onlyA;
  2032. peer->port = 1720;
  2033. if (0 == friend_type) {
  2034. peer->mFriend = 1;
  2035. }
  2036. while (v) {
  2037. if (!strcasecmp(v->name, "h323id")) {
  2038. if (!(peer->h323id = ast_strdup(v->value))) {
  2039. ast_log(LOG_ERROR, "Could not allocate memory for h323id of "
  2040. "peer %s\n", name);
  2041. ooh323_delete_peer(peer);
  2042. return NULL;
  2043. }
  2044. } else if (!strcasecmp(v->name, "e164")) {
  2045. if (!(peer->e164 = ast_strdup(v->value))) {
  2046. ast_log(LOG_ERROR, "Could not allocate memory for e164 of "
  2047. "peer %s\n", name);
  2048. ooh323_delete_peer(peer);
  2049. return NULL;
  2050. }
  2051. } else if (!strcasecmp(v->name, "email")) {
  2052. if (!(peer->email = ast_strdup(v->value))) {
  2053. ast_log(LOG_ERROR, "Could not allocate memory for email of "
  2054. "peer %s\n", name);
  2055. ooh323_delete_peer(peer);
  2056. return NULL;
  2057. }
  2058. } else if (!strcasecmp(v->name, "url")) {
  2059. if (!(peer->url = ast_strdup(v->value))) {
  2060. ast_log(LOG_ERROR, "Could not allocate memory for h323id of "
  2061. "peer %s\n", name);
  2062. ooh323_delete_peer(peer);
  2063. return NULL;
  2064. }
  2065. } else if (!strcasecmp(v->name, "port")) {
  2066. peer->port = atoi(v->value);
  2067. } else if (!strcasecmp(v->name, "host") || !strcasecmp(v->name, "ip")) {
  2068. struct ast_sockaddr p;
  2069. if (!ast_parse_arg(v->value, PARSE_ADDR, &p)) {
  2070. ast_copy_string(peer->ip, ast_sockaddr_stringify_host(&p), sizeof(peer->ip));
  2071. } else {
  2072. ast_copy_string(peer->ip, v->value, sizeof(peer->ip));
  2073. }
  2074. } else if (!strcasecmp(v->name, "outgoinglimit")) {
  2075. peer->outgoinglimit = atoi(v->value);
  2076. if (peer->outgoinglimit < 0)
  2077. peer->outgoinglimit = 0;
  2078. } else if (!strcasecmp(v->name, "accountcode")) {
  2079. ast_copy_string(peer->accountcode, v->value, sizeof(peer->accountcode));
  2080. } else if (!strcasecmp(v->name, "faststart")) {
  2081. peer->faststart = ast_true(v->value);
  2082. } else if (!strcasecmp(v->name, "h245tunneling")) {
  2083. peer->h245tunneling = ast_true(v->value);
  2084. } else if (!strcasecmp(v->name, "g729onlyA")) {
  2085. peer->g729onlyA = ast_true(v->value);
  2086. } else if (!strcasecmp(v->name, "rtptimeout")) {
  2087. peer->rtptimeout = atoi(v->value);
  2088. if(peer->rtptimeout < 0)
  2089. peer->rtptimeout = gRTPTimeout;
  2090. } else if (!strcasecmp(v->name, "rtpmask")) {
  2091. if ((peer->rtpmask = ast_calloc(1, sizeof(struct OOH323Regex))) &&
  2092. (regcomp(&peer->rtpmask->regex, v->value, REG_EXTENDED)
  2093. == 0)) {
  2094. ast_mutex_init(&peer->rtpmask->lock);
  2095. peer->rtpmask->inuse = 1;
  2096. ast_copy_string(peer->rtpmaskstr, v->value,
  2097. sizeof(peer->rtpmaskstr));
  2098. } else peer->rtpmask = NULL;
  2099. } else if (!strcasecmp(v->name, "disallow")) {
  2100. ast_parse_allow_disallow(&peer->prefs, peer->cap,
  2101. v->value, 0);
  2102. } else if (!strcasecmp(v->name, "allow")) {
  2103. const char* tcodecs = v->value;
  2104. if (!strcasecmp(v->value, "all")) {
  2105. tcodecs = "ulaw,alaw,g729,g723,gsm";
  2106. }
  2107. ast_parse_allow_disallow(&peer->prefs, peer->cap,
  2108. tcodecs, 1);
  2109. } else if (!strcasecmp(v->name, "amaflags")) {
  2110. peer->amaflags = ast_cdr_amaflags2int(v->value);
  2111. } else if (!strcasecmp(v->name, "roundtrip")) {
  2112. sscanf(v->value, "%d,%d", &peer->rtdrcount, &peer->rtdrinterval);
  2113. } else if (!strcasecmp(v->name, "dtmfmode")) {
  2114. if (!strcasecmp(v->value, "rfc2833"))
  2115. peer->dtmfmode = H323_DTMF_RFC2833;
  2116. if (!strcasecmp(v->value, "cisco"))
  2117. peer->dtmfmode = H323_DTMF_CISCO;
  2118. else if (!strcasecmp(v->value, "q931keypad"))
  2119. peer->dtmfmode = H323_DTMF_Q931;
  2120. else if (!strcasecmp(v->value, "h245alphanumeric"))
  2121. peer->dtmfmode = H323_DTMF_H245ALPHANUMERIC;
  2122. else if (!strcasecmp(v->value, "h245signal"))
  2123. peer->dtmfmode = H323_DTMF_H245SIGNAL;
  2124. else if (!strcasecmp(v->value, "inband"))
  2125. peer->dtmfmode = H323_DTMF_INBAND;
  2126. } else if (!strcasecmp(v->name, "relaxdtmf")) {
  2127. peer->dtmfmode |= ast_true(v->value) ? H323_DTMF_INBANDRELAX : 0;
  2128. } else if (!strcasecmp(v->name, "dtmfcodec") && atoi(v->value)) {
  2129. peer->dtmfcodec = atoi(v->value);
  2130. } else if (!strcasecmp(v->name, "faxdetect")) {
  2131. if (ast_true(v->value)) {
  2132. peer->faxdetect = FAXDETECT_CNG | FAXDETECT_T38;
  2133. } else if (ast_false(v->value)) {
  2134. peer->faxdetect = 0;
  2135. } else {
  2136. char *buf = ast_strdupa(v->value);
  2137. char *word, *next = buf;
  2138. peer->faxdetect = 0;
  2139. while ((word = strsep(&next, ","))) {
  2140. if (!strcasecmp(word, "cng")) {
  2141. peer->faxdetect |= FAXDETECT_CNG;
  2142. } else if (!strcasecmp(word, "t38")) {
  2143. peer->faxdetect |= FAXDETECT_T38;
  2144. } else {
  2145. ast_log(LOG_WARNING, "Unknown faxdetect mode '%s' on line %d.\n", word, v->lineno);
  2146. }
  2147. }
  2148. }
  2149. } else if (!strcasecmp(v->name, "t38support")) {
  2150. if (!strcasecmp(v->value, "disabled"))
  2151. peer->t38support = T38_DISABLED;
  2152. if (!strcasecmp(v->value, "no"))
  2153. peer->t38support = T38_DISABLED;
  2154. else if (!strcasecmp(v->value, "faxgw"))
  2155. peer->t38support = T38_FAXGW;
  2156. else if (!strcasecmp(v->value, "yes"))
  2157. peer->t38support = T38_ENABLED;
  2158. }
  2159. v = v->next;
  2160. }
  2161. }
  2162. if (gH323Debug)
  2163. ast_verbose("+++ build_peer\n");
  2164. return peer;
  2165. }
  2166. static int ooh323_do_reload(void)
  2167. {
  2168. if (gH323Debug) {
  2169. ast_verbose("--- ooh323_do_reload\n");
  2170. }
  2171. reload_config(1);
  2172. if (gH323Debug) {
  2173. ast_verbose("+++ ooh323_do_reload\n");
  2174. }
  2175. return 0;
  2176. }
  2177. /*--- h323_reload: Force reload of module from cli ---*/
  2178. char *handle_cli_ooh323_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2179. {
  2180. switch (cmd) {
  2181. case CLI_INIT:
  2182. e->command = "ooh323 reload";
  2183. e->usage =
  2184. "Usage: ooh323 reload\n"
  2185. " Reload OOH323 config.\n";
  2186. return NULL;
  2187. case CLI_GENERATE:
  2188. return NULL;
  2189. }
  2190. if (a->argc != 2)
  2191. return CLI_SHOWUSAGE;
  2192. if (gH323Debug)
  2193. ast_verbose("--- ooh323_reload\n");
  2194. ast_mutex_lock(&h323_reload_lock);
  2195. if (h323_reloading) {
  2196. ast_verbose("Previous OOH323 reload not yet done\n");
  2197. } else {
  2198. h323_reloading = 1;
  2199. }
  2200. ast_mutex_unlock(&h323_reload_lock);
  2201. restart_monitor();
  2202. if (gH323Debug)
  2203. ast_verbose("+++ ooh323_reload\n");
  2204. return 0;
  2205. }
  2206. int reload_config(int reload)
  2207. {
  2208. int format;
  2209. struct ooAliases *pNewAlias = NULL, *cur, *prev;
  2210. struct ast_config *cfg;
  2211. struct ast_variable *v;
  2212. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  2213. struct ooh323_user *user = NULL;
  2214. struct ooh323_peer *peer = NULL;
  2215. char *cat;
  2216. const char *utype;
  2217. struct ast_format tmpfmt;
  2218. if (gH323Debug)
  2219. ast_verbose("--- reload_config\n");
  2220. cfg = ast_config_load((char*)config, config_flags);
  2221. /* We *must* have a config file otherwise stop immediately */
  2222. if (!cfg) {
  2223. ast_log(LOG_NOTICE, "Unable to load config %s, OOH323 disabled\n", config);
  2224. return 1;
  2225. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
  2226. return RESULT_SUCCESS;
  2227. if (reload) {
  2228. delete_users();
  2229. delete_peers();
  2230. if (gH323Debug) {
  2231. ast_verbose(" reload_config - Freeing up alias list\n");
  2232. }
  2233. cur = gAliasList;
  2234. while (cur) {
  2235. prev = cur;
  2236. cur = cur->next;
  2237. free(prev->value);
  2238. free(prev);
  2239. }
  2240. gAliasList = NULL;
  2241. }
  2242. /* Inintialize everything to default */
  2243. strcpy(gLogFile, DEFAULT_LOGFILE);
  2244. gPort = 1720;
  2245. gIP[0] = '\0';
  2246. strcpy(gCallerID, DEFAULT_H323ID);
  2247. ast_format_cap_set(gCap, ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0));
  2248. memset(&gPrefs, 0, sizeof(struct ast_codec_pref));
  2249. gDTMFMode = H323_DTMF_RFC2833;
  2250. gDTMFCodec = 101;
  2251. gFAXdetect = FAXDETECT_CNG;
  2252. gT38Support = T38_FAXGW;
  2253. gTRCLVL = OOTRCLVLERR;
  2254. gRasGkMode = RasNoGatekeeper;
  2255. gGatekeeper[0] = '\0';
  2256. gRTPTimeout = 60;
  2257. gRTDRInterval = 0;
  2258. gRTDRCount = 0;
  2259. strcpy(gAccountcode, DEFAULT_H323ACCNT);
  2260. gFastStart = 1;
  2261. gTunneling = 1;
  2262. gTOS = 0;
  2263. strcpy(gContext, DEFAULT_CONTEXT);
  2264. gAliasList = NULL;
  2265. gMediaWaitForConnect = 0;
  2266. ooconfig.mTCPPortStart = 12030;
  2267. ooconfig.mTCPPortEnd = 12230;
  2268. memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
  2269. v = ast_variable_browse(cfg, "general");
  2270. while (v) {
  2271. if (!ast_jb_read_conf(&global_jbconf, v->name, v->value)) {
  2272. v = v->next;
  2273. continue;
  2274. }
  2275. if (!strcasecmp(v->name, "port")) {
  2276. gPort = (int)strtol(v->value, NULL, 10);
  2277. } else if (!strcasecmp(v->name, "bindaddr")) {
  2278. ast_copy_string(gIP, v->value, sizeof(gIP));
  2279. if (ast_parse_arg(v->value, PARSE_ADDR, &bindaddr)) {
  2280. ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
  2281. return 1;
  2282. }
  2283. if (ast_sockaddr_is_ipv6(&bindaddr)) {
  2284. v6mode = 1;
  2285. }
  2286. } else if (!strcasecmp(v->name, "h225portrange")) {
  2287. char* endlimit = 0;
  2288. char temp[512];
  2289. ast_copy_string(temp, v->value, sizeof(temp));
  2290. endlimit = strchr(temp, ',');
  2291. if (endlimit) {
  2292. *endlimit = '\0';
  2293. endlimit++;
  2294. ooconfig.mTCPPortStart = atoi(temp);
  2295. ooconfig.mTCPPortEnd = atoi(endlimit);
  2296. } else {
  2297. ast_log(LOG_ERROR, "h225portrange: Invalid format, separate port range with \",\"\n");
  2298. }
  2299. } else if (!strcasecmp(v->name, "gateway")) {
  2300. gIsGateway = ast_true(v->value);
  2301. } else if (!strcasecmp(v->name, "faststart")) {
  2302. gFastStart = ast_true(v->value);
  2303. if (gFastStart)
  2304. ooH323EpEnableFastStart();
  2305. else
  2306. ooH323EpDisableFastStart();
  2307. } else if (!strcasecmp(v->name, "mediawaitforconnect")) {
  2308. gMediaWaitForConnect = ast_true(v->value);
  2309. if (gMediaWaitForConnect)
  2310. ooH323EpEnableMediaWaitForConnect();
  2311. else
  2312. ooH323EpDisableMediaWaitForConnect();
  2313. } else if (!strcasecmp(v->name, "h245tunneling")) {
  2314. gTunneling = ast_true(v->value);
  2315. if (gTunneling)
  2316. ooH323EpEnableH245Tunneling();
  2317. else
  2318. ooH323EpDisableH245Tunneling();
  2319. } else if (!strcasecmp(v->name, "g729onlyA")) {
  2320. g729onlyA = ast_true(v->value);
  2321. } else if (!strcasecmp(v->name, "roundtrip")) {
  2322. sscanf(v->value, "%d,%d", &gRTDRCount, &gRTDRInterval);
  2323. } else if (!strcasecmp(v->name, "trybemaster")) {
  2324. gBeMaster = ast_true(v->value);
  2325. if (gBeMaster)
  2326. ooH323EpTryBeMaster(1);
  2327. else
  2328. ooH323EpTryBeMaster(0);
  2329. } else if (!strcasecmp(v->name, "h323id")) {
  2330. pNewAlias = ast_calloc(1, sizeof(struct ooAliases));
  2331. if (!pNewAlias) {
  2332. ast_log(LOG_ERROR, "Failed to allocate memory for h323id alias\n");
  2333. return 1;
  2334. }
  2335. if (gAliasList == NULL) { /* first h323id - set as callerid if callerid is not set */
  2336. ast_copy_string(gCallerID, v->value, sizeof(gCallerID));
  2337. }
  2338. pNewAlias->type = T_H225AliasAddress_h323_ID;
  2339. pNewAlias->value = strdup(v->value);
  2340. pNewAlias->next = gAliasList;
  2341. gAliasList = pNewAlias;
  2342. pNewAlias = NULL;
  2343. } else if (!strcasecmp(v->name, "e164")) {
  2344. pNewAlias = ast_calloc(1, sizeof(struct ooAliases));
  2345. if (!pNewAlias) {
  2346. ast_log(LOG_ERROR, "Failed to allocate memory for e164 alias\n");
  2347. return 1;
  2348. }
  2349. pNewAlias->type = T_H225AliasAddress_dialedDigits;
  2350. pNewAlias->value = strdup(v->value);
  2351. pNewAlias->next = gAliasList;
  2352. gAliasList = pNewAlias;
  2353. pNewAlias = NULL;
  2354. } else if (!strcasecmp(v->name, "email")) {
  2355. pNewAlias = ast_calloc(1, sizeof(struct ooAliases));
  2356. if (!pNewAlias) {
  2357. ast_log(LOG_ERROR, "Failed to allocate memory for email alias\n");
  2358. return 1;
  2359. }
  2360. pNewAlias->type = T_H225AliasAddress_email_ID;
  2361. pNewAlias->value = strdup(v->value);
  2362. pNewAlias->next = gAliasList;
  2363. gAliasList = pNewAlias;
  2364. pNewAlias = NULL;
  2365. } else if (!strcasecmp(v->name, "t35country")) {
  2366. t35countrycode = atoi(v->value);
  2367. } else if (!strcasecmp(v->name, "t35extensions")) {
  2368. t35extensions = atoi(v->value);
  2369. } else if (!strcasecmp(v->name, "manufacturer")) {
  2370. manufacturer = atoi(v->value);
  2371. } else if (!strcasecmp(v->name, "vendorid")) {
  2372. ast_copy_string(vendor, v->value, sizeof(vendor));
  2373. } else if (!strcasecmp(v->name, "versionid")) {
  2374. ast_copy_string(version, v->value, sizeof(version));
  2375. } else if (!strcasecmp(v->name, "callerid")) {
  2376. ast_copy_string(gCallerID, v->value, sizeof(gCallerID));
  2377. } else if (!strcasecmp(v->name, "incominglimit")) {
  2378. gIncomingLimit = atoi(v->value);
  2379. } else if (!strcasecmp(v->name, "outgoinglimit")) {
  2380. gOutgoingLimit = atoi(v->value);
  2381. } else if (!strcasecmp(v->name, "gatekeeper")) {
  2382. if (!strcasecmp(v->value, "DISABLE")) {
  2383. gRasGkMode = RasNoGatekeeper;
  2384. } else if (!strcasecmp(v->value, "DISCOVER")) {
  2385. gRasGkMode = RasDiscoverGatekeeper;
  2386. } else {
  2387. gRasGkMode = RasUseSpecificGatekeeper;
  2388. strncpy(gGatekeeper, v->value, sizeof(gGatekeeper)-1);
  2389. }
  2390. } else if (!strcasecmp(v->name, "logfile")) {
  2391. strncpy(gLogFile, v->value, sizeof(gLogFile)-1);
  2392. } else if (!strcasecmp(v->name, "context")) {
  2393. strncpy(gContext, v->value, sizeof(gContext)-1);
  2394. ast_verbose(VERBOSE_PREFIX_3 " == Setting default context to %s\n",
  2395. gContext);
  2396. } else if (!strcasecmp(v->name, "rtptimeout")) {
  2397. gRTPTimeout = atoi(v->value);
  2398. if (gRTPTimeout <= 0)
  2399. gRTPTimeout = 60;
  2400. } else if (!strcasecmp(v->name, "tos")) {
  2401. if (sscanf(v->value, "%30i", &format) == 1)
  2402. gTOS = format & 0xff;
  2403. else if (!strcasecmp(v->value, "lowdelay"))
  2404. gTOS = IPTOS_LOWDELAY;
  2405. else if (!strcasecmp(v->value, "throughput"))
  2406. gTOS = IPTOS_THROUGHPUT;
  2407. else if (!strcasecmp(v->value, "reliability"))
  2408. gTOS = IPTOS_RELIABILITY;
  2409. else if (!strcasecmp(v->value, "mincost"))
  2410. gTOS = IPTOS_MINCOST;
  2411. else if (!strcasecmp(v->value, "none"))
  2412. gTOS = 0;
  2413. else
  2414. ast_log(LOG_WARNING, "Invalid tos value at line %d, should be "
  2415. "'lowdelay', 'throughput', 'reliability', "
  2416. "'mincost', or 'none'\n", v->lineno);
  2417. } else if (!strcasecmp(v->name, "amaflags")) {
  2418. gAMAFLAGS = ast_cdr_amaflags2int(v->value);
  2419. } else if (!strcasecmp(v->name, "accountcode")) {
  2420. ast_copy_string(gAccountcode, v->value, sizeof(gAccountcode));
  2421. } else if (!strcasecmp(v->name, "disallow")) {
  2422. ast_parse_allow_disallow(&gPrefs, gCap, v->value, 0);
  2423. } else if (!strcasecmp(v->name, "allow")) {
  2424. const char* tcodecs = v->value;
  2425. if (!strcasecmp(v->value, "all")) {
  2426. tcodecs = "ulaw,alaw,g729,g723,gsm";
  2427. }
  2428. ast_parse_allow_disallow(&gPrefs, gCap, tcodecs, 1);
  2429. } else if (!strcasecmp(v->name, "dtmfmode")) {
  2430. if (!strcasecmp(v->value, "inband"))
  2431. gDTMFMode = H323_DTMF_INBAND;
  2432. else if (!strcasecmp(v->value, "rfc2833"))
  2433. gDTMFMode = H323_DTMF_RFC2833;
  2434. else if (!strcasecmp(v->value, "cisco"))
  2435. gDTMFMode = H323_DTMF_CISCO;
  2436. else if (!strcasecmp(v->value, "q931keypad"))
  2437. gDTMFMode = H323_DTMF_Q931;
  2438. else if (!strcasecmp(v->value, "h245alphanumeric"))
  2439. gDTMFMode = H323_DTMF_H245ALPHANUMERIC;
  2440. else if (!strcasecmp(v->value, "h245signal"))
  2441. gDTMFMode = H323_DTMF_H245SIGNAL;
  2442. else {
  2443. ast_log(LOG_WARNING, "Unknown dtmf mode '%s', using rfc2833\n",
  2444. v->value);
  2445. gDTMFMode = H323_DTMF_RFC2833;
  2446. }
  2447. } else if (!strcasecmp(v->name, "relaxdtmf")) {
  2448. gDTMFMode |= ast_true(v->value) ? H323_DTMF_INBANDRELAX : 0;
  2449. } else if (!strcasecmp(v->name, "dtmfcodec") && atoi(v->value)) {
  2450. gDTMFCodec = atoi(v->value);
  2451. } else if (!strcasecmp(v->name, "faxdetect")) {
  2452. if (ast_true(v->value)) {
  2453. gFAXdetect = FAXDETECT_CNG | FAXDETECT_T38;
  2454. } else if (ast_false(v->value)) {
  2455. gFAXdetect = 0;
  2456. } else {
  2457. char *buf = ast_strdupa(v->value);
  2458. char *word, *next = buf;
  2459. gFAXdetect = 0;
  2460. while ((word = strsep(&next, ","))) {
  2461. if (!strcasecmp(word, "cng")) {
  2462. gFAXdetect |= FAXDETECT_CNG;
  2463. } else if (!strcasecmp(word, "t38")) {
  2464. gFAXdetect |= FAXDETECT_T38;
  2465. } else {
  2466. ast_log(LOG_WARNING, "Unknown faxdetect mode '%s' on line %d.\n", word, v->lineno);
  2467. }
  2468. }
  2469. }
  2470. } else if (!strcasecmp(v->name, "t38support")) {
  2471. if (!strcasecmp(v->value, "disabled"))
  2472. gT38Support = T38_DISABLED;
  2473. if (!strcasecmp(v->value, "no"))
  2474. gT38Support = T38_DISABLED;
  2475. else if (!strcasecmp(v->value, "faxgw"))
  2476. gT38Support = T38_FAXGW;
  2477. else if (!strcasecmp(v->value, "yes"))
  2478. gT38Support = T38_ENABLED;
  2479. } else if (!strcasecmp(v->name, "tracelevel")) {
  2480. gTRCLVL = atoi(v->value);
  2481. ooH323EpSetTraceLevel(gTRCLVL);
  2482. }
  2483. v = v->next;
  2484. }
  2485. for (cat = ast_category_browse(cfg, NULL); cat; cat = ast_category_browse(cfg, cat)) {
  2486. if (strcasecmp(cat, "general")) {
  2487. int friend_type = 0;
  2488. utype = ast_variable_retrieve(cfg, cat, "type");
  2489. if (utype) {
  2490. friend_type = strcasecmp(utype, "friend");
  2491. if (!strcmp(utype, "user") || 0 == friend_type) {
  2492. user = build_user(cat, ast_variable_browse(cfg, cat));
  2493. if (user) {
  2494. ast_mutex_lock(&userl.lock);
  2495. user->next = userl.users;
  2496. userl.users = user;
  2497. ast_mutex_unlock(&userl.lock);
  2498. } else {
  2499. ast_log(LOG_WARNING, "Failed to build user %s\n", cat);
  2500. }
  2501. }
  2502. if (!strcasecmp(utype, "peer") || 0 == friend_type) {
  2503. peer = build_peer(cat, ast_variable_browse(cfg, cat), friend_type);
  2504. if (peer) {
  2505. ast_mutex_lock(&peerl.lock);
  2506. peer->next = peerl.peers;
  2507. peerl.peers = peer;
  2508. ast_mutex_unlock(&peerl.lock);
  2509. } else {
  2510. ast_log(LOG_WARNING, "Failed to build peer %s\n", cat);
  2511. }
  2512. }
  2513. }
  2514. }
  2515. }
  2516. ast_config_destroy(cfg);
  2517. /* Determine ip address if neccessary */
  2518. if (ast_strlen_zero(gIP)) {
  2519. ooGetLocalIPAddress(gIP);
  2520. if (!strcmp(gIP, "127.0.0.1") || !strcmp(gIP, "::1")) {
  2521. ast_log(LOG_NOTICE, "Failed to determine local ip address. Please "
  2522. "specify it in ooh323.conf. OOH323 Disabled\n");
  2523. return 1;
  2524. }
  2525. }
  2526. if (gH323Debug)
  2527. ast_verbose("+++ reload_config\n");
  2528. return 0;
  2529. }
  2530. static char *handle_cli_ooh323_show_peer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2531. {
  2532. char ip_port[30];
  2533. struct ooh323_peer *prev = NULL, *peer = NULL;
  2534. switch (cmd) {
  2535. case CLI_INIT:
  2536. e->command = "ooh323 show peer";
  2537. e->usage =
  2538. "Usage: ooh323 show peer <name>\n"
  2539. " List details of specific OOH323 peer.\n";
  2540. return NULL;
  2541. case CLI_GENERATE:
  2542. return NULL;
  2543. }
  2544. if (a->argc != 4)
  2545. return CLI_SHOWUSAGE;
  2546. ast_mutex_lock(&peerl.lock);
  2547. peer = peerl.peers;
  2548. while (peer) {
  2549. ast_mutex_lock(&peer->lock);
  2550. if (!strcmp(peer->name, a->argv[3])) {
  2551. break;
  2552. } else {
  2553. prev = peer;
  2554. peer = peer->next;
  2555. ast_mutex_unlock(&prev->lock);
  2556. }
  2557. }
  2558. if (peer) {
  2559. sprintf(ip_port, "%s:%d", peer->ip, peer->port);
  2560. ast_cli(a->fd, "%-15.15s%s\n", "Name: ", peer->name);
  2561. ast_cli(a->fd, "%s:%s,%s\n", "FastStart/H.245 Tunneling", peer->faststart?"yes":"no",
  2562. peer->h245tunneling?"yes":"no");
  2563. ast_cli(a->fd, "%-15.15s%s", "Format Prefs: ", "(");
  2564. print_codec_to_cli(a->fd, &peer->prefs);
  2565. ast_cli(a->fd, ")\n");
  2566. ast_cli(a->fd, "%-15.15s", "DTMF Mode: ");
  2567. if (peer->dtmfmode & H323_DTMF_CISCO) {
  2568. ast_cli(a->fd, "%s\n", "cisco");
  2569. ast_cli(a->fd, "%-15.15s%d\n", "DTMF Codec: ", peer->dtmfcodec);
  2570. } else if (peer->dtmfmode & H323_DTMF_RFC2833) {
  2571. ast_cli(a->fd, "%s\n", "rfc2833");
  2572. ast_cli(a->fd, "%-15.15s%d\n", "DTMF Codec: ", peer->dtmfcodec);
  2573. } else if (peer->dtmfmode & H323_DTMF_Q931) {
  2574. ast_cli(a->fd, "%s\n", "q931keypad");
  2575. } else if (peer->dtmfmode & H323_DTMF_H245ALPHANUMERIC) {
  2576. ast_cli(a->fd, "%s\n", "h245alphanumeric");
  2577. } else if (peer->dtmfmode & H323_DTMF_H245SIGNAL) {
  2578. ast_cli(a->fd, "%s\n", "h245signal");
  2579. } else if (peer->dtmfmode & H323_DTMF_INBAND && peer->dtmfmode & H323_DTMF_INBANDRELAX) {
  2580. ast_cli(a->fd, "%s\n", "inband-relaxed");
  2581. } else if (peer->dtmfmode & H323_DTMF_INBAND) {
  2582. ast_cli(a->fd, "%s\n", "inband");
  2583. } else {
  2584. ast_cli(a->fd, "%s\n", "unknown");
  2585. }
  2586. ast_cli(a->fd,"%-15s", "T.38 Mode: ");
  2587. if (peer->t38support == T38_DISABLED) {
  2588. ast_cli(a->fd, "%s\n", "disabled");
  2589. } else if (peer->t38support == T38_FAXGW) {
  2590. ast_cli(a->fd, "%s\n", "faxgw/chan_sip compatible");
  2591. }
  2592. if (peer->faxdetect == (FAXDETECT_CNG | FAXDETECT_T38)) {
  2593. ast_cli(a->fd,"%-20s%s\n", "FAX Detect:", "Yes");
  2594. } else if (peer->faxdetect & FAXDETECT_CNG) {
  2595. ast_cli(a->fd,"%-20s%s\n", "FAX Detect:", "Cng");
  2596. } else if (peer->faxdetect & FAXDETECT_T38) {
  2597. ast_cli(a->fd,"%-20s%s\n", "FAX Detect:", "T.38");
  2598. } else {
  2599. ast_cli(a->fd,"%-20s%s\n", "FAX Detect:", "No");
  2600. }
  2601. ast_cli(a->fd, "%-15.15s%s\n", "AccountCode: ", peer->accountcode);
  2602. ast_cli(a->fd, "%-15.15s%s\n", "AMA flags: ", ast_cdr_flags2str(peer->amaflags));
  2603. ast_cli(a->fd, "%-15.15s%s\n", "IP:Port: ", ip_port);
  2604. ast_cli(a->fd, "%-15.15s%d\n", "OutgoingLimit: ", peer->outgoinglimit);
  2605. ast_cli(a->fd, "%-15.15s%d\n", "rtptimeout: ", peer->rtptimeout);
  2606. if (peer->rtpmaskstr[0]) {
  2607. ast_cli(a->fd, "%-15.15s%s\n", "rtpmask: ", peer->rtpmaskstr);
  2608. }
  2609. if (peer->rtdrcount && peer->rtdrinterval) {
  2610. ast_cli(a->fd, "%-15.15s%d,%d\n", "RoundTrip: ", peer->rtdrcount, peer->rtdrinterval);
  2611. }
  2612. ast_mutex_unlock(&peer->lock);
  2613. } else {
  2614. ast_cli(a->fd, "Peer %s not found\n", a->argv[3]);
  2615. ast_cli(a->fd, "\n");
  2616. }
  2617. ast_mutex_unlock(&peerl.lock);
  2618. return CLI_SUCCESS;
  2619. }
  2620. static char *handle_cli_ooh323_show_peers(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2621. {
  2622. struct ooh323_peer *prev = NULL, *peer = NULL;
  2623. char formats[FORMAT_STRING_SIZE];
  2624. char ip_port[30];
  2625. #define FORMAT "%-15.15s %-15.15s %-23.23s %-s\n"
  2626. switch (cmd) {
  2627. case CLI_INIT:
  2628. e->command = "ooh323 show peers";
  2629. e->usage =
  2630. "Usage: ooh323 show peers\n"
  2631. " Lists all known OOH323 peers.\n";
  2632. return NULL;
  2633. case CLI_GENERATE:
  2634. return NULL;
  2635. }
  2636. if (a->argc != 3)
  2637. return CLI_SHOWUSAGE;
  2638. ast_cli(a->fd, FORMAT, "Name", "Accountcode", "ip:port", "Formats");
  2639. ast_mutex_lock(&peerl.lock);
  2640. peer = peerl.peers;
  2641. while (peer) {
  2642. ast_mutex_lock(&peer->lock);
  2643. snprintf(ip_port, sizeof(ip_port), "%s:%d", peer->ip, peer->port);
  2644. ast_cli(a->fd, FORMAT, peer->name,
  2645. peer->accountcode,
  2646. ip_port,
  2647. ast_getformatname_multiple(formats,FORMAT_STRING_SIZE,peer->cap));
  2648. prev = peer;
  2649. peer = peer->next;
  2650. ast_mutex_unlock(&prev->lock);
  2651. }
  2652. ast_mutex_unlock(&peerl.lock);
  2653. #undef FORMAT
  2654. return CLI_SUCCESS;
  2655. }
  2656. /*! \brief Print codec list from preference to CLI/manager */
  2657. static void print_codec_to_cli(int fd, struct ast_codec_pref *pref)
  2658. {
  2659. int x;
  2660. struct ast_format tmpfmt;
  2661. for (x = 0; x < 32; x++) {
  2662. ast_codec_pref_index(pref, x, &tmpfmt);
  2663. if (!tmpfmt.id)
  2664. break;
  2665. ast_cli(fd, "%s", ast_getformatname(&tmpfmt));
  2666. ast_cli(fd, ":%d", pref->framing[x]);
  2667. if (x < 31 && ast_codec_pref_index(pref, x + 1, &tmpfmt))
  2668. ast_cli(fd, ",");
  2669. }
  2670. if (!x)
  2671. ast_cli(fd, "none");
  2672. }
  2673. static char *handle_cli_ooh323_show_user(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2674. {
  2675. struct ooh323_user *prev = NULL, *user = NULL;
  2676. switch (cmd) {
  2677. case CLI_INIT:
  2678. e->command = "ooh323 show user";
  2679. e->usage =
  2680. "Usage: ooh323 show user <name>\n"
  2681. " List details of specific OOH323 user.\n";
  2682. return NULL;
  2683. case CLI_GENERATE:
  2684. return NULL;
  2685. }
  2686. if (a->argc != 4)
  2687. return CLI_SHOWUSAGE;
  2688. ast_mutex_lock(&userl.lock);
  2689. user = userl.users;
  2690. while (user) {
  2691. ast_mutex_lock(&user->lock);
  2692. if (!strcmp(user->name, a->argv[3])) {
  2693. break;
  2694. } else {
  2695. prev = user;
  2696. user = user->next;
  2697. ast_mutex_unlock(&prev->lock);
  2698. }
  2699. }
  2700. if (user) {
  2701. ast_cli(a->fd, "%-15.15s%s\n", "Name: ", user->name);
  2702. ast_cli(a->fd, "%s:%s,%s\n", "FastStart/H.245 Tunneling", user->faststart?"yes":"no",
  2703. user->h245tunneling?"yes":"no");
  2704. ast_cli(a->fd, "%-15.15s%s", "Format Prefs: ", "(");
  2705. print_codec_to_cli(a->fd, &user->prefs);
  2706. ast_cli(a->fd, ")\n");
  2707. ast_cli(a->fd, "%-15.15s", "DTMF Mode: ");
  2708. if (user->dtmfmode & H323_DTMF_CISCO) {
  2709. ast_cli(a->fd, "%s\n", "cisco");
  2710. ast_cli(a->fd, "%-15.15s%d\n", "DTMF Codec: ", user->dtmfcodec);
  2711. } else if (user->dtmfmode & H323_DTMF_RFC2833) {
  2712. ast_cli(a->fd, "%s\n", "rfc2833");
  2713. ast_cli(a->fd, "%-15.15s%d\n", "DTMF Codec: ", user->dtmfcodec);
  2714. } else if (user->dtmfmode & H323_DTMF_Q931) {
  2715. ast_cli(a->fd, "%s\n", "q931keypad");
  2716. } else if (user->dtmfmode & H323_DTMF_H245ALPHANUMERIC) {
  2717. ast_cli(a->fd, "%s\n", "h245alphanumeric");
  2718. } else if (user->dtmfmode & H323_DTMF_H245SIGNAL) {
  2719. ast_cli(a->fd, "%s\n", "h245signal");
  2720. } else if (user->dtmfmode & H323_DTMF_INBAND && user->dtmfmode & H323_DTMF_INBANDRELAX) {
  2721. ast_cli(a->fd, "%s\n", "inband-relaxed");
  2722. } else if (user->dtmfmode & H323_DTMF_INBAND) {
  2723. ast_cli(a->fd, "%s\n", "inband");
  2724. } else {
  2725. ast_cli(a->fd, "%s\n", "unknown");
  2726. }
  2727. ast_cli(a->fd,"%-15s", "T.38 Mode: ");
  2728. if (user->t38support == T38_DISABLED) {
  2729. ast_cli(a->fd, "%s\n", "disabled");
  2730. } else if (user->t38support == T38_FAXGW) {
  2731. ast_cli(a->fd, "%s\n", "faxgw/chan_sip compatible");
  2732. }
  2733. if (user->faxdetect == (FAXDETECT_CNG | FAXDETECT_T38)) {
  2734. ast_cli(a->fd,"%-20s%s\n", "FAX Detect:", "Yes");
  2735. } else if (user->faxdetect & FAXDETECT_CNG) {
  2736. ast_cli(a->fd,"%-20s%s\n", "FAX Detect:", "Cng");
  2737. } else if (user->faxdetect & FAXDETECT_T38) {
  2738. ast_cli(a->fd,"%-20s%s\n", "FAX Detect:", "T.38");
  2739. } else {
  2740. ast_cli(a->fd,"%-20s%s\n", "FAX Detect:", "No");
  2741. }
  2742. ast_cli(a->fd, "%-15.15s%s\n", "AccountCode: ", user->accountcode);
  2743. ast_cli(a->fd, "%-15.15s%s\n", "AMA flags: ", ast_cdr_flags2str(user->amaflags));
  2744. ast_cli(a->fd, "%-15.15s%s\n", "Context: ", user->context);
  2745. ast_cli(a->fd, "%-15.15s%d\n", "IncomingLimit: ", user->incominglimit);
  2746. ast_cli(a->fd, "%-15.15s%d\n", "InUse: ", user->inUse);
  2747. ast_cli(a->fd, "%-15.15s%d\n", "rtptimeout: ", user->rtptimeout);
  2748. if (user->rtpmaskstr[0]) {
  2749. ast_cli(a->fd, "%-15.15s%s\n", "rtpmask: ", user->rtpmaskstr);
  2750. }
  2751. ast_mutex_unlock(&user->lock);
  2752. if (user->rtdrcount && user->rtdrinterval) {
  2753. ast_cli(a->fd, "%-15.15s%d,%d\n", "RoundTrip: ", user->rtdrcount, user->rtdrinterval);
  2754. }
  2755. } else {
  2756. ast_cli(a->fd, "User %s not found\n", a->argv[3]);
  2757. ast_cli(a->fd, "\n");
  2758. }
  2759. ast_mutex_unlock(&userl.lock);
  2760. return CLI_SUCCESS;
  2761. }
  2762. static char *handle_cli_ooh323_show_users(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2763. {
  2764. struct ooh323_user *prev = NULL, *user = NULL;
  2765. char formats[FORMAT_STRING_SIZE];
  2766. #define FORMAT1 "%-15.15s %-15.15s %-15.15s %-s\n"
  2767. switch (cmd) {
  2768. case CLI_INIT:
  2769. e->command = "ooh323 show users";
  2770. e->usage =
  2771. "Usage: ooh323 show users \n"
  2772. " Lists all known OOH323 users.\n";
  2773. return NULL;
  2774. case CLI_GENERATE:
  2775. return NULL;
  2776. }
  2777. if (a->argc != 3)
  2778. return CLI_SHOWUSAGE;
  2779. ast_cli(a->fd, FORMAT1, "Username", "Accountcode", "Context", "Formats");
  2780. ast_mutex_lock(&userl.lock);
  2781. user = userl.users;
  2782. while(user)
  2783. {
  2784. ast_mutex_lock(&user->lock);
  2785. ast_cli(a->fd, FORMAT1, user->name,
  2786. user->accountcode, user->context,
  2787. ast_getformatname_multiple(formats, FORMAT_STRING_SIZE, user->cap));
  2788. prev = user;
  2789. user = user->next;
  2790. ast_mutex_unlock(&prev->lock);
  2791. }
  2792. ast_mutex_unlock(&userl.lock);
  2793. #undef FORMAT1
  2794. return RESULT_SUCCESS;
  2795. }
  2796. static char *handle_cli_ooh323_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2797. {
  2798. switch (cmd) {
  2799. case CLI_INIT:
  2800. e->command = "ooh323 set debug [off]";
  2801. e->usage =
  2802. "Usage: ooh323 set debug [off]\n"
  2803. " Enables/Disables debugging of OOH323 channel driver\n";
  2804. return NULL;
  2805. case CLI_GENERATE:
  2806. return NULL;
  2807. }
  2808. if (a->argc < 3 || a->argc > 4)
  2809. return CLI_SHOWUSAGE;
  2810. if (a->argc == 4 && strcasecmp(a->argv[3], "off"))
  2811. return CLI_SHOWUSAGE;
  2812. gH323Debug = (a->argc == 4) ? FALSE : TRUE;
  2813. ast_cli(a->fd, "OOH323 Debugging %s\n", gH323Debug ? "Enabled" : "Disabled");
  2814. return CLI_SUCCESS;
  2815. }
  2816. #if 0
  2817. static int ooh323_show_channels(int fd, int argc, char *argv[])
  2818. {
  2819. return RESULT_SUCCESS;
  2820. }
  2821. #endif
  2822. static char *handle_cli_ooh323_show_config(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2823. {
  2824. char value[FORMAT_STRING_SIZE];
  2825. ooAliases *pAlias = NULL, *pAliasNext = NULL;;
  2826. switch (cmd) {
  2827. case CLI_INIT:
  2828. e->command = "ooh323 show config";
  2829. e->usage =
  2830. "Usage: ooh323 show config\n"
  2831. " Shows global configuration of H.323 channel driver\n";
  2832. return NULL;
  2833. case CLI_GENERATE:
  2834. return NULL;
  2835. }
  2836. if (a->argc != 3)
  2837. return CLI_SHOWUSAGE;
  2838. ast_cli(a->fd, "\nObjective Open H.323 Channel Driver's Config:\n");
  2839. snprintf(value, sizeof(value), "%s:%d", gIP, gPort);
  2840. ast_cli(a->fd, "%-20s%s\n", "IP:Port: ", value);
  2841. ast_cli(a->fd, "%-20s%d-%d\n", "H.225 port range: ", ooconfig.mTCPPortStart, ooconfig.mTCPPortEnd);
  2842. ast_cli(a->fd, "%-20s%s\n", "FastStart", gFastStart?"yes":"no");
  2843. ast_cli(a->fd, "%-20s%s\n", "Tunneling", gTunneling?"yes":"no");
  2844. ast_cli(a->fd, "%-20s%s\n", "CallerId", gCallerID);
  2845. ast_cli(a->fd, "%-20s%s\n", "MediaWaitForConnect", gMediaWaitForConnect?"yes":"no");
  2846. #if (0)
  2847. extern OOH323EndPoint gH323ep;
  2848. ast_cli(a->fd, "%-20s%s\n", "FASTSTART",
  2849. (OO_TESTFLAG(gH323ep.flags, OO_M_FASTSTART) != 0) ? "yes" : "no");
  2850. ast_cli(a->fd, "%-20s%s\n", "TUNNELING",
  2851. (OO_TESTFLAG(gH323ep.flags, OO_M_TUNNELING) != 0) ? "yes" : "no");
  2852. ast_cli(a->fd, "%-20s%s\n", "MEDIAWAITFORCONN",
  2853. (OO_TESTFLAG(gH323ep.flags, OO_M_MEDIAWAITFORCONN) != 0) ? "yes" : "no");
  2854. #endif
  2855. if (gRasGkMode == RasNoGatekeeper) {
  2856. snprintf(value, sizeof(value), "%s", "No Gatekeeper");
  2857. } else if (gRasGkMode == RasDiscoverGatekeeper) {
  2858. snprintf(value, sizeof(value), "%s", "Discover");
  2859. } else {
  2860. snprintf(value, sizeof(value), "%s", gGatekeeper);
  2861. }
  2862. ast_cli(a->fd, "%-20s%s\n", "Gatekeeper:", value);
  2863. ast_cli(a->fd, "%-20s%s\n", "H.323 LogFile:", gLogFile);
  2864. ast_cli(a->fd, "%-20s%s\n", "Context:", gContext);
  2865. ast_cli(a->fd, "%-20s%s\n", "Capability:",
  2866. ast_getformatname_multiple(value,FORMAT_STRING_SIZE,gCap));
  2867. ast_cli(a->fd, "%-20s", "DTMF Mode: ");
  2868. if (gDTMFMode & H323_DTMF_CISCO) {
  2869. ast_cli(a->fd, "%s\n", "cisco");
  2870. ast_cli(a->fd, "%-20.15s%d\n", "DTMF Codec: ", gDTMFCodec);
  2871. } else if (gDTMFMode & H323_DTMF_RFC2833) {
  2872. ast_cli(a->fd, "%s\n", "rfc2833");
  2873. ast_cli(a->fd, "%-20.15s%d\n", "DTMF Codec: ", gDTMFCodec);
  2874. } else if (gDTMFMode & H323_DTMF_Q931) {
  2875. ast_cli(a->fd, "%s\n", "q931keypad");
  2876. } else if (gDTMFMode & H323_DTMF_H245ALPHANUMERIC) {
  2877. ast_cli(a->fd, "%s\n", "h245alphanumeric");
  2878. } else if (gDTMFMode & H323_DTMF_H245SIGNAL) {
  2879. ast_cli(a->fd, "%s\n", "h245signal");
  2880. } else if (gDTMFMode & H323_DTMF_INBAND && gDTMFMode & H323_DTMF_INBANDRELAX) {
  2881. ast_cli(a->fd, "%s\n", "inband-relaxed");
  2882. } else if (gDTMFMode & H323_DTMF_INBAND) {
  2883. ast_cli(a->fd, "%s\n", "inband");
  2884. } else {
  2885. ast_cli(a->fd, "%s\n", "unknown");
  2886. }
  2887. ast_cli(a->fd,"%-20s", "T.38 Mode: ");
  2888. if (gT38Support == T38_DISABLED) {
  2889. ast_cli(a->fd, "%s\n", "disabled");
  2890. } else if (gT38Support == T38_FAXGW) {
  2891. ast_cli(a->fd, "%s\n", "faxgw/chan_sip compatible");
  2892. }
  2893. if (gFAXdetect == (FAXDETECT_CNG | FAXDETECT_T38)) {
  2894. ast_cli(a->fd,"%-20s%s\n", "FAX Detect:", "Yes");
  2895. } else if (gFAXdetect & FAXDETECT_CNG) {
  2896. ast_cli(a->fd,"%-20s%s\n", "FAX Detect:", "Cng");
  2897. } else if (gFAXdetect & FAXDETECT_T38) {
  2898. ast_cli(a->fd,"%-20s%s\n", "FAX Detect:", "T.38");
  2899. } else {
  2900. ast_cli(a->fd,"%-20s%s\n", "FAX Detect:", "No");
  2901. }
  2902. if (gRTDRCount && gRTDRInterval) {
  2903. ast_cli(a->fd, "%-20.15s%d,%d\n", "RoundTrip: ", gRTDRCount, gRTDRInterval);
  2904. }
  2905. ast_cli(a->fd, "%-20s%ld\n", "Call counter: ", callnumber);
  2906. ast_cli(a->fd, "%-20s%s\n", "AccountCode: ", gAccountcode);
  2907. ast_cli(a->fd, "%-20s%s\n", "AMA flags: ", ast_cdr_flags2str(gAMAFLAGS));
  2908. pAlias = gAliasList;
  2909. if(pAlias) {
  2910. ast_cli(a->fd, "%-20s\n", "Aliases: ");
  2911. }
  2912. while (pAlias) {
  2913. pAliasNext = pAlias->next;
  2914. if (pAliasNext) {
  2915. ast_cli(a->fd,"\t%-30s\t%-30s\n",pAlias->value, pAliasNext->value);
  2916. pAlias = pAliasNext->next;
  2917. } else {
  2918. ast_cli(a->fd,"\t%-30s\n",pAlias->value);
  2919. pAlias = pAlias->next;
  2920. }
  2921. }
  2922. return CLI_SUCCESS;
  2923. }
  2924. static struct ast_cli_entry cli_ooh323[] = {
  2925. AST_CLI_DEFINE(handle_cli_ooh323_set_debug, "Enable/Disable OOH323 debugging"),
  2926. AST_CLI_DEFINE(handle_cli_ooh323_show_config, "Show details on global configuration of H.323 channel driver"),
  2927. AST_CLI_DEFINE(handle_cli_ooh323_show_peer, "Show details on specific OOH323 peer"),
  2928. AST_CLI_DEFINE(handle_cli_ooh323_show_peers, "Show defined OOH323 peers"),
  2929. AST_CLI_DEFINE(handle_cli_ooh323_show_user, "Show details on specific OOH323 user"),
  2930. AST_CLI_DEFINE(handle_cli_ooh323_show_users, "Show defined OOH323 users"),
  2931. AST_CLI_DEFINE(handle_cli_ooh323_reload, "reload ooh323 config")
  2932. };
  2933. /*! \brief OOH323 Dialplan function - reads ooh323 settings */
  2934. static int function_ooh323_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  2935. {
  2936. struct ooh323_pvt *p = chan->tech_pvt;
  2937. ast_channel_lock(chan);
  2938. if (!p) {
  2939. ast_channel_unlock(chan);
  2940. return -1;
  2941. }
  2942. if (strcmp(chan->tech->type, "OOH323")) {
  2943. ast_log(LOG_ERROR, "This function is only supported on OOH323 channels, Channel is %s\n", chan->tech->type);
  2944. ast_channel_unlock(chan);
  2945. return -1;
  2946. }
  2947. ast_mutex_lock(&p->lock);
  2948. if (!strcasecmp(data, "faxdetect")) {
  2949. ast_copy_string(buf, p->faxdetect ? "1" : "0", len);
  2950. } else if (!strcasecmp(data, "t38support")) {
  2951. ast_copy_string(buf, p->t38support ? "1" : "0", len);
  2952. } else if (!strcasecmp(data, "caller_h323id")) {
  2953. ast_copy_string(buf, p->caller_h323id, len);
  2954. } else if (!strcasecmp(data, "caller_dialeddigits")) {
  2955. ast_copy_string(buf, p->caller_dialedDigits, len);
  2956. } else if (!strcasecmp(data, "caller_email")) {
  2957. ast_copy_string(buf, p->caller_email, len);
  2958. } else if (!strcasecmp(data, "h323id_url")) {
  2959. ast_copy_string(buf, p->caller_url, len);
  2960. } else if (!strcasecmp(data, "callee_h323id")) {
  2961. ast_copy_string(buf, p->callee_h323id, len);
  2962. } else if (!strcasecmp(data, "callee_dialeddigits")) {
  2963. ast_copy_string(buf, p->callee_dialedDigits, len);
  2964. } else if (!strcasecmp(data, "callee_email")) {
  2965. ast_copy_string(buf, p->callee_email, len);
  2966. } else if (!strcasecmp(data, "callee_url")) {
  2967. ast_copy_string(buf, p->callee_url, len);
  2968. }
  2969. ast_mutex_unlock(&p->lock);
  2970. ast_channel_unlock(chan);
  2971. return 0;
  2972. }
  2973. /*! \brief OOH323 Dialplan function - writes ooh323 settings */
  2974. static int function_ooh323_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  2975. {
  2976. struct ooh323_pvt *p = chan->tech_pvt;
  2977. int res = -1;
  2978. ast_channel_lock(chan);
  2979. if (!p) {
  2980. ast_channel_unlock(chan);
  2981. return -1;
  2982. }
  2983. if (strcmp(chan->tech->type, "OOH323")) {
  2984. ast_log(LOG_ERROR, "This function is only supported on OOH323 channels, Channel is %s\n", chan->tech->type);
  2985. ast_channel_unlock(chan);
  2986. return -1;
  2987. }
  2988. ast_mutex_lock(&p->lock);
  2989. if (!strcasecmp(data, "faxdetect")) {
  2990. if (ast_true(value)) {
  2991. p->faxdetect = 1;
  2992. res = 0;
  2993. } else if (ast_false(value)) {
  2994. p->faxdetect = 0;
  2995. res = 0;
  2996. } else {
  2997. char *buf = ast_strdupa(value);
  2998. char *word, *next = buf;
  2999. p->faxdetect = 0;
  3000. res = 0;
  3001. while ((word = strsep(&next, ","))) {
  3002. if (!strcasecmp(word, "cng")) {
  3003. p->faxdetect |= FAXDETECT_CNG;
  3004. } else if (!strcasecmp(word, "t38")) {
  3005. p->faxdetect |= FAXDETECT_T38;
  3006. } else {
  3007. ast_log(LOG_WARNING, "Unknown faxdetect mode '%s'.\n", word);
  3008. res = -1;
  3009. }
  3010. }
  3011. }
  3012. } else if (!strcasecmp(data, "t38support")) {
  3013. if (ast_true(value)) {
  3014. p->t38support = 1;
  3015. res = 0;
  3016. } else {
  3017. p->t38support = 0;
  3018. res = 0;
  3019. }
  3020. }
  3021. ast_mutex_unlock(&p->lock);
  3022. ast_channel_unlock(chan);
  3023. return res;
  3024. }
  3025. /*! \brief Structure to declare a dialplan function: OOH323 */
  3026. static struct ast_custom_function ooh323_function = {
  3027. .name = "OOH323",
  3028. .read = function_ooh323_read,
  3029. .write = function_ooh323_write,
  3030. };
  3031. static int load_module(void)
  3032. {
  3033. int res;
  3034. struct ooAliases * pNewAlias = NULL;
  3035. struct ooh323_peer *peer = NULL;
  3036. struct ast_format tmpfmt;
  3037. OOH225MsgCallbacks h225Callbacks = {0, 0, 0, 0};
  3038. OOH323CALLBACKS h323Callbacks = {
  3039. .onNewCallCreated = onNewCallCreated,
  3040. .onAlerting = onAlerting,
  3041. .onProgress = onProgress,
  3042. .onIncomingCall = NULL,
  3043. .onOutgoingCall = onOutgoingCall,
  3044. .onCallEstablished = onCallEstablished,
  3045. .onCallCleared = onCallCleared,
  3046. .openLogicalChannels = NULL,
  3047. .onReceivedDTMF = ooh323_onReceivedDigit,
  3048. .onModeChanged = onModeChanged
  3049. };
  3050. if (!(gCap = ast_format_cap_alloc())) {
  3051. return 1;
  3052. }
  3053. if (!(ooh323_tech.capabilities = ast_format_cap_alloc())) {
  3054. return 1;
  3055. }
  3056. ast_format_cap_add(gCap, ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0));
  3057. ast_format_cap_add_all(ooh323_tech.capabilities);
  3058. myself = ast_module_info->self;
  3059. h225Callbacks.onReceivedSetup = &ooh323_onReceivedSetup;
  3060. userl.users = NULL;
  3061. ast_mutex_init(&userl.lock);
  3062. peerl.peers = NULL;
  3063. ast_mutex_init(&peerl.lock);
  3064. #if 0
  3065. ast_register_atexit(&ast_ooh323c_exit);
  3066. #endif
  3067. if (!(sched = ast_sched_context_create())) {
  3068. ast_log(LOG_WARNING, "Unable to create schedule context\n");
  3069. }
  3070. if (!(io = io_context_create())) {
  3071. ast_log(LOG_WARNING, "Unable to create I/O context\n");
  3072. }
  3073. if (!(res = reload_config(0))) {
  3074. /* Make sure we can register our OOH323 channel type */
  3075. if (ast_channel_register(&ooh323_tech)) {
  3076. ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
  3077. return 0;
  3078. }
  3079. ast_rtp_glue_register(&ooh323_rtp);
  3080. ast_udptl_proto_register(&ooh323_udptl);
  3081. ast_cli_register_multiple(cli_ooh323, sizeof(cli_ooh323) / sizeof(struct ast_cli_entry));
  3082. /* fire up the H.323 Endpoint */
  3083. if (OO_OK != ooH323EpInitialize(OO_CALLMODE_AUDIOCALL, gLogFile)) {
  3084. ast_log(LOG_ERROR, "Failed to initialize OOH323 endpoint-"
  3085. "OOH323 Disabled\n");
  3086. return 1;
  3087. }
  3088. if (gIsGateway)
  3089. ooH323EpSetAsGateway();
  3090. ooH323EpSetVersionInfo(t35countrycode, t35extensions, manufacturer,
  3091. vendor, version);
  3092. ooH323EpDisableAutoAnswer();
  3093. ooH323EpSetH225MsgCallbacks(h225Callbacks);
  3094. ooH323EpSetTraceLevel(gTRCLVL);
  3095. ooH323EpSetLocalAddress(gIP, gPort);
  3096. if (v6mode) {
  3097. ast_debug(1, "OOH323 channel is in IP6 mode\n");
  3098. }
  3099. ooH323EpSetCallerID(gCallerID);
  3100. if(ooH323EpSetTCPPortRange(ooconfig.mTCPPortStart,
  3101. ooconfig.mTCPPortEnd) == OO_FAILED) {
  3102. ast_log(LOG_ERROR, "h225portrange: Failed to set range\n");
  3103. }
  3104. /* Set aliases if any */
  3105. for (pNewAlias = gAliasList; pNewAlias; pNewAlias = pNewAlias->next) {
  3106. switch (pNewAlias->type) {
  3107. case T_H225AliasAddress_h323_ID:
  3108. ooH323EpAddAliasH323ID(pNewAlias->value);
  3109. break;
  3110. case T_H225AliasAddress_dialedDigits:
  3111. ooH323EpAddAliasDialedDigits(pNewAlias->value);
  3112. break;
  3113. case T_H225AliasAddress_email_ID:
  3114. ooH323EpAddAliasEmailID(pNewAlias->value);
  3115. break;
  3116. default:
  3117. ;
  3118. }
  3119. }
  3120. ast_mutex_lock(&peerl.lock);
  3121. peer = peerl.peers;
  3122. while (peer) {
  3123. if(peer->h323id) ooH323EpAddAliasH323ID(peer->h323id);
  3124. if(peer->email) ooH323EpAddAliasEmailID(peer->email);
  3125. if(peer->e164) ooH323EpAddAliasDialedDigits(peer->e164);
  3126. if(peer->url) ooH323EpAddAliasURLID(peer->url);
  3127. peer = peer->next;
  3128. }
  3129. ast_mutex_unlock(&peerl.lock);
  3130. if (gMediaWaitForConnect)
  3131. ooH323EpEnableMediaWaitForConnect();
  3132. else
  3133. ooH323EpDisableMediaWaitForConnect();
  3134. /* Fast start and tunneling options */
  3135. if (gFastStart)
  3136. ooH323EpEnableFastStart();
  3137. else
  3138. ooH323EpDisableFastStart();
  3139. if (!gTunneling)
  3140. ooH323EpDisableH245Tunneling();
  3141. if (gBeMaster)
  3142. ooH323EpTryBeMaster(1);
  3143. ooH323EpEnableManualRingback();
  3144. /* Gatekeeper */
  3145. if (gRasGkMode == RasUseSpecificGatekeeper)
  3146. ooGkClientInit(gRasGkMode, gGatekeeper, 0);
  3147. else if (gRasGkMode == RasDiscoverGatekeeper)
  3148. ooGkClientInit(gRasGkMode, 0, 0);
  3149. /* Register callbacks */
  3150. ooH323EpSetH323Callbacks(h323Callbacks);
  3151. /* Add endpoint capabilities */
  3152. if (ooh323c_set_capability(&gPrefs, gCap, gDTMFMode, gDTMFCodec) < 0) {
  3153. ast_log(LOG_ERROR, "Capabilities failure for OOH323. OOH323 Disabled.\n");
  3154. return 1;
  3155. }
  3156. /* Create H.323 listener */
  3157. if (ooCreateH323Listener() != OO_OK) {
  3158. ast_log(LOG_ERROR, "OOH323 Listener Creation failure. "
  3159. "OOH323 DISABLED\n");
  3160. ooH323EpDestroy();
  3161. return 1;
  3162. }
  3163. if (ooh323c_start_stack_thread() < 0) {
  3164. ast_log(LOG_ERROR, "Failed to start OOH323 stack thread. "
  3165. "OOH323 DISABLED\n");
  3166. ooH323EpDestroy();
  3167. return 1;
  3168. }
  3169. /* And start the monitor for the first time */
  3170. restart_monitor();
  3171. }
  3172. /* Register dialplan functions */
  3173. ast_custom_function_register(&ooh323_function);
  3174. return 0;
  3175. }
  3176. static void *do_monitor(void *data)
  3177. {
  3178. int res;
  3179. int reloading;
  3180. struct ooh323_pvt *h323 = NULL;
  3181. time_t t;
  3182. for (;;) {
  3183. struct ooh323_pvt *h323_next;
  3184. /* Check for a reload request */
  3185. ast_mutex_lock(&h323_reload_lock);
  3186. reloading = h323_reloading;
  3187. h323_reloading = 0;
  3188. ast_mutex_unlock(&h323_reload_lock);
  3189. if (reloading) {
  3190. ast_verb(1, "Reloading H.323\n");
  3191. ooh323_do_reload();
  3192. }
  3193. /* Check for interfaces needing to be killed */
  3194. ast_mutex_lock(&iflock);
  3195. time(&t);
  3196. h323 = iflist;
  3197. while (h323) {
  3198. h323_next = h323->next;
  3199. /* TODO: Need to add rtptimeout keepalive support */
  3200. if (ast_test_flag(h323, H323_NEEDDESTROY)) {
  3201. ooh323_destroy (h323);
  3202. } /* else if (ast_test_flag(h323, H323_NEEDSTART) && h323->owner) {
  3203. ast_channel_lock(h323->owner);
  3204. if (ast_pbx_start(h323->owner)) {
  3205. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", h323->owner->name);
  3206. ast_channel_unlock(h323->owner);
  3207. ast_hangup(h323->owner);
  3208. }
  3209. ast_channel_unlock(h323->owner);
  3210. ast_clear_flag(h323, H323_NEEDSTART);
  3211. } */
  3212. h323 = h323_next;
  3213. }
  3214. ast_mutex_unlock(&iflock);
  3215. pthread_testcancel();
  3216. /* Wait for sched or io */
  3217. res = ast_sched_wait(sched);
  3218. if ((res < 0) || (res > 1000)) {
  3219. res = 1000;
  3220. }
  3221. res = ast_io_wait(io, res);
  3222. pthread_testcancel();
  3223. ast_mutex_lock(&monlock);
  3224. if (res >= 0) {
  3225. ast_sched_runq(sched);
  3226. }
  3227. ast_mutex_unlock(&monlock);
  3228. }
  3229. /* Never reached */
  3230. return NULL;
  3231. }
  3232. int restart_monitor(void)
  3233. {
  3234. pthread_attr_t attr;
  3235. /* If we're supposed to be stopped -- stay stopped */
  3236. if (monitor_thread == AST_PTHREADT_STOP)
  3237. return 0;
  3238. if (ast_mutex_lock(&monlock)) {
  3239. ast_log(LOG_WARNING, "Unable to lock monitor\n");
  3240. return -1;
  3241. }
  3242. if (monitor_thread == pthread_self()) {
  3243. ast_mutex_unlock(&monlock);
  3244. ast_log(LOG_WARNING, "Cannot kill myself\n");
  3245. return -1;
  3246. }
  3247. if (monitor_thread != AST_PTHREADT_NULL) {
  3248. /* Wake up the thread */
  3249. pthread_kill(monitor_thread, SIGURG);
  3250. } else {
  3251. pthread_attr_init(&attr);
  3252. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  3253. /* Start a new monitor */
  3254. if (ast_pthread_create(&monitor_thread, &attr, do_monitor, NULL) < 0) {
  3255. ast_mutex_unlock(&monlock);
  3256. ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
  3257. return -1;
  3258. }
  3259. }
  3260. ast_mutex_unlock(&monlock);
  3261. return 0;
  3262. }
  3263. int ooh323_destroy(struct ooh323_pvt *p)
  3264. {
  3265. /* NOTE: Assumes iflock already acquired */
  3266. struct ooh323_pvt *prev = NULL, *cur = NULL;
  3267. struct ooh323_user *user = NULL;
  3268. if (gH323Debug) {
  3269. ast_verbose("--- ooh323_destroy \n");
  3270. if (p)
  3271. ast_verbose(" Destroying %s\n", p->username);
  3272. }
  3273. cur = iflist;
  3274. while (cur) {
  3275. if (cur == p) { break; }
  3276. prev = cur;
  3277. cur = cur->next;
  3278. }
  3279. if (cur) {
  3280. ast_mutex_lock(&cur->lock);
  3281. if (prev)
  3282. prev->next = cur->next;
  3283. else
  3284. iflist = cur->next;
  3285. if (cur->callToken) {
  3286. if (gH323Debug)
  3287. ast_verbose(" Destroying %s\n", cur->callToken);
  3288. ast_free(cur->callToken);
  3289. cur->callToken = 0;
  3290. }
  3291. if (cur->username) {
  3292. free(cur->username);
  3293. cur->username = 0;
  3294. }
  3295. if (cur->host) {
  3296. free(cur->host);
  3297. cur->host = 0;
  3298. }
  3299. if (cur->callerid_name) {
  3300. free(cur->callerid_name);
  3301. cur->callerid_name = 0;
  3302. }
  3303. if (cur->callerid_num) {
  3304. free(cur->callerid_num);
  3305. cur->callerid_num = 0;
  3306. }
  3307. if (cur->rtp) {
  3308. ast_rtp_instance_destroy(cur->rtp);
  3309. cur->rtp = NULL;
  3310. }
  3311. if (cur->udptl) {
  3312. ast_udptl_destroy(cur->udptl);
  3313. cur->udptl = NULL;
  3314. }
  3315. /* Unlink us from the owner if we have one */
  3316. if (cur->owner) {
  3317. while(ast_channel_trylock(cur->owner)) {
  3318. ast_debug(1, "Failed to grab lock, trying again\n");
  3319. DEADLOCK_AVOIDANCE(&cur->lock);
  3320. }
  3321. ast_debug(1, "Detaching from %s\n", cur->owner->name);
  3322. cur->owner->tech_pvt = NULL;
  3323. ast_channel_unlock(cur->owner);
  3324. cur->owner = NULL;
  3325. ast_module_unref(myself);
  3326. }
  3327. if (cur->vad) {
  3328. ast_dsp_free(cur->vad);
  3329. cur->vad = NULL;
  3330. }
  3331. /* decrement user/peer count */
  3332. if(!ast_test_flag(cur, H323_OUTGOING)) {
  3333. if (cur->neighbor.user) {
  3334. user = find_user(p->callerid_name, cur->neighbor.user);
  3335. if(user && user->inUse > 0) {
  3336. ast_mutex_lock(&user->lock);
  3337. user->inUse--;
  3338. ast_mutex_unlock(&user->lock);
  3339. }
  3340. free(cur->neighbor.user);
  3341. }
  3342. } else {
  3343. /* outgoing limit decrement here !!! */
  3344. }
  3345. ast_mutex_unlock(&cur->lock);
  3346. ast_mutex_destroy(&cur->lock);
  3347. cur->cap = ast_format_cap_destroy(cur->cap);
  3348. ast_free(cur);
  3349. }
  3350. if (gH323Debug)
  3351. ast_verbose("+++ ooh323_destroy\n");
  3352. return 0;
  3353. }
  3354. int delete_peers()
  3355. {
  3356. struct ooh323_peer *cur = NULL, *prev = NULL;
  3357. ast_mutex_lock(&peerl.lock);
  3358. cur = peerl.peers;
  3359. while (cur) {
  3360. prev = cur;
  3361. cur = cur->next;
  3362. ast_mutex_destroy(&prev->lock);
  3363. if(prev->h323id) free(prev->h323id);
  3364. if(prev->email) free(prev->email);
  3365. if(prev->url) free(prev->url);
  3366. if(prev->e164) free(prev->e164);
  3367. if(prev->rtpmask) {
  3368. ast_mutex_lock(&prev->rtpmask->lock);
  3369. prev->rtpmask->inuse--;
  3370. ast_mutex_unlock(&prev->rtpmask->lock);
  3371. if (prev->rtpmask->inuse == 0) {
  3372. regfree(&prev->rtpmask->regex);
  3373. ast_mutex_destroy(&prev->rtpmask->lock);
  3374. free(prev->rtpmask);
  3375. }
  3376. }
  3377. free(prev);
  3378. if (cur == peerl.peers) {
  3379. break;
  3380. }
  3381. }
  3382. peerl.peers = NULL;
  3383. ast_mutex_unlock(&peerl.lock);
  3384. return 0;
  3385. }
  3386. int delete_users()
  3387. {
  3388. struct ooh323_user *cur = NULL, *prev = NULL;
  3389. ast_mutex_lock(&userl.lock);
  3390. cur = userl.users;
  3391. while (cur) {
  3392. prev = cur;
  3393. cur = cur->next;
  3394. ast_mutex_destroy(&prev->lock);
  3395. if(prev->rtpmask) {
  3396. ast_mutex_lock(&prev->rtpmask->lock);
  3397. prev->rtpmask->inuse--;
  3398. ast_mutex_unlock(&prev->rtpmask->lock);
  3399. if (prev->rtpmask->inuse == 0) {
  3400. regfree(&prev->rtpmask->regex);
  3401. ast_mutex_destroy(&prev->rtpmask->lock);
  3402. free(prev->rtpmask);
  3403. }
  3404. }
  3405. prev->cap = ast_format_cap_destroy(prev->cap);
  3406. free(prev);
  3407. if (cur == userl.users) {
  3408. break;
  3409. }
  3410. }
  3411. userl.users = NULL;
  3412. ast_mutex_unlock(&userl.lock);
  3413. return 0;
  3414. }
  3415. static int unload_module(void)
  3416. {
  3417. struct ooh323_pvt *p;
  3418. struct ooAliases *cur = NULL, *prev = NULL;
  3419. if (gH323Debug) {
  3420. ast_verbose("--- ooh323 unload_module \n");
  3421. }
  3422. /* First, take us out of the channel loop */
  3423. ast_cli_unregister_multiple(cli_ooh323, sizeof(cli_ooh323) / sizeof(struct ast_cli_entry));
  3424. ast_rtp_glue_unregister(&ooh323_rtp);
  3425. ast_udptl_proto_unregister(&ooh323_udptl);
  3426. ast_channel_unregister(&ooh323_tech);
  3427. #if 0
  3428. ast_unregister_atexit(&ast_ooh323c_exit);
  3429. #endif
  3430. if (gH323Debug) {
  3431. ast_verbose(" unload_module - hanging up all interfaces\n");
  3432. }
  3433. if (!ast_mutex_lock(&iflock)) {
  3434. /* Hangup all interfaces if they have an owner */
  3435. p = iflist;
  3436. while (p) {
  3437. if (p->owner) {
  3438. ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
  3439. }
  3440. p = p->next;
  3441. }
  3442. iflist = NULL;
  3443. ast_mutex_unlock(&iflock);
  3444. } else {
  3445. ast_log(LOG_WARNING, "Unable to lock the interface list\n");
  3446. return -1;
  3447. }
  3448. if (gH323Debug) {
  3449. ast_verbose(" unload_module - stopping monitor thread\n");
  3450. }
  3451. if (monitor_thread != AST_PTHREADT_NULL) {
  3452. if (!ast_mutex_lock(&monlock)) {
  3453. if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
  3454. pthread_cancel(monitor_thread);
  3455. pthread_kill(monitor_thread, SIGURG);
  3456. pthread_join(monitor_thread, NULL);
  3457. }
  3458. monitor_thread = AST_PTHREADT_STOP;
  3459. ast_mutex_unlock(&monlock);
  3460. } else {
  3461. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  3462. return -1;
  3463. }
  3464. }
  3465. if (gH323Debug) {
  3466. ast_verbose(" unload_module - stopping stack thread\n");
  3467. }
  3468. ooh323c_stop_stack_thread();
  3469. if (gH323Debug) {
  3470. ast_verbose(" unload_module - freeing up memory used by interfaces\n");
  3471. }
  3472. if (!ast_mutex_lock(&iflock)) {
  3473. struct ooh323_pvt *pl;
  3474. /* Destroy all the interfaces and free their memory */
  3475. p = iflist;
  3476. while (p) {
  3477. pl = p;
  3478. p = p->next;
  3479. /* Free associated memory */
  3480. ooh323_destroy(pl);
  3481. }
  3482. iflist = NULL;
  3483. ast_mutex_unlock(&iflock);
  3484. } else {
  3485. ast_log(LOG_WARNING, "Unable to lock the interface list\n");
  3486. return -1;
  3487. }
  3488. if (gH323Debug) {
  3489. ast_verbose(" unload_module - deleting users\n");
  3490. }
  3491. delete_users();
  3492. if (gH323Debug) {
  3493. ast_verbose(" unload_module - deleting peers\n");
  3494. }
  3495. delete_peers();
  3496. if (gH323Debug) {
  3497. ast_verbose(" unload_module - Freeing up alias list\n");
  3498. }
  3499. cur = gAliasList;
  3500. while (cur) {
  3501. prev = cur;
  3502. cur = cur->next;
  3503. free(prev->value);
  3504. free(prev);
  3505. }
  3506. gAliasList = NULL;
  3507. if (gH323Debug) {
  3508. ast_verbose(" unload_module- destroying OOH323 endpoint \n");
  3509. }
  3510. ooH323EpDestroy();
  3511. /* Unregister dial plan functions */
  3512. ast_custom_function_unregister(&ooh323_function);
  3513. if (gH323Debug) {
  3514. ast_verbose("+++ ooh323 unload_module \n");
  3515. }
  3516. gCap = ast_format_cap_destroy(gCap);
  3517. ooh323_tech.capabilities = ast_format_cap_destroy(ooh323_tech.capabilities);
  3518. return 0;
  3519. }
  3520. static enum ast_rtp_glue_result ooh323_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **rtp)
  3521. {
  3522. struct ooh323_pvt *p = NULL;
  3523. enum ast_rtp_glue_result res = AST_RTP_GLUE_RESULT_LOCAL;
  3524. if (!(p = (struct ooh323_pvt *) chan->tech_pvt))
  3525. return AST_RTP_GLUE_RESULT_FORBID;
  3526. if (!(p->rtp)) {
  3527. return AST_RTP_GLUE_RESULT_FORBID;
  3528. }
  3529. *rtp = p->rtp ? ao2_ref(p->rtp, +1), p->rtp : NULL;
  3530. res = AST_RTP_GLUE_RESULT_LOCAL;
  3531. if (ast_test_flag(&global_jbconf, AST_JB_FORCED)) {
  3532. res = AST_RTP_GLUE_RESULT_FORBID;
  3533. }
  3534. return res;
  3535. }
  3536. static enum ast_rtp_glue_result ooh323_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp_instance **rtp)
  3537. {
  3538. struct ooh323_pvt *p = NULL;
  3539. enum ast_rtp_glue_result res = AST_RTP_GLUE_RESULT_LOCAL;
  3540. if (!(p = (struct ooh323_pvt *) chan->tech_pvt))
  3541. return AST_RTP_GLUE_RESULT_FORBID;
  3542. if (!(p->rtp)) {
  3543. return AST_RTP_GLUE_RESULT_FORBID;
  3544. }
  3545. *rtp = p->vrtp ? ao2_ref(p->vrtp, +1), p->vrtp : NULL;
  3546. res = AST_RTP_GLUE_RESULT_LOCAL;
  3547. return res;
  3548. }
  3549. int ooh323_update_capPrefsOrderForCall
  3550. (ooCallData *call, struct ast_codec_pref *prefs)
  3551. {
  3552. int i = 0;
  3553. struct ast_format tmpfmt;
  3554. ast_codec_pref_index(prefs, i, &tmpfmt);
  3555. ooResetCapPrefs(call);
  3556. while (tmpfmt.id) {
  3557. ooAppendCapToCapPrefs(call, ooh323_convertAsteriskCapToH323Cap(&tmpfmt));
  3558. ast_codec_pref_index(prefs, ++i, &tmpfmt);
  3559. }
  3560. return 0;
  3561. }
  3562. int ooh323_convertAsteriskCapToH323Cap(struct ast_format *format)
  3563. {
  3564. switch (format->id) {
  3565. case AST_FORMAT_ULAW:
  3566. return OO_G711ULAW64K;
  3567. case AST_FORMAT_ALAW:
  3568. return OO_G711ALAW64K;
  3569. case AST_FORMAT_GSM:
  3570. return OO_GSMFULLRATE;
  3571. #ifdef AST_FORMAT_AMRNB
  3572. case AST_FORMAT_AMRNB:
  3573. return OO_AMRNB;
  3574. #endif
  3575. #ifdef AST_FORMAT_SPEEX
  3576. case AST_FORMAT_SPEEX:
  3577. return OO_SPEEX;
  3578. #endif
  3579. case AST_FORMAT_G729A:
  3580. return OO_G729A;
  3581. case AST_FORMAT_G726:
  3582. return OO_G726;
  3583. case AST_FORMAT_G726_AAL2:
  3584. return OO_G726AAL2;
  3585. case AST_FORMAT_G723_1:
  3586. return OO_G7231;
  3587. case AST_FORMAT_H263:
  3588. return OO_H263VIDEO;
  3589. default:
  3590. ast_log(LOG_NOTICE, "Don't know how to deal with mode %s\n", ast_getformatname(format));
  3591. return -1;
  3592. }
  3593. }
  3594. static int ooh323_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *rtp,
  3595. struct ast_rtp_instance *vrtp, struct ast_rtp_instance *trtp, const struct ast_format_cap *cap, int nat_active)
  3596. {
  3597. /* XXX Deal with Video */
  3598. struct ooh323_pvt *p;
  3599. struct ast_sockaddr tmp;
  3600. int mode;
  3601. if (gH323Debug)
  3602. ast_verbose("--- ooh323_set_peer - %s\n", chan->name);
  3603. if (!rtp) {
  3604. return 0;
  3605. }
  3606. mode = ooh323_convertAsteriskCapToH323Cap(&chan->writeformat);
  3607. p = (struct ooh323_pvt *) chan->tech_pvt;
  3608. if (!p) {
  3609. ast_log(LOG_ERROR, "No Private Structure, this is bad\n");
  3610. return -1;
  3611. }
  3612. ast_rtp_instance_get_remote_address(rtp, &tmp);
  3613. ast_rtp_instance_get_local_address(rtp, &tmp);
  3614. return 0;
  3615. /* May 20101003 */
  3616. /* What we should to do here? */
  3617. }
  3618. int configure_local_rtp(struct ooh323_pvt *p, ooCallData *call)
  3619. {
  3620. char lhost[INET6_ADDRSTRLEN], *lport=NULL;
  3621. struct ast_sockaddr tmp;
  3622. ooMediaInfo mediaInfo;
  3623. int x;
  3624. struct ast_format tmpfmt;
  3625. ast_format_clear(&tmpfmt);
  3626. if (gH323Debug)
  3627. ast_verbose("--- configure_local_rtp\n");
  3628. if (ast_parse_arg(call->localIP, PARSE_ADDR, &tmp)) {
  3629. ast_sockaddr_copy(&tmp, &bindaddr);
  3630. }
  3631. if (!(p->rtp = ast_rtp_instance_new("asterisk", sched, &tmp, NULL))) {
  3632. ast_log(LOG_WARNING, "Unable to create RTP session: %s\n",
  3633. strerror(errno));
  3634. return 0;
  3635. }
  3636. ast_rtp_instance_set_qos(p->rtp, gTOS, 0, "ooh323-rtp");
  3637. if (!(p->udptl = ast_udptl_new_with_bindaddr(sched, io, 0, &tmp))) {
  3638. ast_log(LOG_WARNING, "Unable to create UDPTL session: %s\n",
  3639. strerror(errno));
  3640. return 0;
  3641. }
  3642. ast_udptl_set_far_max_datagram(p->udptl, 144);
  3643. if (p->owner) {
  3644. while (p->owner && ast_channel_trylock(p->owner)) {
  3645. ast_debug(1,"Failed to grab lock, trying again\n");
  3646. DEADLOCK_AVOIDANCE(&p->lock);
  3647. }
  3648. if (!p->owner) {
  3649. ast_mutex_unlock(&p->lock);
  3650. ast_log(LOG_ERROR, "Channel has no owner\n");
  3651. return 0;
  3652. }
  3653. } else {
  3654. ast_log(LOG_ERROR, "Channel has no owner\n");
  3655. return 0;
  3656. }
  3657. ast_channel_set_fd(p->owner, 0, ast_rtp_instance_fd(p->rtp, 0));
  3658. ast_channel_set_fd(p->owner, 1, ast_rtp_instance_fd(p->rtp, 1));
  3659. ast_channel_set_fd(p->owner, 5, ast_udptl_fd(p->udptl));
  3660. ast_channel_unlock(p->owner);
  3661. if (p->rtp) {
  3662. ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(p->rtp), p->rtp, &p->prefs);
  3663. if (p->dtmfmode & H323_DTMF_RFC2833 && p->dtmfcodec) {
  3664. ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_DTMF, 1);
  3665. ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(p->rtp),
  3666. p->rtp, p->dtmfcodec, "audio", "telephone-event", 0);
  3667. }
  3668. if (p->dtmfmode & H323_DTMF_CISCO && p->dtmfcodec) {
  3669. ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_DTMF, 1);
  3670. ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(p->rtp),
  3671. p->rtp, p->dtmfcodec, "audio", "cisco-telephone-event", 0);
  3672. }
  3673. /* figure out our local RTP port and tell the H.323 stack about it*/
  3674. ast_rtp_instance_get_local_address(p->rtp, &tmp);
  3675. strncpy(lhost, ast_sockaddr_stringify_addr(&tmp), sizeof(lhost));
  3676. lport = ast_sockaddr_stringify_port(&tmp);
  3677. if (p->rtptimeout) {
  3678. ast_rtp_instance_set_timeout(p->rtp, p->rtptimeout);
  3679. }
  3680. ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_RTCP, 1);
  3681. }
  3682. if (p->rtdrcount) {
  3683. if (gH323Debug)
  3684. ast_verbose("Setup RTDR info: %d, %d\n", p->rtdrinterval, p->rtdrcount);
  3685. call->rtdrInterval = p->rtdrinterval;
  3686. call->rtdrCount = p->rtdrcount;
  3687. }
  3688. ast_copy_string(mediaInfo.lMediaIP, lhost, sizeof(mediaInfo.lMediaIP));
  3689. mediaInfo.lMediaPort = atoi(lport);
  3690. mediaInfo.lMediaCntrlPort = mediaInfo.lMediaPort +1;
  3691. for (x = 0; ast_codec_pref_index(&p->prefs, x, &tmpfmt); x++) {
  3692. strcpy(mediaInfo.dir, "transmit");
  3693. mediaInfo.cap = ooh323_convertAsteriskCapToH323Cap(&tmpfmt);
  3694. ooAddMediaInfo(call, mediaInfo);
  3695. strcpy(mediaInfo.dir, "receive");
  3696. ooAddMediaInfo(call, mediaInfo);
  3697. if (mediaInfo.cap == OO_G729A) {
  3698. strcpy(mediaInfo.dir, "transmit");
  3699. mediaInfo.cap = OO_G729;
  3700. ooAddMediaInfo(call, mediaInfo);
  3701. strcpy(mediaInfo.dir, "receive");
  3702. ooAddMediaInfo(call, mediaInfo);
  3703. strcpy(mediaInfo.dir, "transmit");
  3704. mediaInfo.cap = OO_G729B;
  3705. ooAddMediaInfo(call, mediaInfo);
  3706. strcpy(mediaInfo.dir, "receive");
  3707. ooAddMediaInfo(call, mediaInfo);
  3708. }
  3709. }
  3710. if (p->udptl) {
  3711. ast_udptl_get_us(p->udptl, &tmp);
  3712. strncpy(lhost, ast_sockaddr_stringify_addr(&tmp), sizeof(lhost));
  3713. lport = ast_sockaddr_stringify_port(&tmp);
  3714. ast_copy_string(mediaInfo.lMediaIP, lhost, sizeof(mediaInfo.lMediaIP));
  3715. mediaInfo.lMediaPort = atoi(lport);
  3716. mediaInfo.lMediaCntrlPort = mediaInfo.lMediaPort +1;
  3717. mediaInfo.cap = OO_T38;
  3718. strcpy(mediaInfo.dir, "transmit");
  3719. ooAddMediaInfo(call, mediaInfo);
  3720. strcpy(mediaInfo.dir, "receive");
  3721. ooAddMediaInfo(call, mediaInfo);
  3722. }
  3723. if (gH323Debug)
  3724. ast_verbose("+++ configure_local_rtp\n");
  3725. return 1;
  3726. }
  3727. void setup_rtp_connection(ooCallData *call, const char *remoteIp,
  3728. int remotePort)
  3729. {
  3730. struct ooh323_pvt *p = NULL;
  3731. struct ast_sockaddr tmp;
  3732. if (gH323Debug)
  3733. ast_verbose("--- setup_rtp_connection %s:%d\n", remoteIp, remotePort);
  3734. /* Find the call or allocate a private structure if call not found */
  3735. p = find_call(call);
  3736. if (!p) {
  3737. ast_log(LOG_ERROR, "Something is wrong: rtp\n");
  3738. return;
  3739. }
  3740. ast_parse_arg(remoteIp, PARSE_ADDR, &tmp);
  3741. ast_sockaddr_set_port(&tmp, remotePort);
  3742. ast_rtp_instance_set_remote_address(p->rtp, &tmp);
  3743. if (p->writeformat.id == AST_FORMAT_G726_AAL2)
  3744. ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(p->rtp), p->rtp, 2,
  3745. "audio", "G726-32", AST_RTP_OPT_G726_NONSTANDARD);
  3746. if(gH323Debug)
  3747. ast_verbose("+++ setup_rtp_connection\n");
  3748. return;
  3749. }
  3750. void close_rtp_connection(ooCallData *call)
  3751. {
  3752. struct ooh323_pvt *p = NULL;
  3753. if(gH323Debug)
  3754. ast_verbose("--- close_rtp_connection\n");
  3755. p = find_call(call);
  3756. if (!p) {
  3757. ast_log(LOG_ERROR, "Couldn't find matching call to close rtp "
  3758. "connection\n");
  3759. return;
  3760. }
  3761. ast_mutex_lock(&p->lock);
  3762. if (p->rtp) {
  3763. ast_rtp_instance_stop(p->rtp);
  3764. }
  3765. ast_mutex_unlock(&p->lock);
  3766. if(gH323Debug)
  3767. ast_verbose("+++ close_rtp_connection\n");
  3768. return;
  3769. }
  3770. /*
  3771. udptl handling functions
  3772. */
  3773. static struct ast_udptl *ooh323_get_udptl_peer(struct ast_channel *chan)
  3774. {
  3775. struct ooh323_pvt *p;
  3776. struct ast_udptl *udptl = NULL;
  3777. p = chan->tech_pvt;
  3778. if (!p)
  3779. return NULL;
  3780. ast_mutex_lock(&p->lock);
  3781. if (p->udptl)
  3782. udptl = p->udptl;
  3783. ast_mutex_unlock(&p->lock);
  3784. return udptl;
  3785. }
  3786. static int ooh323_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl)
  3787. {
  3788. struct ooh323_pvt *p;
  3789. p = chan->tech_pvt;
  3790. if (!p)
  3791. return -1;
  3792. ast_mutex_lock(&p->lock);
  3793. if (udptl) {
  3794. ast_udptl_get_peer(udptl, &p->udptlredirip);
  3795. } else
  3796. memset(&p->udptlredirip, 0, sizeof(p->udptlredirip));
  3797. ast_mutex_unlock(&p->lock);
  3798. /* free(callToken); */
  3799. return 0;
  3800. }
  3801. void setup_udptl_connection(ooCallData *call, const char *remoteIp,
  3802. int remotePort)
  3803. {
  3804. struct ooh323_pvt *p = NULL;
  3805. struct ast_sockaddr them;
  3806. if (gH323Debug)
  3807. ast_verbose("--- setup_udptl_connection\n");
  3808. /* Find the call or allocate a private structure if call not found */
  3809. p = find_call(call);
  3810. if (!p) {
  3811. ast_log(LOG_ERROR, "Something is wrong: rtp\n");
  3812. return;
  3813. }
  3814. ast_mutex_lock(&p->lock);
  3815. if (p->owner) {
  3816. while (p->owner && ast_channel_trylock(p->owner)) {
  3817. ast_debug(1, "Failed to grab lock, trying again\n");
  3818. DEADLOCK_AVOIDANCE(&p->lock);
  3819. }
  3820. if (!p->owner) {
  3821. ast_mutex_unlock(&p->lock);
  3822. ast_log(LOG_ERROR, "Channel has no owner\n");
  3823. return;
  3824. }
  3825. } else {
  3826. ast_mutex_unlock(&p->lock);
  3827. ast_log(LOG_ERROR, "Channel has no owner\n");
  3828. return;
  3829. }
  3830. ast_parse_arg(remoteIp, PARSE_ADDR, &them);
  3831. ast_sockaddr_set_port(&them, remotePort);
  3832. ast_udptl_set_peer(p->udptl, &them);
  3833. ast_udptl_set_tag(p->udptl, "%s", p->owner->name);
  3834. p->t38_tx_enable = 1;
  3835. p->lastTxT38 = time(NULL);
  3836. if (p->t38support == T38_ENABLED) {
  3837. struct ast_control_t38_parameters parameters = { .request_response = 0 };
  3838. parameters.request_response = AST_T38_NEGOTIATED;
  3839. parameters.max_ifp = ast_udptl_get_far_max_ifp(p->udptl);
  3840. parameters.rate = AST_T38_RATE_14400;
  3841. ast_queue_control_data(p->owner, AST_CONTROL_T38_PARAMETERS, &parameters, sizeof(parameters));
  3842. }
  3843. if (gH323Debug)
  3844. ast_debug(1, "Receiving UDPTL %s:%s\n", ast_sockaddr_stringify_host(&them),
  3845. ast_sockaddr_stringify_port(&them));
  3846. ast_channel_unlock(p->owner);
  3847. ast_mutex_unlock(&p->lock);
  3848. if(gH323Debug)
  3849. ast_verbose("+++ setup_udptl_connection\n");
  3850. return;
  3851. }
  3852. void close_udptl_connection(ooCallData *call)
  3853. {
  3854. struct ooh323_pvt *p = NULL;
  3855. if(gH323Debug)
  3856. ast_verbose("--- close_udptl_connection\n");
  3857. p = find_call(call);
  3858. if (!p) {
  3859. ast_log(LOG_ERROR, "Couldn't find matching call to close udptl "
  3860. "connection\n");
  3861. return;
  3862. }
  3863. ast_mutex_lock(&p->lock);
  3864. if (p->owner) {
  3865. while (p->owner && ast_channel_trylock(p->owner)) {
  3866. ast_debug(1, "Failed to grab lock, trying again\n");
  3867. DEADLOCK_AVOIDANCE(&p->lock);
  3868. }
  3869. if (!p->owner) {
  3870. ast_mutex_unlock(&p->lock);
  3871. ast_log(LOG_ERROR, "Channel has no owner\n");
  3872. return;
  3873. }
  3874. } else {
  3875. ast_mutex_unlock(&p->lock);
  3876. ast_log(LOG_ERROR, "Channel has no owner\n");
  3877. return;
  3878. }
  3879. p->t38_tx_enable = 0;
  3880. if (p->t38support == T38_ENABLED) {
  3881. struct ast_control_t38_parameters parameters = { .request_response = 0 };
  3882. parameters.request_response = AST_T38_TERMINATED;
  3883. ast_queue_control_data(p->owner, AST_CONTROL_T38_PARAMETERS, &parameters, sizeof(parameters));
  3884. }
  3885. ast_channel_unlock(p->owner);
  3886. ast_mutex_unlock(&p->lock);
  3887. if(gH323Debug)
  3888. ast_verbose("+++ close_udptl_connection\n");
  3889. return;
  3890. }
  3891. /* end of udptl handling */
  3892. int update_our_aliases(ooCallData *call, struct ooh323_pvt *p)
  3893. {
  3894. int updated = -1;
  3895. ooAliases *psAlias = NULL;
  3896. if (!call->ourAliases)
  3897. return updated;
  3898. for (psAlias = call->ourAliases; psAlias; psAlias = psAlias->next) {
  3899. if (psAlias->type == T_H225AliasAddress_h323_ID) {
  3900. ast_copy_string(p->callee_h323id, psAlias->value, sizeof(p->callee_h323id));
  3901. updated = 1;
  3902. }
  3903. if (psAlias->type == T_H225AliasAddress_dialedDigits) {
  3904. ast_copy_string(p->callee_dialedDigits, psAlias->value,
  3905. sizeof(p->callee_dialedDigits));
  3906. updated = 1;
  3907. }
  3908. if (psAlias->type == T_H225AliasAddress_url_ID) {
  3909. ast_copy_string(p->callee_url, psAlias->value, sizeof(p->callee_url));
  3910. updated = 1;
  3911. }
  3912. if (psAlias->type == T_H225AliasAddress_email_ID) {
  3913. ast_copy_string(p->callee_email, psAlias->value, sizeof(p->callee_email));
  3914. updated = 1;
  3915. }
  3916. }
  3917. return updated;
  3918. }
  3919. struct ast_frame *ooh323_rtp_read(struct ast_channel *ast, struct ooh323_pvt *p)
  3920. {
  3921. /* Retrieve audio/etc from channel. Assumes p->lock is already held. */
  3922. struct ast_frame *f;
  3923. struct ast_frame *dfr = NULL;
  3924. static struct ast_frame null_frame = { AST_FRAME_NULL, };
  3925. switch (ast->fdno) {
  3926. case 0:
  3927. f = ast_rtp_instance_read(p->rtp, 0); /* RTP Audio */
  3928. break;
  3929. case 1:
  3930. f = ast_rtp_instance_read(p->rtp, 1); /* RTCP Control Channel */
  3931. break;
  3932. case 2:
  3933. f = ast_rtp_instance_read(p->vrtp, 0); /* RTP Video */
  3934. break;
  3935. case 3:
  3936. f = ast_rtp_instance_read(p->vrtp, 1); /* RTCP Control Channel for video */
  3937. break;
  3938. case 5:
  3939. f = ast_udptl_read(p->udptl); /* UDPTL t.38 data */
  3940. if (gH323Debug) {
  3941. ast_debug(1, "Got UDPTL %d/%d len %d for %s\n",
  3942. f->frametype, f->subclass.integer, f->datalen, ast->name);
  3943. }
  3944. break;
  3945. default:
  3946. f = &null_frame;
  3947. }
  3948. if (p->owner && !p->faxmode && (f->frametype == AST_FRAME_VOICE)) {
  3949. /* We already hold the channel lock */
  3950. if (!(ast_format_cap_iscompatible(p->owner->nativeformats, &f->subclass.format))) {
  3951. ast_debug(1, "Oooh, voice format changed to %s\n", ast_getformatname(&f->subclass.format));
  3952. ast_format_cap_set(p->owner->nativeformats, &f->subclass.format);
  3953. ast_set_read_format(p->owner, &p->owner->readformat);
  3954. ast_set_write_format(p->owner, &p->owner->writeformat);
  3955. }
  3956. if (((p->dtmfmode & H323_DTMF_INBAND) || (p->faxdetect & FAXDETECT_CNG)) && p->vad &&
  3957. (f->subclass.format.id == AST_FORMAT_SLINEAR || f->subclass.format.id == AST_FORMAT_ALAW ||
  3958. f->subclass.format.id == AST_FORMAT_ULAW)) {
  3959. dfr = ast_frdup(f);
  3960. dfr = ast_dsp_process(p->owner, p->vad, dfr);
  3961. }
  3962. } else {
  3963. return f;
  3964. }
  3965. /* process INBAND DTMF*/
  3966. if (dfr && (dfr->frametype == AST_FRAME_DTMF) && ((dfr->subclass.integer == 'f') || (dfr->subclass.integer == 'e'))) {
  3967. ast_debug(1, "* Detected FAX Tone %s\n", (dfr->subclass.integer == 'e') ? "CED" : "CNG");
  3968. /* Switch to T.38 ON CED*/
  3969. if (!p->faxmode && !p->chmodepend && (dfr->subclass.integer == 'e') && (p->t38support != T38_DISABLED)) {
  3970. if (gH323Debug)
  3971. ast_verbose("request to change %s to t.38 because fax ced\n", p->callToken);
  3972. p->chmodepend = 1;
  3973. p->faxdetected = 1;
  3974. ooRequestChangeMode(p->callToken, 1);
  3975. } else if ((dfr->subclass.integer == 'f') && !p->faxdetected) {
  3976. const char *target_context = S_OR(p->owner->macrocontext, p->owner->context);
  3977. if ((strcmp(p->owner->exten, "fax")) &&
  3978. (ast_exists_extension(p->owner, target_context, "fax", 1,
  3979. S_COR(p->owner->caller.id.number.valid, p->owner->caller.id.number.str, NULL)))) {
  3980. ast_verb(2, "Redirecting '%s' to fax extension due to CNG detection\n", p->owner->name);
  3981. pbx_builtin_setvar_helper(p->owner, "FAXEXTEN", p->owner->exten);
  3982. if (ast_async_goto(p->owner, target_context, "fax", 1)) {
  3983. ast_log(LOG_NOTICE, "Failed to async goto '%s' into fax of '%s'\n", p->owner->name,target_context);
  3984. }
  3985. p->faxdetected = 1;
  3986. if (dfr) {
  3987. ast_frfree(dfr);
  3988. }
  3989. return &ast_null_frame;
  3990. }
  3991. }
  3992. } else if (dfr && dfr->frametype == AST_FRAME_DTMF) {
  3993. ast_debug(1, "* Detected inband DTMF '%c'\n", f->subclass.integer);
  3994. ast_frfree(f);
  3995. return dfr;
  3996. }
  3997. if (dfr) {
  3998. ast_frfree(dfr);
  3999. }
  4000. return f;
  4001. }
  4002. void onModeChanged(ooCallData *call, int t38mode) {
  4003. struct ooh323_pvt *p;
  4004. p = find_call(call);
  4005. if (!p) {
  4006. ast_log(LOG_ERROR, "No matching call found for %s\n", call->callToken);
  4007. return;
  4008. }
  4009. ast_mutex_lock(&p->lock);
  4010. if (gH323Debug)
  4011. ast_debug(1, "change mode to %d for %s\n", t38mode, call->callToken);
  4012. if (t38mode == p->faxmode) {
  4013. if (gH323Debug)
  4014. ast_debug(1, "mode for %s is already %d\n", call->callToken,
  4015. t38mode);
  4016. p->chmodepend = 0;
  4017. ast_mutex_unlock(&p->lock);
  4018. return;
  4019. }
  4020. if (p->owner) {
  4021. while (p->owner && ast_channel_trylock(p->owner)) {
  4022. ast_debug(1,"Failed to grab lock, trying again\n");
  4023. DEADLOCK_AVOIDANCE(&p->lock);
  4024. }
  4025. if (!p->owner) {
  4026. p->chmodepend = 0;
  4027. ast_mutex_unlock(&p->lock);
  4028. ast_log(LOG_ERROR, "Channel has no owner\n");
  4029. return;
  4030. }
  4031. } else {
  4032. p->chmodepend = 0;
  4033. ast_mutex_unlock(&p->lock);
  4034. ast_log(LOG_ERROR, "Channel has no owner\n");
  4035. return;
  4036. }
  4037. if (t38mode) {
  4038. if (p->t38support == T38_ENABLED) {
  4039. struct ast_control_t38_parameters parameters = { .request_response = 0 };
  4040. if ((p->faxdetect & FAXDETECT_T38) && !p->faxdetected) {
  4041. const char *target_context;
  4042. ast_debug(1, "* Detected T.38 Request\n");
  4043. target_context = S_OR(p->owner->macrocontext, p->owner->context);
  4044. if ((strcmp(p->owner->exten, "fax")) &&
  4045. (ast_exists_extension(p->owner, target_context, "fax", 1,
  4046. S_COR(p->owner->caller.id.number.valid, p->owner->caller.id.number.str, NULL)))) {
  4047. ast_verb(2, "Redirecting '%s' to fax extension due to CNG detection\n", p->owner->name);
  4048. pbx_builtin_setvar_helper(p->owner, "FAXEXTEN", p->owner->exten);
  4049. if (ast_async_goto(p->owner, target_context, "fax", 1)) {
  4050. ast_log(LOG_NOTICE, "Failed to async goto '%s' into fax of '%s'\n", p->owner->name,target_context);
  4051. }
  4052. }
  4053. p->faxdetected = 1;
  4054. }
  4055. /* AST_T38_CONTROL mode */
  4056. parameters.request_response = AST_T38_REQUEST_NEGOTIATE;
  4057. if (call->T38FarMaxDatagram) {
  4058. ast_udptl_set_far_max_datagram(p->udptl, call->T38FarMaxDatagram);
  4059. } else {
  4060. ast_udptl_set_far_max_datagram(p->udptl, 144);
  4061. }
  4062. if (call->T38Version) {
  4063. parameters.version = call->T38Version;
  4064. }
  4065. parameters.max_ifp = ast_udptl_get_far_max_ifp(p->udptl);
  4066. parameters.rate = AST_T38_RATE_14400;
  4067. ast_queue_control_data(p->owner, AST_CONTROL_T38_PARAMETERS,
  4068. &parameters, sizeof(parameters));
  4069. p->faxmode = 1;
  4070. }
  4071. } else {
  4072. if (p->t38support == T38_ENABLED) {
  4073. struct ast_control_t38_parameters parameters = { .request_response = 0 };
  4074. parameters.request_response = AST_T38_REQUEST_TERMINATE;
  4075. parameters.max_ifp = ast_udptl_get_far_max_ifp(p->udptl);
  4076. parameters.rate = AST_T38_RATE_14400;
  4077. ast_queue_control_data(p->owner, AST_CONTROL_T38_PARAMETERS,
  4078. &parameters, sizeof(parameters));
  4079. }
  4080. p->faxmode = 0;
  4081. p->faxdetected = 0;
  4082. p->t38_init = 0;
  4083. }
  4084. p->chmodepend = 0;
  4085. ast_channel_unlock(p->owner);
  4086. ast_mutex_unlock(&p->lock);
  4087. }
  4088. int ooh323_convert_hangupcause_asteriskToH323(int cause)
  4089. {
  4090. switch (cause) {
  4091. case AST_CAUSE_CALL_REJECTED:
  4092. return OO_REASON_REMOTE_REJECTED;
  4093. case AST_CAUSE_UNALLOCATED:
  4094. return OO_REASON_NOUSER;
  4095. case AST_CAUSE_BUSY:
  4096. return OO_REASON_REMOTE_BUSY;
  4097. case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL:
  4098. return OO_REASON_NOCOMMON_CAPABILITIES;
  4099. case AST_CAUSE_CONGESTION:
  4100. return OO_REASON_REMOTE_BUSY;
  4101. case AST_CAUSE_NO_ANSWER:
  4102. return OO_REASON_REMOTE_NOANSWER;
  4103. case AST_CAUSE_NORMAL:
  4104. return OO_REASON_REMOTE_CLEARED;
  4105. case AST_CAUSE_FAILURE:
  4106. default:
  4107. return OO_REASON_UNKNOWN;
  4108. }
  4109. return 0;
  4110. }
  4111. int ooh323_convert_hangupcause_h323ToAsterisk(int cause)
  4112. {
  4113. switch (cause) {
  4114. case OO_REASON_REMOTE_REJECTED:
  4115. return AST_CAUSE_CALL_REJECTED;
  4116. case OO_REASON_NOUSER:
  4117. return AST_CAUSE_UNALLOCATED;
  4118. case OO_REASON_REMOTE_BUSY:
  4119. case OO_REASON_LOCAL_BUSY:
  4120. return AST_CAUSE_BUSY;
  4121. case OO_REASON_NOCOMMON_CAPABILITIES: /* No codecs approved */
  4122. return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
  4123. case OO_REASON_REMOTE_CONGESTED:
  4124. case OO_REASON_LOCAL_CONGESTED:
  4125. return AST_CAUSE_CONGESTION;
  4126. case OO_REASON_REMOTE_NOANSWER:
  4127. return AST_CAUSE_NO_ANSWER;
  4128. case OO_REASON_UNKNOWN:
  4129. case OO_REASON_INVALIDMESSAGE:
  4130. case OO_REASON_TRANSPORTFAILURE:
  4131. return AST_CAUSE_FAILURE;
  4132. case OO_REASON_REMOTE_CLEARED:
  4133. return AST_CAUSE_NORMAL;
  4134. default:
  4135. return AST_CAUSE_NORMAL;
  4136. }
  4137. /* Never reached */
  4138. return 0;
  4139. }
  4140. #if 0
  4141. void ast_ooh323c_exit()
  4142. {
  4143. ooGkClientDestroy();
  4144. }
  4145. #endif
  4146. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Objective Systems H323 Channel");