chan_ooh323.c 144 KB

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