chan_ooh323.c 134 KB

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