chan_ooh323.c 146 KB

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