chan_ooh323.c 146 KB

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