chan_mgcp.c 148 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2006, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Implementation of Media Gateway Control Protocol
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \ingroup channel_drivers
  25. */
  26. /*! \li \ref chan_mgcp.c uses the configuration file \ref mgcp.conf
  27. * \addtogroup configuration_file
  28. */
  29. /*! \page mgcp.conf mgcp.conf
  30. * \verbinclude mgcp.conf.sample
  31. */
  32. /*** MODULEINFO
  33. <use type="module">res_pktccops</use>
  34. <support_level>extended</support_level>
  35. ***/
  36. #include "asterisk.h"
  37. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  38. #include <sys/socket.h>
  39. #include <sys/ioctl.h>
  40. #include <net/if.h>
  41. #include <fcntl.h>
  42. #include <netdb.h>
  43. #include <sys/signal.h>
  44. #include <signal.h>
  45. #include <netinet/in.h>
  46. #include <netinet/in_systm.h>
  47. #include <netinet/ip.h>
  48. #include <arpa/inet.h>
  49. #include <ctype.h>
  50. #include "asterisk/lock.h"
  51. #include "asterisk/channel.h"
  52. #include "asterisk/config.h"
  53. #include "asterisk/module.h"
  54. #include "asterisk/pickup.h"
  55. #include "asterisk/pbx.h"
  56. #include "asterisk/sched.h"
  57. #include "asterisk/io.h"
  58. #include "asterisk/rtp_engine.h"
  59. #include "asterisk/acl.h"
  60. #include "asterisk/callerid.h"
  61. #include "asterisk/cli.h"
  62. #include "asterisk/say.h"
  63. #include "asterisk/astdb.h"
  64. #include "asterisk/features.h"
  65. #include "asterisk/app.h"
  66. #include "asterisk/musiconhold.h"
  67. #include "asterisk/utils.h"
  68. #include "asterisk/netsock2.h"
  69. #include "asterisk/causes.h"
  70. #include "asterisk/dsp.h"
  71. #include "asterisk/devicestate.h"
  72. #include "asterisk/stringfields.h"
  73. #include "asterisk/abstract_jb.h"
  74. #include "asterisk/chanvars.h"
  75. #include "asterisk/pktccops.h"
  76. #include "asterisk/stasis.h"
  77. #include "asterisk/bridge.h"
  78. #include "asterisk/features_config.h"
  79. #include "asterisk/parking.h"
  80. #include "asterisk/stasis_channels.h"
  81. #include "asterisk/format_cache.h"
  82. /*
  83. * Define to work around buggy dlink MGCP phone firmware which
  84. * appears not to know that "rt" is part of the "G" package.
  85. */
  86. /* #define DLINK_BUGGY_FIRMWARE */
  87. #define MGCPDUMPER
  88. #define DEFAULT_EXPIRY 120
  89. #define MAX_EXPIRY 3600
  90. #define DIRECTMEDIA 1
  91. #ifndef INADDR_NONE
  92. #define INADDR_NONE (in_addr_t)(-1)
  93. #endif
  94. /*! Global jitterbuffer configuration - by default, jb is disabled
  95. * \note Values shown here match the defaults shown in mgcp.conf.sample */
  96. static struct ast_jb_conf default_jbconf =
  97. {
  98. .flags = 0,
  99. .max_size = 200,
  100. .resync_threshold = 1000,
  101. .impl = "fixed",
  102. .target_extra = 40,
  103. };
  104. static struct ast_jb_conf global_jbconf;
  105. static const char tdesc[] = "Media Gateway Control Protocol (MGCP)";
  106. static const char config[] = "mgcp.conf";
  107. #define MGCP_DTMF_RFC2833 (1 << 0)
  108. #define MGCP_DTMF_INBAND (1 << 1)
  109. #define MGCP_DTMF_HYBRID (1 << 2)
  110. #define DEFAULT_MGCP_GW_PORT 2427 /*!< From RFC 2705 */
  111. #define DEFAULT_MGCP_CA_PORT 2727 /*!< From RFC 2705 */
  112. #define MGCP_MAX_PACKET 1500 /*!< Also from RFC 2543, should sub headers tho */
  113. #define DEFAULT_RETRANS 1000 /*!< How frequently to retransmit */
  114. #define MAX_RETRANS 5 /*!< Try only 5 times for retransmissions */
  115. /*! MGCP rtp stream modes { */
  116. #define MGCP_CX_SENDONLY 0
  117. #define MGCP_CX_RECVONLY 1
  118. #define MGCP_CX_SENDRECV 2
  119. #define MGCP_CX_CONF 3
  120. #define MGCP_CX_CONFERENCE 3
  121. #define MGCP_CX_MUTE 4
  122. #define MGCP_CX_INACTIVE 4
  123. /*! } */
  124. static const char * const mgcp_cxmodes[] = {
  125. "sendonly",
  126. "recvonly",
  127. "sendrecv",
  128. "confrnce",
  129. "inactive"
  130. };
  131. enum {
  132. MGCP_CMD_EPCF,
  133. MGCP_CMD_CRCX,
  134. MGCP_CMD_MDCX,
  135. MGCP_CMD_DLCX,
  136. MGCP_CMD_RQNT,
  137. MGCP_CMD_NTFY,
  138. MGCP_CMD_AUEP,
  139. MGCP_CMD_AUCX,
  140. MGCP_CMD_RSIP
  141. };
  142. static char context[AST_MAX_EXTENSION] = "default";
  143. static char language[MAX_LANGUAGE] = "";
  144. static char musicclass[MAX_MUSICCLASS] = "";
  145. static char parkinglot[AST_MAX_CONTEXT];
  146. static char cid_num[AST_MAX_EXTENSION] = "";
  147. static char cid_name[AST_MAX_EXTENSION] = "";
  148. static int dtmfmode = 0;
  149. static int nat = 0;
  150. static int ncs = 0;
  151. static int pktcgatealloc = 0;
  152. static int hangupongateremove = 0;
  153. static ast_group_t cur_callergroup = 0;
  154. static ast_group_t cur_pickupgroup = 0;
  155. static struct {
  156. unsigned int tos;
  157. unsigned int tos_audio;
  158. unsigned int cos;
  159. unsigned int cos_audio;
  160. } qos = { 0, 0, 0, 0 };
  161. static int immediate = 0;
  162. static int callwaiting = 0;
  163. static int callreturn = 0;
  164. static int slowsequence = 0;
  165. static int threewaycalling = 0;
  166. /*! This is for flashhook transfers */
  167. static int transfer = 0;
  168. static int cancallforward = 0;
  169. static int singlepath = 0;
  170. static int directmedia = DIRECTMEDIA;
  171. static char accountcode[AST_MAX_ACCOUNT_CODE] = "";
  172. static char mailbox[AST_MAX_MAILBOX_UNIQUEID];
  173. static int amaflags = 0;
  174. static int adsi = 0;
  175. static unsigned int oseq_global = 0;
  176. AST_MUTEX_DEFINE_STATIC(oseq_lock);
  177. /*! Wait up to 16 seconds for first digit (FXO logic) */
  178. static int firstdigittimeout = 16000;
  179. /*! How long to wait for following digits (FXO logic) */
  180. static int gendigittimeout = 8000;
  181. /*! How long to wait for an extra digit, if there is an ambiguous match */
  182. static int matchdigittimeout = 3000;
  183. /*! Protect the monitoring thread, so only one process can kill or start it, and not
  184. when it's doing something critical. */
  185. AST_MUTEX_DEFINE_STATIC(netlock);
  186. AST_MUTEX_DEFINE_STATIC(monlock);
  187. /*! This is the thread for the monitor which checks for input on the channels
  188. * which are not currently in use.
  189. */
  190. static pthread_t monitor_thread = AST_PTHREADT_NULL;
  191. static int restart_monitor(void);
  192. static struct ast_format_cap *global_capability;
  193. static int nonCodecCapability = AST_RTP_DTMF;
  194. static char ourhost[MAXHOSTNAMELEN];
  195. static struct in_addr __ourip;
  196. static int ourport;
  197. static int mgcpdebug = 0;
  198. static struct ast_sched_context *sched;
  199. static struct io_context *io;
  200. /*! The private structures of the mgcp channels are linked for
  201. * selecting outgoing channels
  202. */
  203. #define MGCP_MAX_HEADERS 64
  204. #define MGCP_MAX_LINES 64
  205. struct mgcp_request {
  206. int len;
  207. char *verb;
  208. char *identifier;
  209. char *endpoint;
  210. char *version;
  211. int headers; /*!< MGCP Headers */
  212. char *header[MGCP_MAX_HEADERS];
  213. int lines; /*!< SDP Content */
  214. char *line[MGCP_MAX_LINES];
  215. char data[MGCP_MAX_PACKET];
  216. int cmd; /*!< int version of verb = command */
  217. unsigned int trid; /*!< int version of identifier = transaction id */
  218. struct mgcp_request *next; /*!< next in the queue */
  219. };
  220. /*! \brief mgcp_message: MGCP message for queuing up */
  221. struct mgcp_message {
  222. struct mgcp_endpoint *owner_ep;
  223. struct mgcp_subchannel *owner_sub;
  224. int retrans;
  225. unsigned long expire;
  226. unsigned int seqno;
  227. int len;
  228. struct mgcp_message *next;
  229. char buf[0];
  230. };
  231. #define RESPONSE_TIMEOUT 30 /*!< in seconds */
  232. struct mgcp_response {
  233. time_t whensent;
  234. int len;
  235. int seqno;
  236. struct mgcp_response *next;
  237. char buf[0];
  238. };
  239. #define MAX_SUBS 2
  240. #define SUB_REAL 0
  241. #define SUB_ALT 1
  242. struct mgcp_subchannel {
  243. /*! subchannel magic string.
  244. Needed to prove that any subchannel pointer passed by asterisk
  245. really points to a valid subchannel memory area.
  246. Ugly.. But serves the purpose for the time being.
  247. */
  248. #define MGCP_SUBCHANNEL_MAGIC "!978!"
  249. char magic[6];
  250. ast_mutex_t lock;
  251. int id;
  252. struct ast_channel *owner;
  253. struct mgcp_endpoint *parent;
  254. struct ast_rtp_instance *rtp;
  255. struct sockaddr_in tmpdest;
  256. char txident[80]; /*! \todo FIXME txident is replaced by rqnt_ident in endpoint.
  257. This should be obsoleted */
  258. char cxident[80];
  259. char callid[80];
  260. int cxmode;
  261. struct mgcp_request *cx_queue; /*!< pending CX commands */
  262. ast_mutex_t cx_queue_lock; /*!< CX queue lock */
  263. int nat;
  264. int iseq; /*!< Not used? RTP? */
  265. int outgoing;
  266. int alreadygone;
  267. int sdpsent;
  268. struct cops_gate *gate;
  269. struct mgcp_subchannel *next; /*!< for out circular linked list */
  270. };
  271. #define MGCP_ONHOOK 1
  272. #define MGCP_OFFHOOK 2
  273. #define TYPE_TRUNK 1
  274. #define TYPE_LINE 2
  275. struct mgcp_endpoint {
  276. ast_mutex_t lock;
  277. char name[80];
  278. struct mgcp_subchannel *sub; /*!< Pointer to our current connection, channel and stuff */
  279. char accountcode[AST_MAX_ACCOUNT_CODE];
  280. char exten[AST_MAX_EXTENSION]; /*!< Extention where to start */
  281. char context[AST_MAX_EXTENSION];
  282. char language[MAX_LANGUAGE];
  283. char cid_num[AST_MAX_EXTENSION]; /*!< Caller*ID number */
  284. char cid_name[AST_MAX_EXTENSION]; /*!< Caller*ID name */
  285. char lastcallerid[AST_MAX_EXTENSION]; /*!< Last Caller*ID */
  286. char dtmf_buf[AST_MAX_EXTENSION]; /*!< place to collect digits be */
  287. char call_forward[AST_MAX_EXTENSION]; /*!< Last Caller*ID */
  288. char musicclass[MAX_MUSICCLASS];
  289. char curtone[80]; /*!< Current tone */
  290. char mailbox[AST_MAX_EXTENSION];
  291. char parkinglot[AST_MAX_CONTEXT]; /*!< Parkinglot */
  292. struct stasis_subscription *mwi_event_sub;
  293. ast_group_t callgroup;
  294. ast_group_t pickupgroup;
  295. int callwaiting;
  296. int hascallwaiting;
  297. int transfer;
  298. int threewaycalling;
  299. int singlepath;
  300. int cancallforward;
  301. int directmedia;
  302. int callreturn;
  303. int dnd; /* How does this affect callwait? Do we just deny a mgcp_request if we're dnd? */
  304. int hascallerid;
  305. int hidecallerid;
  306. int dtmfmode;
  307. int amaflags;
  308. int ncs;
  309. int pktcgatealloc;
  310. int hangupongateremove;
  311. int type;
  312. int slowsequence; /*!< MS: Sequence the endpoint as a whole */
  313. int group;
  314. int iseq; /*!< Not used? */
  315. int lastout; /*!< tracking this on the subchannels. Is it needed here? */
  316. int needdestroy; /*!< Not used? */
  317. struct ast_format_cap *cap;
  318. int nonCodecCapability;
  319. int onhooktime;
  320. int msgstate; /*!< voicemail message state */
  321. int immediate;
  322. int hookstate;
  323. int adsi;
  324. char rqnt_ident[80]; /*!< request identifier */
  325. struct mgcp_request *rqnt_queue; /*!< pending RQNT commands */
  326. ast_mutex_t rqnt_queue_lock;
  327. struct mgcp_request *cmd_queue; /*!< pending commands other than RQNT */
  328. ast_mutex_t cmd_queue_lock;
  329. int delme; /*!< needed for reload */
  330. int needaudit; /*!< needed for reload */
  331. struct ast_dsp *dsp; /*!< XXX Should there be a dsp/subchannel? XXX */
  332. /* owner is tracked on the subchannels, and the *sub indicates whos in charge */
  333. /* struct ast_channel *owner; */
  334. /* struct ast_rtp *rtp; */
  335. /* struct sockaddr_in tmpdest; */
  336. /* message go the the endpoint and not the channel so they stay here */
  337. struct ast_variable *chanvars; /*!< Variables to set for channel created by user */
  338. struct mgcp_endpoint *next;
  339. struct mgcp_gateway *parent;
  340. };
  341. static struct mgcp_gateway {
  342. /* A gateway containing one or more endpoints */
  343. char name[80];
  344. int isnamedottedip; /*!< is the name FQDN or dotted ip */
  345. struct sockaddr_in addr;
  346. struct sockaddr_in defaddr;
  347. struct in_addr ourip;
  348. int dynamic;
  349. int expire; /*!< XXX Should we ever expire dynamic registrations? XXX */
  350. struct mgcp_endpoint *endpoints;
  351. struct ast_ha *ha;
  352. /* obsolete
  353. time_t lastouttime;
  354. int lastout;
  355. int messagepending;
  356. */
  357. /* Wildcard endpoint name */
  358. char wcardep[30];
  359. struct mgcp_message *msgs; /*!< gw msg queue */
  360. ast_mutex_t msgs_lock; /*!< queue lock */
  361. int retransid; /*!< retrans timer id */
  362. int delme; /*!< needed for reload */
  363. int realtime;
  364. struct mgcp_response *responses;
  365. struct mgcp_gateway *next;
  366. } *gateways = NULL;
  367. AST_MUTEX_DEFINE_STATIC(mgcp_reload_lock);
  368. static int mgcp_reloading = 0;
  369. /*! \brief gatelock: mutex for gateway/endpoint lists */
  370. AST_MUTEX_DEFINE_STATIC(gatelock);
  371. static int mgcpsock = -1;
  372. static struct sockaddr_in bindaddr;
  373. static void mgcp_set_owner(struct mgcp_subchannel *sub, struct ast_channel *chan);
  374. static struct ast_frame *mgcp_read(struct ast_channel *ast);
  375. static int transmit_response(struct mgcp_subchannel *sub, char *msg, struct mgcp_request *req, char *msgrest);
  376. static int transmit_notify_request(struct mgcp_subchannel *sub, char *tone);
  377. static int transmit_modify_request(struct mgcp_subchannel *sub);
  378. static int transmit_connect(struct mgcp_subchannel *sub);
  379. static int transmit_notify_request_with_callerid(struct mgcp_subchannel *sub, char *tone, char *callernum, char *callername);
  380. static int transmit_modify_with_sdp(struct mgcp_subchannel *sub, struct ast_rtp_instance *rtp, const struct ast_format_cap *codecs);
  381. static int transmit_connection_del(struct mgcp_subchannel *sub);
  382. static int transmit_audit_endpoint(struct mgcp_endpoint *p);
  383. static void start_rtp(struct mgcp_subchannel *sub);
  384. static void handle_response(struct mgcp_endpoint *p, struct mgcp_subchannel *sub,
  385. int result, unsigned int ident, struct mgcp_request *resp);
  386. static void dump_cmd_queues(struct mgcp_endpoint *p, struct mgcp_subchannel *sub);
  387. static char *mgcp_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
  388. static int reload_config(int reload);
  389. static struct ast_channel *mgcp_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *dest, int *cause);
  390. static int mgcp_call(struct ast_channel *ast, const char *dest, int timeout);
  391. static int mgcp_hangup(struct ast_channel *ast);
  392. static int mgcp_answer(struct ast_channel *ast);
  393. static struct ast_frame *mgcp_read(struct ast_channel *ast);
  394. static int mgcp_write(struct ast_channel *ast, struct ast_frame *frame);
  395. static int mgcp_indicate(struct ast_channel *ast, int ind, const void *data, size_t datalen);
  396. static int mgcp_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
  397. static int mgcp_senddigit_begin(struct ast_channel *ast, char digit);
  398. static int mgcp_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
  399. static int mgcp_devicestate(const char *data);
  400. static void add_header_offhook(struct mgcp_subchannel *sub, struct mgcp_request *resp, char *tone);
  401. static int transmit_connect_with_sdp(struct mgcp_subchannel *sub, struct ast_rtp_instance *rtp);
  402. static struct mgcp_gateway *build_gateway(char *cat, struct ast_variable *v);
  403. static int mgcp_alloc_pktcgate(struct mgcp_subchannel *sub);
  404. static int acf_channel_read(struct ast_channel *chan, const char *funcname, char *preparse, char *buf, size_t buflen);
  405. static struct ast_variable *add_var(const char *buf, struct ast_variable *list);
  406. static struct ast_variable *copy_vars(struct ast_variable *src);
  407. static struct ast_channel_tech mgcp_tech = {
  408. .type = "MGCP",
  409. .description = tdesc,
  410. .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
  411. .requester = mgcp_request,
  412. .devicestate = mgcp_devicestate,
  413. .call = mgcp_call,
  414. .hangup = mgcp_hangup,
  415. .answer = mgcp_answer,
  416. .read = mgcp_read,
  417. .write = mgcp_write,
  418. .indicate = mgcp_indicate,
  419. .fixup = mgcp_fixup,
  420. .send_digit_begin = mgcp_senddigit_begin,
  421. .send_digit_end = mgcp_senddigit_end,
  422. .func_channel_read = acf_channel_read,
  423. };
  424. static void mwi_event_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg)
  425. {
  426. /* This module does not handle MWI in an event-based manner. However, it
  427. * subscribes to MWI for each mailbox that is configured so that the core
  428. * knows that we care about it. Then, chan_mgcp will get the MWI from the
  429. * event cache instead of checking the mailbox directly. */
  430. }
  431. static int has_voicemail(struct mgcp_endpoint *p)
  432. {
  433. int new_msgs;
  434. RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
  435. msg = stasis_cache_get(ast_mwi_state_cache(), ast_mwi_state_type(), p->mailbox);
  436. if (msg) {
  437. struct ast_mwi_state *mwi_state = stasis_message_data(msg);
  438. new_msgs = mwi_state->new_msgs;
  439. } else {
  440. new_msgs = ast_app_has_voicemail(p->mailbox, NULL);
  441. }
  442. return new_msgs;
  443. }
  444. static int unalloc_sub(struct mgcp_subchannel *sub)
  445. {
  446. struct mgcp_endpoint *p = sub->parent;
  447. if (p->sub == sub) {
  448. ast_log(LOG_WARNING, "Trying to unalloc the real channel %s@%s?!?\n", p->name, p->parent->name);
  449. return -1;
  450. }
  451. ast_debug(1, "Released sub %d of channel %s@%s\n", sub->id, p->name, p->parent->name);
  452. mgcp_set_owner(sub, NULL);
  453. if (!ast_strlen_zero(sub->cxident)) {
  454. transmit_connection_del(sub);
  455. }
  456. sub->cxident[0] = '\0';
  457. sub->callid[0] = '\0';
  458. sub->cxmode = MGCP_CX_INACTIVE;
  459. sub->outgoing = 0;
  460. sub->alreadygone = 0;
  461. memset(&sub->tmpdest, 0, sizeof(sub->tmpdest));
  462. if (sub->rtp) {
  463. ast_rtp_instance_destroy(sub->rtp);
  464. sub->rtp = NULL;
  465. }
  466. dump_cmd_queues(NULL, sub);
  467. return 0;
  468. }
  469. /* modified for new transport mechanism */
  470. static int __mgcp_xmit(struct mgcp_gateway *gw, char *data, int len)
  471. {
  472. int res;
  473. if (gw->addr.sin_addr.s_addr)
  474. res=sendto(mgcpsock, data, len, 0, (struct sockaddr *)&gw->addr, sizeof(struct sockaddr_in));
  475. else
  476. res=sendto(mgcpsock, data, len, 0, (struct sockaddr *)&gw->defaddr, sizeof(struct sockaddr_in));
  477. if (res != len) {
  478. ast_log(LOG_WARNING, "mgcp_xmit returned %d: %s\n", res, strerror(errno));
  479. }
  480. return res;
  481. }
  482. static int resend_response(struct mgcp_subchannel *sub, struct mgcp_response *resp)
  483. {
  484. struct mgcp_endpoint *p = sub->parent;
  485. int res;
  486. ast_debug(1, "Retransmitting:\n%s\n to %s:%d\n", resp->buf, ast_inet_ntoa(p->parent->addr.sin_addr), ntohs(p->parent->addr.sin_port));
  487. res = __mgcp_xmit(p->parent, resp->buf, resp->len);
  488. if (res > 0)
  489. res = 0;
  490. return res;
  491. }
  492. static int send_response(struct mgcp_subchannel *sub, struct mgcp_request *req)
  493. {
  494. struct mgcp_endpoint *p = sub->parent;
  495. int res;
  496. ast_debug(1, "Transmitting:\n%s\n to %s:%d\n", req->data, ast_inet_ntoa(p->parent->addr.sin_addr), ntohs(p->parent->addr.sin_port));
  497. res = __mgcp_xmit(p->parent, req->data, req->len);
  498. if (res > 0)
  499. res = 0;
  500. return res;
  501. }
  502. /* modified for new transport framework */
  503. static void dump_queue(struct mgcp_gateway *gw, struct mgcp_endpoint *p)
  504. {
  505. struct mgcp_message *cur, *q = NULL, *w, *prev;
  506. ast_mutex_lock(&gw->msgs_lock);
  507. for (prev = NULL, cur = gw->msgs; cur; prev = cur, cur = cur->next) {
  508. if (!p || cur->owner_ep == p) {
  509. if (prev) {
  510. prev->next = cur->next;
  511. } else {
  512. gw->msgs = cur->next;
  513. }
  514. ast_log(LOG_NOTICE, "Removing message from %s transaction %u\n",
  515. gw->name, cur->seqno);
  516. w = cur;
  517. if (q) {
  518. w->next = q;
  519. } else {
  520. w->next = NULL;
  521. }
  522. q = w;
  523. }
  524. }
  525. ast_mutex_unlock(&gw->msgs_lock);
  526. while (q) {
  527. cur = q;
  528. q = q->next;
  529. ast_free(cur);
  530. }
  531. }
  532. static void mgcp_queue_frame(struct mgcp_subchannel *sub, struct ast_frame *f)
  533. {
  534. for (;;) {
  535. if (sub->owner) {
  536. if (!ast_channel_trylock(sub->owner)) {
  537. ast_queue_frame(sub->owner, f);
  538. ast_channel_unlock(sub->owner);
  539. break;
  540. } else {
  541. DEADLOCK_AVOIDANCE(&sub->lock);
  542. }
  543. } else {
  544. break;
  545. }
  546. }
  547. }
  548. static void mgcp_queue_hangup(struct mgcp_subchannel *sub)
  549. {
  550. for (;;) {
  551. if (sub->owner) {
  552. if (!ast_channel_trylock(sub->owner)) {
  553. ast_queue_hangup(sub->owner);
  554. ast_channel_unlock(sub->owner);
  555. break;
  556. } else {
  557. DEADLOCK_AVOIDANCE(&sub->lock);
  558. }
  559. } else {
  560. break;
  561. }
  562. }
  563. }
  564. static void mgcp_queue_control(struct mgcp_subchannel *sub, int control)
  565. {
  566. struct ast_frame f = { AST_FRAME_CONTROL, { control } };
  567. return mgcp_queue_frame(sub, &f);
  568. }
  569. static int retrans_pkt(const void *data)
  570. {
  571. struct mgcp_gateway *gw = (struct mgcp_gateway *)data;
  572. struct mgcp_message *cur, *exq = NULL, *w, *prev;
  573. int res = 0;
  574. /* find out expired msgs */
  575. ast_mutex_lock(&gw->msgs_lock);
  576. for (prev = NULL, cur = gw->msgs; cur; prev = cur, cur = cur->next) {
  577. if (cur->retrans < MAX_RETRANS) {
  578. cur->retrans++;
  579. ast_debug(1, "Retransmitting #%d transaction %u on [%s]\n",
  580. cur->retrans, cur->seqno, gw->name);
  581. __mgcp_xmit(gw, cur->buf, cur->len);
  582. } else {
  583. if (prev)
  584. prev->next = cur->next;
  585. else
  586. gw->msgs = cur->next;
  587. ast_log(LOG_WARNING, "Maximum retries exceeded for transaction %u on [%s]\n",
  588. cur->seqno, gw->name);
  589. w = cur;
  590. if (exq) {
  591. w->next = exq;
  592. } else {
  593. w->next = NULL;
  594. }
  595. exq = w;
  596. }
  597. }
  598. if (!gw->msgs) {
  599. gw->retransid = -1;
  600. res = 0;
  601. } else {
  602. res = 1;
  603. }
  604. ast_mutex_unlock(&gw->msgs_lock);
  605. while (exq) {
  606. cur = exq;
  607. /* time-out transaction */
  608. handle_response(cur->owner_ep, cur->owner_sub, 406, cur->seqno, NULL);
  609. exq = exq->next;
  610. ast_free(cur);
  611. }
  612. return res;
  613. }
  614. /* modified for the new transaction mechanism */
  615. static int mgcp_postrequest(struct mgcp_endpoint *p, struct mgcp_subchannel *sub,
  616. char *data, int len, unsigned int seqno)
  617. {
  618. struct mgcp_message *msg;
  619. struct mgcp_message *cur;
  620. struct mgcp_gateway *gw;
  621. struct timeval now;
  622. if (!(msg = ast_malloc(sizeof(*msg) + len))) {
  623. return -1;
  624. }
  625. if (!(gw = ((p && p->parent) ? p->parent : NULL))) {
  626. ast_free(msg);
  627. return -1;
  628. }
  629. msg->owner_sub = sub;
  630. msg->owner_ep = p;
  631. msg->seqno = seqno;
  632. msg->next = NULL;
  633. msg->len = len;
  634. msg->retrans = 0;
  635. memcpy(msg->buf, data, msg->len);
  636. ast_mutex_lock(&gw->msgs_lock);
  637. for (cur = gw->msgs; cur && cur->next; cur = cur->next);
  638. if (cur) {
  639. cur->next = msg;
  640. } else {
  641. gw->msgs = msg;
  642. }
  643. now = ast_tvnow();
  644. msg->expire = now.tv_sec * 1000 + now.tv_usec / 1000 + DEFAULT_RETRANS;
  645. if (gw->retransid == -1)
  646. gw->retransid = ast_sched_add(sched, DEFAULT_RETRANS, retrans_pkt, (void *)gw);
  647. ast_mutex_unlock(&gw->msgs_lock);
  648. __mgcp_xmit(gw, msg->buf, msg->len);
  649. /* XXX Should schedule retransmission XXX */
  650. return 0;
  651. }
  652. /* modified for new transport */
  653. static int send_request(struct mgcp_endpoint *p, struct mgcp_subchannel *sub,
  654. struct mgcp_request *req, unsigned int seqno)
  655. {
  656. int res = 0;
  657. struct mgcp_request **queue, *q, *r, *t;
  658. ast_mutex_t *l;
  659. ast_debug(1, "Slow sequence is %d\n", p->slowsequence);
  660. if (p->slowsequence) {
  661. queue = &p->cmd_queue;
  662. l = &p->cmd_queue_lock;
  663. ast_mutex_lock(l);
  664. } else {
  665. switch (req->cmd) {
  666. case MGCP_CMD_DLCX:
  667. queue = &sub->cx_queue;
  668. l = &sub->cx_queue_lock;
  669. ast_mutex_lock(l);
  670. q = sub->cx_queue;
  671. /* delete pending cx cmds */
  672. /* buggy sb5120 */
  673. if (!sub->parent->ncs) {
  674. while (q) {
  675. r = q->next;
  676. ast_free(q);
  677. q = r;
  678. }
  679. *queue = NULL;
  680. }
  681. break;
  682. case MGCP_CMD_CRCX:
  683. case MGCP_CMD_MDCX:
  684. queue = &sub->cx_queue;
  685. l = &sub->cx_queue_lock;
  686. ast_mutex_lock(l);
  687. break;
  688. case MGCP_CMD_RQNT:
  689. queue = &p->rqnt_queue;
  690. l = &p->rqnt_queue_lock;
  691. ast_mutex_lock(l);
  692. break;
  693. default:
  694. queue = &p->cmd_queue;
  695. l = &p->cmd_queue_lock;
  696. ast_mutex_lock(l);
  697. break;
  698. }
  699. }
  700. if (!(r = ast_malloc(sizeof(*r)))) {
  701. ast_log(LOG_WARNING, "Cannot post MGCP request: insufficient memory\n");
  702. ast_mutex_unlock(l);
  703. return -1;
  704. }
  705. memcpy(r, req, sizeof(*r));
  706. if (!(*queue)) {
  707. ast_debug(1, "Posting Request:\n%s to %s:%d\n", req->data,
  708. ast_inet_ntoa(p->parent->addr.sin_addr), ntohs(p->parent->addr.sin_port));
  709. res = mgcp_postrequest(p, sub, req->data, req->len, seqno);
  710. } else {
  711. ast_debug(1, "Queueing Request:\n%s to %s:%d\n", req->data,
  712. ast_inet_ntoa(p->parent->addr.sin_addr), ntohs(p->parent->addr.sin_port));
  713. }
  714. /* XXX find tail. We could also keep tail in the data struct for faster access */
  715. for (t = *queue; t && t->next; t = t->next);
  716. r->next = NULL;
  717. if (t)
  718. t->next = r;
  719. else
  720. *queue = r;
  721. ast_mutex_unlock(l);
  722. return res;
  723. }
  724. static int mgcp_call(struct ast_channel *ast, const char *dest, int timeout)
  725. {
  726. int res;
  727. struct mgcp_endpoint *p;
  728. struct mgcp_subchannel *sub;
  729. char tone[50] = "";
  730. const char *distinctive_ring = pbx_builtin_getvar_helper(ast, "ALERT_INFO");
  731. ast_debug(3, "MGCP mgcp_call(%s)\n", ast_channel_name(ast));
  732. sub = ast_channel_tech_pvt(ast);
  733. p = sub->parent;
  734. ast_mutex_lock(&sub->lock);
  735. switch (p->hookstate) {
  736. case MGCP_OFFHOOK:
  737. if (!ast_strlen_zero(distinctive_ring)) {
  738. snprintf(tone, sizeof(tone), "L/wt%s", distinctive_ring);
  739. ast_debug(3, "MGCP distinctive callwait %s\n", tone);
  740. } else {
  741. ast_copy_string(tone, (p->ncs ? "L/wt1" : "L/wt"), sizeof(tone));
  742. ast_debug(3, "MGCP normal callwait %s\n", tone);
  743. }
  744. break;
  745. case MGCP_ONHOOK:
  746. default:
  747. if (!ast_strlen_zero(distinctive_ring)) {
  748. snprintf(tone, sizeof(tone), "L/r%s", distinctive_ring);
  749. ast_debug(3, "MGCP distinctive ring %s\n", tone);
  750. } else {
  751. ast_copy_string(tone, "L/rg", sizeof(tone));
  752. ast_debug(3, "MGCP default ring\n");
  753. }
  754. break;
  755. }
  756. if ((ast_channel_state(ast) != AST_STATE_DOWN) && (ast_channel_state(ast) != AST_STATE_RESERVED)) {
  757. ast_log(LOG_WARNING, "mgcp_call called on %s, neither down nor reserved\n", ast_channel_name(ast));
  758. ast_mutex_unlock(&sub->lock);
  759. return -1;
  760. }
  761. res = 0;
  762. sub->outgoing = 1;
  763. sub->cxmode = MGCP_CX_RECVONLY;
  764. ast_setstate(ast, AST_STATE_RINGING);
  765. if (p->type == TYPE_LINE) {
  766. if (!sub->rtp) {
  767. start_rtp(sub);
  768. } else {
  769. transmit_modify_request(sub);
  770. }
  771. if (sub->next->owner && !ast_strlen_zero(sub->next->cxident) && !ast_strlen_zero(sub->next->callid)) {
  772. /* try to prevent a callwait from disturbing the other connection */
  773. sub->next->cxmode = MGCP_CX_RECVONLY;
  774. transmit_modify_request(sub->next);
  775. }
  776. transmit_notify_request_with_callerid(sub, tone,
  777. S_COR(ast_channel_connected(ast)->id.number.valid, ast_channel_connected(ast)->id.number.str, ""),
  778. S_COR(ast_channel_connected(ast)->id.name.valid, ast_channel_connected(ast)->id.name.str, ""));
  779. ast_setstate(ast, AST_STATE_RINGING);
  780. if (sub->next->owner && !ast_strlen_zero(sub->next->cxident) && !ast_strlen_zero(sub->next->callid)) {
  781. /* Put the connection back in sendrecv */
  782. sub->next->cxmode = MGCP_CX_SENDRECV;
  783. transmit_modify_request(sub->next);
  784. }
  785. } else {
  786. ast_log(LOG_NOTICE, "Don't know how to dial on trunks yet\n");
  787. res = -1;
  788. }
  789. ast_mutex_unlock(&sub->lock);
  790. return res;
  791. }
  792. static int mgcp_hangup(struct ast_channel *ast)
  793. {
  794. struct mgcp_subchannel *sub = ast_channel_tech_pvt(ast);
  795. struct mgcp_endpoint *p = sub->parent;
  796. ast_debug(1, "mgcp_hangup(%s)\n", ast_channel_name(ast));
  797. if (!ast_channel_tech_pvt(ast)) {
  798. ast_debug(1, "Asked to hangup channel not connected\n");
  799. return 0;
  800. }
  801. if (strcmp(sub->magic, MGCP_SUBCHANNEL_MAGIC)) {
  802. ast_debug(1, "Invalid magic. MGCP subchannel freed up already.\n");
  803. return 0;
  804. }
  805. ast_mutex_lock(&sub->lock);
  806. ast_debug(3, "MGCP mgcp_hangup(%s) on %s@%s\n", ast_channel_name(ast), p->name, p->parent->name);
  807. if ((p->dtmfmode & MGCP_DTMF_INBAND) && p->dsp) {
  808. /* check whether other channel is active. */
  809. if (!sub->next->owner) {
  810. if (p->dtmfmode & MGCP_DTMF_HYBRID) {
  811. p->dtmfmode &= ~MGCP_DTMF_INBAND;
  812. }
  813. ast_debug(2, "MGCP free dsp on %s@%s\n", p->name, p->parent->name);
  814. ast_dsp_free(p->dsp);
  815. p->dsp = NULL;
  816. }
  817. }
  818. mgcp_set_owner(sub, NULL);
  819. /* for deleting gate */
  820. if (p->pktcgatealloc && sub->gate) {
  821. sub->gate->gate_open = NULL;
  822. sub->gate->gate_remove = NULL;
  823. sub->gate->got_dq_gi = NULL;
  824. sub->gate->tech_pvt = NULL;
  825. if (sub->gate->state == GATE_ALLOC_PROGRESS || sub->gate->state == GATE_ALLOCATED) {
  826. ast_pktccops_gate_alloc(GATE_DEL, sub->gate, 0, 0, 0, 0, 0, 0, NULL, NULL);
  827. } else {
  828. sub->gate->deltimer = time(NULL) + 5;
  829. }
  830. sub->gate = NULL;
  831. }
  832. if (!ast_strlen_zero(sub->cxident)) {
  833. transmit_connection_del(sub);
  834. }
  835. sub->cxident[0] = '\0';
  836. if ((sub == p->sub) && sub->next->owner) {
  837. RAII_VAR(struct ast_channel *, bridged, ast_channel_bridge_peer(sub->next->owner), ast_channel_cleanup);
  838. if (p->hookstate == MGCP_OFFHOOK) {
  839. if (sub->next->owner && bridged) {
  840. /* ncs fix! */
  841. transmit_notify_request_with_callerid(p->sub, (p->ncs ? "L/wt1" : "L/wt"),
  842. S_COR(ast_channel_caller(bridged)->id.number.valid, ast_channel_caller(bridged)->id.number.str, ""),
  843. S_COR(ast_channel_caller(bridged)->id.name.valid, ast_channel_caller(bridged)->id.name.str, ""));
  844. }
  845. } else {
  846. /* set our other connection as the primary and swith over to it */
  847. p->sub = sub->next;
  848. p->sub->cxmode = MGCP_CX_RECVONLY;
  849. transmit_modify_request(p->sub);
  850. if (sub->next->owner && bridged) {
  851. transmit_notify_request_with_callerid(p->sub, "L/rg",
  852. S_COR(ast_channel_caller(bridged)->id.number.valid, ast_channel_caller(bridged)->id.number.str, ""),
  853. S_COR(ast_channel_caller(bridged)->id.name.valid, ast_channel_caller(bridged)->id.name.str, ""));
  854. }
  855. }
  856. } else if ((sub == p->sub->next) && p->hookstate == MGCP_OFFHOOK) {
  857. transmit_notify_request(sub, p->ncs ? "" : "L/v");
  858. } else if (p->hookstate == MGCP_OFFHOOK) {
  859. transmit_notify_request(sub, "L/ro");
  860. } else {
  861. transmit_notify_request(sub, "");
  862. }
  863. ast_channel_tech_pvt_set(ast, NULL);
  864. sub->alreadygone = 0;
  865. sub->outgoing = 0;
  866. sub->cxmode = MGCP_CX_INACTIVE;
  867. sub->callid[0] = '\0';
  868. if (p) {
  869. memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
  870. }
  871. /* Reset temporary destination */
  872. memset(&sub->tmpdest, 0, sizeof(sub->tmpdest));
  873. if (sub->rtp) {
  874. ast_rtp_instance_destroy(sub->rtp);
  875. sub->rtp = NULL;
  876. }
  877. ast_module_unref(ast_module_info->self);
  878. if ((p->hookstate == MGCP_ONHOOK) && (!sub->next->rtp)) {
  879. p->hidecallerid = 0;
  880. if (p->hascallwaiting && !p->callwaiting) {
  881. ast_verb(3, "Enabling call waiting on %s\n", ast_channel_name(ast));
  882. p->callwaiting = -1;
  883. }
  884. if (has_voicemail(p)) {
  885. ast_debug(3, "MGCP mgcp_hangup(%s) on %s@%s set vmwi(+)\n",
  886. ast_channel_name(ast), p->name, p->parent->name);
  887. transmit_notify_request(sub, "L/vmwi(+)");
  888. } else {
  889. ast_debug(3, "MGCP mgcp_hangup(%s) on %s@%s set vmwi(-)\n",
  890. ast_channel_name(ast), p->name, p->parent->name);
  891. transmit_notify_request(sub, "L/vmwi(-)");
  892. }
  893. }
  894. ast_mutex_unlock(&sub->lock);
  895. return 0;
  896. }
  897. static char *handle_mgcp_show_endpoints(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  898. {
  899. struct mgcp_gateway *mg;
  900. struct mgcp_endpoint *me;
  901. int hasendpoints = 0;
  902. struct ast_variable * v = NULL;
  903. switch (cmd) {
  904. case CLI_INIT:
  905. e->command = "mgcp show endpoints";
  906. e->usage =
  907. "Usage: mgcp show endpoints\n"
  908. " Lists all endpoints known to the MGCP (Media Gateway Control Protocol) subsystem.\n";
  909. return NULL;
  910. case CLI_GENERATE:
  911. return NULL;
  912. }
  913. if (a->argc != 3) {
  914. return CLI_SHOWUSAGE;
  915. }
  916. ast_mutex_lock(&gatelock);
  917. for (mg = gateways; mg; mg = mg->next) {
  918. ast_cli(a->fd, "Gateway '%s' at %s (%s%s)\n", mg->name, mg->addr.sin_addr.s_addr ? ast_inet_ntoa(mg->addr.sin_addr) : ast_inet_ntoa(mg->defaddr.sin_addr), mg->realtime ? "Realtime, " : "", mg->dynamic ? "Dynamic" : "Static");
  919. for (me = mg->endpoints; me; me = me->next) {
  920. ast_cli(a->fd, " -- '%s@%s in '%s' is %s\n", me->name, mg->name, me->context, me->sub->owner ? "active" : "idle");
  921. if (me->chanvars) {
  922. ast_cli(a->fd, " Variables:\n");
  923. for (v = me->chanvars ; v ; v = v->next) {
  924. ast_cli(a->fd, " %s = '%s'\n", v->name, v->value);
  925. }
  926. }
  927. hasendpoints = 1;
  928. }
  929. if (!hasendpoints) {
  930. ast_cli(a->fd, " << No Endpoints Defined >> ");
  931. }
  932. }
  933. ast_mutex_unlock(&gatelock);
  934. return CLI_SUCCESS;
  935. }
  936. static char *handle_mgcp_audit_endpoint(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  937. {
  938. struct mgcp_gateway *mg;
  939. struct mgcp_endpoint *me;
  940. int found = 0;
  941. char *ename,*gname, *c;
  942. switch (cmd) {
  943. case CLI_INIT:
  944. e->command = "mgcp audit endpoint";
  945. e->usage =
  946. "Usage: mgcp audit endpoint <endpointid>\n"
  947. " Lists the capabilities of an endpoint in the MGCP (Media Gateway Control Protocol) subsystem.\n"
  948. " mgcp debug MUST be on to see the results of this command.\n";
  949. return NULL;
  950. case CLI_GENERATE:
  951. return NULL;
  952. }
  953. if (!mgcpdebug) {
  954. return CLI_SHOWUSAGE;
  955. }
  956. if (a->argc != 4)
  957. return CLI_SHOWUSAGE;
  958. /* split the name into parts by null */
  959. ename = ast_strdupa(a->argv[3]);
  960. for (gname = ename; *gname; gname++) {
  961. if (*gname == '@') {
  962. *gname = 0;
  963. gname++;
  964. break;
  965. }
  966. }
  967. if (gname[0] == '[') {
  968. gname++;
  969. }
  970. if ((c = strrchr(gname, ']'))) {
  971. *c = '\0';
  972. }
  973. ast_mutex_lock(&gatelock);
  974. for (mg = gateways; mg; mg = mg->next) {
  975. if (!strcasecmp(mg->name, gname)) {
  976. for (me = mg->endpoints; me; me = me->next) {
  977. if (!strcasecmp(me->name, ename)) {
  978. found = 1;
  979. transmit_audit_endpoint(me);
  980. break;
  981. }
  982. }
  983. if (found) {
  984. break;
  985. }
  986. }
  987. }
  988. if (!found) {
  989. ast_cli(a->fd, " << Could not find endpoint >> ");
  990. }
  991. ast_mutex_unlock(&gatelock);
  992. return CLI_SUCCESS;
  993. }
  994. static char *handle_mgcp_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  995. {
  996. switch (cmd) {
  997. case CLI_INIT:
  998. e->command = "mgcp set debug {on|off}";
  999. e->usage =
  1000. "Usage: mgcp set debug {on|off}\n"
  1001. " Enables/Disables dumping of MGCP packets for debugging purposes\n";
  1002. return NULL;
  1003. case CLI_GENERATE:
  1004. return NULL;
  1005. }
  1006. if (a->argc != e->args)
  1007. return CLI_SHOWUSAGE;
  1008. if (!strncasecmp(a->argv[e->args - 1], "on", 2)) {
  1009. mgcpdebug = 1;
  1010. ast_cli(a->fd, "MGCP Debugging Enabled\n");
  1011. } else if (!strncasecmp(a->argv[3], "off", 3)) {
  1012. mgcpdebug = 0;
  1013. ast_cli(a->fd, "MGCP Debugging Disabled\n");
  1014. } else {
  1015. return CLI_SHOWUSAGE;
  1016. }
  1017. return CLI_SUCCESS;
  1018. }
  1019. static struct ast_cli_entry cli_mgcp[] = {
  1020. AST_CLI_DEFINE(handle_mgcp_audit_endpoint, "Audit specified MGCP endpoint"),
  1021. AST_CLI_DEFINE(handle_mgcp_show_endpoints, "List defined MGCP endpoints"),
  1022. AST_CLI_DEFINE(handle_mgcp_set_debug, "Enable/Disable MGCP debugging"),
  1023. AST_CLI_DEFINE(mgcp_reload, "Reload MGCP configuration"),
  1024. };
  1025. static int mgcp_answer(struct ast_channel *ast)
  1026. {
  1027. int res = 0;
  1028. struct mgcp_subchannel *sub = ast_channel_tech_pvt(ast);
  1029. struct mgcp_endpoint *p = sub->parent;
  1030. ast_mutex_lock(&sub->lock);
  1031. sub->cxmode = MGCP_CX_SENDRECV;
  1032. if (!sub->rtp) {
  1033. start_rtp(sub);
  1034. } else {
  1035. transmit_modify_request(sub);
  1036. }
  1037. ast_verb(3, "MGCP mgcp_answer(%s) on %s@%s-%d\n",
  1038. ast_channel_name(ast), p->name, p->parent->name, sub->id);
  1039. if (ast_channel_state(ast) != AST_STATE_UP) {
  1040. ast_setstate(ast, AST_STATE_UP);
  1041. ast_debug(1, "mgcp_answer(%s)\n", ast_channel_name(ast));
  1042. transmit_notify_request(sub, "");
  1043. transmit_modify_request(sub);
  1044. }
  1045. ast_mutex_unlock(&sub->lock);
  1046. return res;
  1047. }
  1048. static struct ast_frame *mgcp_rtp_read(struct mgcp_subchannel *sub)
  1049. {
  1050. /* Retrieve audio/etc from channel. Assumes sub->lock is already held. */
  1051. struct ast_frame *f;
  1052. f = ast_rtp_instance_read(sub->rtp, 0);
  1053. /* Don't send RFC2833 if we're not supposed to */
  1054. if (f && (f->frametype == AST_FRAME_DTMF) && !(sub->parent->dtmfmode & MGCP_DTMF_RFC2833))
  1055. return &ast_null_frame;
  1056. if (sub->owner) {
  1057. /* We already hold the channel lock */
  1058. if (f->frametype == AST_FRAME_VOICE) {
  1059. if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(sub->owner), f->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
  1060. struct ast_format_cap *caps;
  1061. ast_debug(1, "Oooh, format changed to %s\n", ast_format_get_name(f->subclass.format));
  1062. caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  1063. if (caps) {
  1064. ast_format_cap_append(caps, f->subclass.format, 0);
  1065. ast_channel_nativeformats_set(sub->owner, caps);
  1066. ao2_ref(caps, -1);
  1067. } else {
  1068. return &ast_null_frame;
  1069. }
  1070. ast_set_read_format(sub->owner, ast_channel_readformat(sub->owner));
  1071. ast_set_write_format(sub->owner, ast_channel_writeformat(sub->owner));
  1072. }
  1073. /* Courtesy fearnor aka alex@pilosoft.com */
  1074. if ((sub->parent->dtmfmode & MGCP_DTMF_INBAND) && (sub->parent->dsp)) {
  1075. #if 0
  1076. ast_log(LOG_NOTICE, "MGCP ast_dsp_process\n");
  1077. #endif
  1078. f = ast_dsp_process(sub->owner, sub->parent->dsp, f);
  1079. }
  1080. }
  1081. }
  1082. return f;
  1083. }
  1084. static void mgcp_set_owner(struct mgcp_subchannel *sub, struct ast_channel *chan)
  1085. {
  1086. sub->owner = chan;
  1087. if (sub->rtp) {
  1088. ast_rtp_instance_set_channel_id(sub->rtp, sub->owner ? ast_channel_uniqueid(chan) : "");
  1089. }
  1090. }
  1091. static struct ast_frame *mgcp_read(struct ast_channel *ast)
  1092. {
  1093. struct ast_frame *f;
  1094. struct mgcp_subchannel *sub = ast_channel_tech_pvt(ast);
  1095. ast_mutex_lock(&sub->lock);
  1096. f = mgcp_rtp_read(sub);
  1097. ast_mutex_unlock(&sub->lock);
  1098. return f;
  1099. }
  1100. static int mgcp_write(struct ast_channel *ast, struct ast_frame *frame)
  1101. {
  1102. struct mgcp_subchannel *sub = ast_channel_tech_pvt(ast);
  1103. int res = 0;
  1104. if (frame->frametype != AST_FRAME_VOICE) {
  1105. if (frame->frametype == AST_FRAME_IMAGE)
  1106. return 0;
  1107. else {
  1108. ast_log(LOG_WARNING, "Can't send %u type frames with MGCP write\n", frame->frametype);
  1109. return 0;
  1110. }
  1111. } else {
  1112. if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), frame->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
  1113. struct ast_str *cap_buf = ast_str_alloca(64);
  1114. ast_log(LOG_WARNING, "Asked to transmit frame type %s, while native formats is %s (read/write = %s/%s)\n",
  1115. ast_format_get_name(frame->subclass.format),
  1116. ast_format_cap_get_names(ast_channel_nativeformats(ast), &cap_buf),
  1117. ast_format_get_name(ast_channel_readformat(ast)),
  1118. ast_format_get_name(ast_channel_writeformat(ast)));
  1119. /* return -1; */
  1120. }
  1121. }
  1122. if (sub) {
  1123. ast_mutex_lock(&sub->lock);
  1124. if (!sub->sdpsent && sub->gate) {
  1125. if (sub->gate->state == GATE_ALLOCATED) {
  1126. ast_debug(1, "GATE ALLOCATED, sending sdp\n");
  1127. transmit_modify_with_sdp(sub, NULL, 0);
  1128. }
  1129. }
  1130. if ((sub->parent->sub == sub) || !sub->parent->singlepath) {
  1131. if (sub->rtp) {
  1132. res = ast_rtp_instance_write(sub->rtp, frame);
  1133. }
  1134. }
  1135. ast_mutex_unlock(&sub->lock);
  1136. }
  1137. return res;
  1138. }
  1139. static int mgcp_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  1140. {
  1141. struct mgcp_subchannel *sub = ast_channel_tech_pvt(newchan);
  1142. ast_mutex_lock(&sub->lock);
  1143. ast_log(LOG_NOTICE, "mgcp_fixup(%s, %s)\n", ast_channel_name(oldchan), ast_channel_name(newchan));
  1144. if (sub->owner != oldchan) {
  1145. ast_mutex_unlock(&sub->lock);
  1146. ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, sub->owner);
  1147. return -1;
  1148. }
  1149. mgcp_set_owner(sub, newchan);
  1150. ast_mutex_unlock(&sub->lock);
  1151. return 0;
  1152. }
  1153. static int mgcp_senddigit_begin(struct ast_channel *ast, char digit)
  1154. {
  1155. struct mgcp_subchannel *sub = ast_channel_tech_pvt(ast);
  1156. struct mgcp_endpoint *p = sub->parent;
  1157. int res = 0;
  1158. ast_mutex_lock(&sub->lock);
  1159. if (p->dtmfmode & MGCP_DTMF_INBAND || p->dtmfmode & MGCP_DTMF_HYBRID) {
  1160. ast_debug(1, "Sending DTMF using inband/hybrid\n");
  1161. res = -1; /* Let asterisk play inband indications */
  1162. } else if (p->dtmfmode & MGCP_DTMF_RFC2833) {
  1163. ast_debug(1, "Sending DTMF using RFC2833\n");
  1164. ast_rtp_instance_dtmf_begin(sub->rtp, digit);
  1165. } else {
  1166. ast_log(LOG_ERROR, "Don't know about DTMF_MODE %d\n", p->dtmfmode);
  1167. }
  1168. ast_mutex_unlock(&sub->lock);
  1169. return res;
  1170. }
  1171. static int mgcp_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration)
  1172. {
  1173. struct mgcp_subchannel *sub = ast_channel_tech_pvt(ast);
  1174. struct mgcp_endpoint *p = sub->parent;
  1175. int res = 0;
  1176. char tmp[4];
  1177. ast_mutex_lock(&sub->lock);
  1178. if (p->dtmfmode & MGCP_DTMF_INBAND || p->dtmfmode & MGCP_DTMF_HYBRID) {
  1179. ast_debug(1, "Stopping DTMF using inband/hybrid\n");
  1180. res = -1; /* Tell Asterisk to stop inband indications */
  1181. } else if (p->dtmfmode & MGCP_DTMF_RFC2833) {
  1182. ast_debug(1, "Stopping DTMF using RFC2833\n");
  1183. if (sub->parent->ncs) {
  1184. tmp[0] = digit;
  1185. tmp[1] = '\0';
  1186. } else {
  1187. tmp[0] = 'D';
  1188. tmp[1] = '/';
  1189. tmp[2] = digit;
  1190. tmp[3] = '\0';
  1191. }
  1192. transmit_notify_request(sub, tmp);
  1193. ast_rtp_instance_dtmf_end(sub->rtp, digit);
  1194. } else {
  1195. ast_log(LOG_ERROR, "Don't know about DTMF_MODE %d\n", p->dtmfmode);
  1196. }
  1197. ast_mutex_unlock(&sub->lock);
  1198. return res;
  1199. }
  1200. /*!
  1201. * \brief mgcp_devicestate: channel callback for device status monitoring
  1202. * \param data tech/resource name of MGCP device to query
  1203. *
  1204. * Callback for device state management in channel subsystem
  1205. * to obtain device status (up/down) of a specific MGCP endpoint
  1206. *
  1207. * \return device status result (from devicestate.h) AST_DEVICE_INVALID (not available) or AST_DEVICE_UNKNOWN (available but unknown state)
  1208. */
  1209. static int mgcp_devicestate(const char *data)
  1210. {
  1211. struct mgcp_gateway *g;
  1212. struct mgcp_endpoint *e = NULL;
  1213. char *tmp, *endpt, *gw;
  1214. int ret = AST_DEVICE_INVALID;
  1215. endpt = ast_strdupa(data);
  1216. if ((tmp = strchr(endpt, '@'))) {
  1217. *tmp++ = '\0';
  1218. gw = tmp;
  1219. } else
  1220. goto error;
  1221. ast_mutex_lock(&gatelock);
  1222. for (g = gateways; g; g = g->next) {
  1223. if (strcasecmp(g->name, gw) == 0) {
  1224. e = g->endpoints;
  1225. break;
  1226. }
  1227. }
  1228. if (!e)
  1229. goto error;
  1230. for (; e; e = e->next) {
  1231. if (strcasecmp(e->name, endpt) == 0) {
  1232. break;
  1233. }
  1234. }
  1235. if (!e)
  1236. goto error;
  1237. /*
  1238. * As long as the gateway/endpoint is valid, we'll
  1239. * assume that the device is available and its state
  1240. * can be tracked.
  1241. */
  1242. ret = AST_DEVICE_UNKNOWN;
  1243. error:
  1244. ast_mutex_unlock(&gatelock);
  1245. return ret;
  1246. }
  1247. static char *control2str(int ind) {
  1248. switch (ind) {
  1249. case AST_CONTROL_HANGUP:
  1250. return "Other end has hungup";
  1251. case AST_CONTROL_RING:
  1252. return "Local ring";
  1253. case AST_CONTROL_RINGING:
  1254. return "Remote end is ringing";
  1255. case AST_CONTROL_ANSWER:
  1256. return "Remote end has answered";
  1257. case AST_CONTROL_BUSY:
  1258. return "Remote end is busy";
  1259. case AST_CONTROL_TAKEOFFHOOK:
  1260. return "Make it go off hook";
  1261. case AST_CONTROL_OFFHOOK:
  1262. return "Line is off hook";
  1263. case AST_CONTROL_CONGESTION:
  1264. return "Congestion (circuits busy)";
  1265. case AST_CONTROL_FLASH:
  1266. return "Flash hook";
  1267. case AST_CONTROL_WINK:
  1268. return "Wink";
  1269. case AST_CONTROL_OPTION:
  1270. return "Set a low-level option";
  1271. case AST_CONTROL_RADIO_KEY:
  1272. return "Key Radio";
  1273. case AST_CONTROL_RADIO_UNKEY:
  1274. return "Un-Key Radio";
  1275. }
  1276. return "UNKNOWN";
  1277. }
  1278. static int mgcp_indicate(struct ast_channel *ast, int ind, const void *data, size_t datalen)
  1279. {
  1280. struct mgcp_subchannel *sub = ast_channel_tech_pvt(ast);
  1281. int res = 0;
  1282. ast_debug(3, "MGCP asked to indicate %d '%s' condition on channel %s\n",
  1283. ind, control2str(ind), ast_channel_name(ast));
  1284. ast_mutex_lock(&sub->lock);
  1285. switch(ind) {
  1286. case AST_CONTROL_RINGING:
  1287. #ifdef DLINK_BUGGY_FIRMWARE
  1288. transmit_notify_request(sub, "rt");
  1289. #else
  1290. if (!sub->sdpsent) { /* will hide the inband progress!!! */
  1291. transmit_notify_request(sub, sub->parent->ncs ? "L/rt" : "G/rt");
  1292. }
  1293. #endif
  1294. break;
  1295. case AST_CONTROL_BUSY:
  1296. transmit_notify_request(sub, "L/bz");
  1297. break;
  1298. case AST_CONTROL_INCOMPLETE:
  1299. /* We do not currently support resetting of the Interdigit Timer, so treat
  1300. * Incomplete control frames as a congestion response
  1301. */
  1302. case AST_CONTROL_CONGESTION:
  1303. transmit_notify_request(sub, sub->parent->ncs ? "L/cg" : "G/cg");
  1304. break;
  1305. case AST_CONTROL_HOLD:
  1306. ast_moh_start(ast, data, NULL);
  1307. break;
  1308. case AST_CONTROL_UNHOLD:
  1309. ast_moh_stop(ast);
  1310. break;
  1311. case AST_CONTROL_SRCUPDATE:
  1312. ast_rtp_instance_update_source(sub->rtp);
  1313. break;
  1314. case AST_CONTROL_SRCCHANGE:
  1315. ast_rtp_instance_change_source(sub->rtp);
  1316. break;
  1317. case AST_CONTROL_PROGRESS:
  1318. case AST_CONTROL_PROCEEDING:
  1319. transmit_modify_request(sub);
  1320. case -1:
  1321. transmit_notify_request(sub, "");
  1322. break;
  1323. default:
  1324. ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", ind);
  1325. /* fallthrough */
  1326. case AST_CONTROL_PVT_CAUSE_CODE:
  1327. res = -1;
  1328. }
  1329. ast_mutex_unlock(&sub->lock);
  1330. return res;
  1331. }
  1332. static struct ast_channel *mgcp_new(struct mgcp_subchannel *sub, int state, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor)
  1333. {
  1334. struct ast_format_cap *caps = NULL;
  1335. struct ast_channel *tmp;
  1336. struct ast_variable *v = NULL;
  1337. struct mgcp_endpoint *i = sub->parent;
  1338. struct ast_format *tmpfmt;
  1339. caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  1340. if (!caps) {
  1341. ast_log(LOG_ERROR, "Format capabilities could not be created\n");
  1342. return NULL;
  1343. }
  1344. tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, i->accountcode, i->exten, i->context, assignedids, requestor, i->amaflags, "MGCP/%s@%s-%d", i->name, i->parent->name, sub->id);
  1345. if (!tmp) {
  1346. ast_log(LOG_WARNING, "Channel could not be created\n");
  1347. ao2_ref(caps, -1);
  1348. return NULL;
  1349. }
  1350. ast_channel_stage_snapshot(tmp);
  1351. ast_channel_tech_set(tmp, &mgcp_tech);
  1352. if (ast_format_cap_count(i->cap)) {
  1353. ast_format_cap_append_from_cap(caps, i->cap, AST_MEDIA_TYPE_UNKNOWN);
  1354. } else {
  1355. ast_format_cap_append_from_cap(caps, global_capability, AST_MEDIA_TYPE_UNKNOWN);
  1356. }
  1357. ast_channel_nativeformats_set(tmp, caps);
  1358. ao2_ref(caps, -1);
  1359. if (sub->rtp) {
  1360. ast_channel_set_fd(tmp, 0, ast_rtp_instance_fd(sub->rtp, 0));
  1361. }
  1362. if (i->dtmfmode & (MGCP_DTMF_INBAND | MGCP_DTMF_HYBRID)) {
  1363. i->dsp = ast_dsp_new();
  1364. ast_dsp_set_features(i->dsp, DSP_FEATURE_DIGIT_DETECT);
  1365. /* this is to prevent clipping of dtmf tones during dsp processing */
  1366. ast_dsp_set_digitmode(i->dsp, DSP_DIGITMODE_NOQUELCH);
  1367. } else {
  1368. i->dsp = NULL;
  1369. }
  1370. if (state == AST_STATE_RING) {
  1371. ast_channel_rings_set(tmp, 1);
  1372. }
  1373. tmpfmt = ast_format_cap_get_format(ast_channel_nativeformats(tmp), 0);
  1374. ast_channel_set_writeformat(tmp, tmpfmt);
  1375. ast_channel_set_rawwriteformat(tmp, tmpfmt);
  1376. ast_channel_set_readformat(tmp, tmpfmt);
  1377. ast_channel_set_rawreadformat(tmp, tmpfmt);
  1378. ao2_ref(tmpfmt, -1);
  1379. ast_channel_tech_pvt_set(tmp, sub);
  1380. if (!ast_strlen_zero(i->language))
  1381. ast_channel_language_set(tmp, i->language);
  1382. if (!ast_strlen_zero(i->accountcode))
  1383. ast_channel_accountcode_set(tmp, i->accountcode);
  1384. if (i->amaflags)
  1385. ast_channel_amaflags_set(tmp, i->amaflags);
  1386. mgcp_set_owner(sub, tmp);
  1387. ast_module_ref(ast_module_info->self);
  1388. ast_channel_callgroup_set(tmp, i->callgroup);
  1389. ast_channel_pickupgroup_set(tmp, i->pickupgroup);
  1390. ast_channel_call_forward_set(tmp, i->call_forward);
  1391. ast_channel_context_set(tmp, i->context);
  1392. ast_channel_exten_set(tmp, i->exten);
  1393. /* Don't use ast_set_callerid() here because it will
  1394. * generate a needless NewCallerID event */
  1395. if (!ast_strlen_zero(i->cid_num)) {
  1396. ast_channel_caller(tmp)->ani.number.valid = 1;
  1397. ast_channel_caller(tmp)->ani.number.str = ast_strdup(i->cid_num);
  1398. }
  1399. if (!i->adsi) {
  1400. ast_channel_adsicpe_set(tmp, AST_ADSI_UNAVAILABLE);
  1401. }
  1402. ast_channel_priority_set(tmp, 1);
  1403. /* Set channel variables for this call from configuration */
  1404. for (v = i->chanvars ; v ; v = v->next) {
  1405. char valuebuf[1024];
  1406. pbx_builtin_setvar_helper(tmp, v->name, ast_get_encoded_str(v->value, valuebuf, sizeof(valuebuf)));
  1407. }
  1408. if (sub->rtp) {
  1409. ast_jb_configure(tmp, &global_jbconf);
  1410. }
  1411. ast_channel_stage_snapshot_done(tmp);
  1412. ast_channel_unlock(tmp);
  1413. if (state != AST_STATE_DOWN) {
  1414. if (ast_pbx_start(tmp)) {
  1415. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(tmp));
  1416. ast_hangup(tmp);
  1417. tmp = NULL;
  1418. }
  1419. }
  1420. ast_verb(3, "MGCP mgcp_new(%s) created in state: %s\n",
  1421. ast_channel_name(tmp), ast_state2str(state));
  1422. return tmp;
  1423. }
  1424. static char *get_sdp_by_line(char* line, char *name, int nameLen)
  1425. {
  1426. if (strncasecmp(line, name, nameLen) == 0 && line[nameLen] == '=') {
  1427. char *r = line + nameLen + 1;
  1428. while (*r && (*r < 33)) ++r;
  1429. return r;
  1430. }
  1431. return "";
  1432. }
  1433. static char *get_sdp(struct mgcp_request *req, char *name)
  1434. {
  1435. int x;
  1436. int len = strlen(name);
  1437. char *r;
  1438. for (x = 0; x < req->lines; x++) {
  1439. r = get_sdp_by_line(req->line[x], name, len);
  1440. if (r[0] != '\0') return r;
  1441. }
  1442. return "";
  1443. }
  1444. static void sdpLineNum_iterator_init(int *iterator)
  1445. {
  1446. *iterator = 0;
  1447. }
  1448. static char *get_sdp_iterate(int* iterator, struct mgcp_request *req, char *name)
  1449. {
  1450. int len = strlen(name);
  1451. char *r;
  1452. while (*iterator < req->lines) {
  1453. r = get_sdp_by_line(req->line[(*iterator)++], name, len);
  1454. if (r[0] != '\0') return r;
  1455. }
  1456. return "";
  1457. }
  1458. static char *__get_header(struct mgcp_request *req, char *name, int *start, char *def)
  1459. {
  1460. int x;
  1461. int len = strlen(name);
  1462. char *r;
  1463. for (x = *start; x < req->headers; x++) {
  1464. if (!strncasecmp(req->header[x], name, len) &&
  1465. (req->header[x][len] == ':')) {
  1466. r = req->header[x] + len + 1;
  1467. while (*r && (*r < 33)) {
  1468. r++;
  1469. }
  1470. *start = x + 1;
  1471. return r;
  1472. }
  1473. }
  1474. /* Don't return NULL, so get_header is always a valid pointer */
  1475. return def;
  1476. }
  1477. static char *get_header(struct mgcp_request *req, char *name)
  1478. {
  1479. int start = 0;
  1480. return __get_header(req, name, &start, "");
  1481. }
  1482. /*! \brief get_csv: (SC:) get comma separated value */
  1483. static char *get_csv(char *c, int *len, char **next)
  1484. {
  1485. char *s;
  1486. *next = NULL, *len = 0;
  1487. if (!c) return NULL;
  1488. while (*c && (*c < 33 || *c == ',')) {
  1489. c++;
  1490. }
  1491. s = c;
  1492. while (*c && (*c >= 33 && *c != ',')) {
  1493. c++, (*len)++;
  1494. }
  1495. *next = c;
  1496. if (*len == 0) {
  1497. s = NULL, *next = NULL;
  1498. }
  1499. return s;
  1500. }
  1501. static struct mgcp_gateway *find_realtime_gw(char *name, char *at, struct sockaddr_in *sin)
  1502. {
  1503. struct mgcp_gateway *g = NULL;
  1504. struct ast_variable *mgcpgwconfig = NULL;
  1505. struct ast_variable *gwv, *epname = NULL;
  1506. struct mgcp_endpoint *e;
  1507. char lines[256];
  1508. int i, j;
  1509. ast_debug(1, "*** find Realtime MGCPGW\n");
  1510. if (!(i = ast_check_realtime("mgcpgw")) || !(j = ast_check_realtime("mgcpep"))) {
  1511. return NULL;
  1512. }
  1513. if (ast_strlen_zero(at)) {
  1514. ast_debug(1, "null gw name\n");
  1515. return NULL;
  1516. }
  1517. if (!(mgcpgwconfig = ast_load_realtime("mgcpgw", "name", at, NULL))) {
  1518. return NULL;
  1519. }
  1520. /*!
  1521. * \note This is a fairly odd way of instantiating lines. Instead of each
  1522. * line created by virtue of being in the database (and loaded via
  1523. * ast_load_realtime_multientry), this code forces a specific order with a
  1524. * "lines" entry in the "mgcpgw" record. This has benefits, because as with
  1525. * chan_dahdi, values are inherited across definitions. The downside is
  1526. * that it's not as clear what the values will be simply by looking at a
  1527. * single row in the database, and it's probable that the sanest configuration
  1528. * should have the first column in the "mgcpep" table be "clearvars", with a
  1529. * static value of "all", if any variables are set at all. It may be worth
  1530. * making this assumption explicit in the code in the future, and then just
  1531. * using ast_load_realtime_multientry for the "mgcpep" records.
  1532. */
  1533. lines[0] = '\0';
  1534. for (gwv = mgcpgwconfig; gwv; gwv = gwv->next) {
  1535. if (!strcasecmp(gwv->name, "lines")) {
  1536. ast_copy_string(lines, gwv->value, sizeof(lines));
  1537. break;
  1538. }
  1539. }
  1540. /* Position gwv at the end of the list */
  1541. for (gwv = gwv && gwv->next ? gwv : mgcpgwconfig; gwv->next; gwv = gwv->next);
  1542. if (!ast_strlen_zero(lines)) {
  1543. AST_DECLARE_APP_ARGS(args,
  1544. AST_APP_ARG(line)[100];
  1545. );
  1546. AST_STANDARD_APP_ARGS(args, lines);
  1547. for (i = 0; i < args.argc; i++) {
  1548. gwv->next = ast_load_realtime("mgcpep", "name", at, "line", args.line[i], NULL);
  1549. /* Remove "line" AND position gwv at the end of the list. */
  1550. for (epname = NULL; gwv->next; gwv = gwv->next) {
  1551. if (!strcasecmp(gwv->next->name, "line")) {
  1552. /* Remove it from the list */
  1553. epname = gwv->next;
  1554. gwv->next = gwv->next->next;
  1555. }
  1556. }
  1557. /* Since "line" instantiates the configuration, we have to move it to the end. */
  1558. if (epname) {
  1559. gwv->next = epname;
  1560. epname->next = NULL;
  1561. gwv = gwv->next;
  1562. }
  1563. }
  1564. }
  1565. for (gwv = mgcpgwconfig; gwv; gwv = gwv->next) {
  1566. ast_debug(1, "MGCP Realtime var: %s => %s\n", gwv->name, gwv->value);
  1567. }
  1568. if (mgcpgwconfig) {
  1569. g = build_gateway(at, mgcpgwconfig);
  1570. ast_variables_destroy(mgcpgwconfig);
  1571. }
  1572. if (g) {
  1573. g->next = gateways;
  1574. g->realtime = 1;
  1575. gateways = g;
  1576. for (e = g->endpoints; e; e = e->next) {
  1577. transmit_audit_endpoint(e);
  1578. e->needaudit = 0;
  1579. }
  1580. }
  1581. return g;
  1582. }
  1583. static struct mgcp_subchannel *find_subchannel_and_lock(char *name, int msgid, struct sockaddr_in *sin)
  1584. {
  1585. struct mgcp_endpoint *p = NULL;
  1586. struct mgcp_subchannel *sub = NULL;
  1587. struct mgcp_gateway *g;
  1588. char tmp[256] = "";
  1589. char *at = NULL, *c;
  1590. int found = 0;
  1591. if (name) {
  1592. ast_copy_string(tmp, name, sizeof(tmp));
  1593. at = strchr(tmp, '@');
  1594. if (!at) {
  1595. ast_log(LOG_NOTICE, "Endpoint '%s' has no at sign!\n", name);
  1596. return NULL;
  1597. }
  1598. *at++ = '\0';
  1599. }
  1600. ast_mutex_lock(&gatelock);
  1601. if (at && (at[0] == '[')) {
  1602. at++;
  1603. c = strrchr(at, ']');
  1604. if (c) {
  1605. *c = '\0';
  1606. }
  1607. }
  1608. for (g = gateways ? gateways : find_realtime_gw(name, at, sin); g; g = g->next ? g->next : find_realtime_gw(name, at, sin)) {
  1609. if ((!name || !strcasecmp(g->name, at)) &&
  1610. (sin || g->addr.sin_addr.s_addr || g->defaddr.sin_addr.s_addr)) {
  1611. /* Found the gateway. If it's dynamic, save it's address -- now for the endpoint */
  1612. if (sin && g->dynamic && name) {
  1613. if ((g->addr.sin_addr.s_addr != sin->sin_addr.s_addr) ||
  1614. (g->addr.sin_port != sin->sin_port)) {
  1615. memcpy(&g->addr, sin, sizeof(g->addr));
  1616. {
  1617. struct ast_sockaddr tmp1, tmp2;
  1618. struct sockaddr_in tmp3 = {0,};
  1619. tmp3.sin_addr = g->ourip;
  1620. ast_sockaddr_from_sin(&tmp1, &g->addr);
  1621. ast_sockaddr_from_sin(&tmp2, &tmp3);
  1622. if (ast_ouraddrfor(&tmp1, &tmp2)) {
  1623. memcpy(&g->ourip, &__ourip, sizeof(g->ourip));
  1624. }
  1625. ast_sockaddr_to_sin(&tmp2, &tmp3);
  1626. g->ourip = tmp3.sin_addr;
  1627. }
  1628. ast_verb(3, "Registered MGCP gateway '%s' at %s port %d\n", g->name, ast_inet_ntoa(g->addr.sin_addr), ntohs(g->addr.sin_port));
  1629. }
  1630. /* not dynamic, check if the name matches */
  1631. } else if (name) {
  1632. if (strcasecmp(g->name, at)) {
  1633. g = g->next;
  1634. continue;
  1635. }
  1636. /* not dynamic, no name, check if the addr matches */
  1637. } else if (!name && sin) {
  1638. if ((g->addr.sin_addr.s_addr != sin->sin_addr.s_addr) ||
  1639. (g->addr.sin_port != sin->sin_port)) {
  1640. if(!g->next)
  1641. g = find_realtime_gw(name, at, sin);
  1642. else
  1643. g = g->next;
  1644. continue;
  1645. }
  1646. } else {
  1647. continue;
  1648. }
  1649. for (p = g->endpoints; p; p = p->next) {
  1650. ast_debug(1, "Searching on %s@%s for subchannel\n", p->name, g->name);
  1651. if (msgid) {
  1652. sub = p->sub;
  1653. found = 1;
  1654. break;
  1655. } else if (name && !strcasecmp(p->name, tmp)) {
  1656. ast_debug(1, "Coundn't determine subchannel, assuming current master %s@%s-%d\n",
  1657. p->name, g->name, p->sub->id);
  1658. sub = p->sub;
  1659. found = 1;
  1660. break;
  1661. }
  1662. }
  1663. if (sub && found) {
  1664. ast_mutex_lock(&sub->lock);
  1665. break;
  1666. }
  1667. }
  1668. }
  1669. ast_mutex_unlock(&gatelock);
  1670. if (!sub) {
  1671. if (name) {
  1672. if (g) {
  1673. ast_log(LOG_NOTICE, "Endpoint '%s' not found on gateway '%s'\n", tmp, at);
  1674. } else {
  1675. ast_log(LOG_NOTICE, "Gateway '%s' (and thus its endpoint '%s') does not exist\n", at, tmp);
  1676. }
  1677. }
  1678. }
  1679. return sub;
  1680. }
  1681. static void parse(struct mgcp_request *req)
  1682. {
  1683. /* Divide fields by NULL's */
  1684. char *c;
  1685. int f = 0;
  1686. c = req->data;
  1687. /* First header starts immediately */
  1688. req->header[f] = c;
  1689. for (; *c; c++) {
  1690. if (*c == '\n') {
  1691. /* We've got a new header */
  1692. *c = 0;
  1693. ast_debug(3, "Header: %s (%d)\n", req->header[f], (int) strlen(req->header[f]));
  1694. if (ast_strlen_zero(req->header[f])) {
  1695. /* Line by itself means we're now in content */
  1696. c++;
  1697. break;
  1698. }
  1699. if (f >= MGCP_MAX_HEADERS - 1) {
  1700. ast_log(LOG_WARNING, "Too many MGCP headers...\n");
  1701. } else {
  1702. f++;
  1703. }
  1704. req->header[f] = c + 1;
  1705. } else if (*c == '\r') {
  1706. /* Ignore but eliminate \r's */
  1707. *c = 0;
  1708. }
  1709. }
  1710. /* Check for last header */
  1711. if (!ast_strlen_zero(req->header[f])) {
  1712. f++;
  1713. }
  1714. req->headers = f;
  1715. /* Now we process any mime content */
  1716. f = 0;
  1717. req->line[f] = c;
  1718. for (; *c; c++) {
  1719. if (*c == '\n') {
  1720. /* We've got a new line */
  1721. *c = 0;
  1722. ast_debug(3, "Line: %s (%d)\n", req->line[f], (int) strlen(req->line[f]));
  1723. if (f >= MGCP_MAX_LINES - 1) {
  1724. ast_log(LOG_WARNING, "Too many SDP lines...\n");
  1725. } else {
  1726. f++;
  1727. }
  1728. req->line[f] = c + 1;
  1729. } else if (*c == '\r') {
  1730. /* Ignore and eliminate \r's */
  1731. *c = 0;
  1732. }
  1733. }
  1734. /* Check for last line */
  1735. if (!ast_strlen_zero(req->line[f])) {
  1736. f++;
  1737. }
  1738. req->lines = f;
  1739. /* Parse up the initial header */
  1740. c = req->header[0];
  1741. while (*c && *c < 33) c++;
  1742. /* First the verb */
  1743. req->verb = c;
  1744. while (*c && (*c > 32)) c++;
  1745. if (*c) {
  1746. *c = '\0';
  1747. c++;
  1748. while (*c && (*c < 33)) c++;
  1749. req->identifier = c;
  1750. while (*c && (*c > 32)) c++;
  1751. if (*c) {
  1752. *c = '\0';
  1753. c++;
  1754. while (*c && (*c < 33)) c++;
  1755. req->endpoint = c;
  1756. while (*c && (*c > 32)) c++;
  1757. if (*c) {
  1758. *c = '\0';
  1759. c++;
  1760. while (*c && (*c < 33)) c++;
  1761. req->version = c;
  1762. while (*c && (*c > 32)) c++;
  1763. while (*c && (*c < 33)) c++;
  1764. while (*c && (*c > 32)) c++;
  1765. *c = '\0';
  1766. }
  1767. }
  1768. }
  1769. ast_debug(1, "Verb: '%s', Identifier: '%s', Endpoint: '%s', Version: '%s'\n",
  1770. req->verb, req->identifier, req->endpoint, req->version);
  1771. ast_debug(1, "%d headers, %d lines\n", req->headers, req->lines);
  1772. if (*c) {
  1773. ast_log(LOG_WARNING, "Odd content, extra stuff left over ('%s')\n", c);
  1774. }
  1775. }
  1776. static int process_sdp(struct mgcp_subchannel *sub, struct mgcp_request *req)
  1777. {
  1778. char *m;
  1779. char *c;
  1780. char *a;
  1781. char host[258];
  1782. int len = 0;
  1783. int portno;
  1784. struct ast_format_cap *peercap;
  1785. int peerNonCodecCapability;
  1786. struct sockaddr_in sin;
  1787. struct ast_sockaddr sin_tmp;
  1788. char *codecs;
  1789. struct ast_hostent ahp; struct hostent *hp;
  1790. int codec, codec_count=0;
  1791. int iterator;
  1792. struct mgcp_endpoint *p = sub->parent;
  1793. struct ast_str *global_buf = ast_str_alloca(64);
  1794. struct ast_str *peer_buf = ast_str_alloca(64);
  1795. struct ast_str *pvt_buf = ast_str_alloca(64);
  1796. /* Get codec and RTP info from SDP */
  1797. m = get_sdp(req, "m");
  1798. c = get_sdp(req, "c");
  1799. if (ast_strlen_zero(m) || ast_strlen_zero(c)) {
  1800. ast_log(LOG_WARNING, "Insufficient information for SDP (m = '%s', c = '%s')\n", m, c);
  1801. return -1;
  1802. }
  1803. if (sscanf(c, "IN IP4 %256s", host) != 1) {
  1804. ast_log(LOG_WARNING, "Invalid host in c= line, '%s'\n", c);
  1805. return -1;
  1806. }
  1807. /* XXX This could block for a long time, and block the main thread! XXX */
  1808. hp = ast_gethostbyname(host, &ahp);
  1809. if (!hp) {
  1810. ast_log(LOG_WARNING, "Unable to lookup host in c= line, '%s'\n", c);
  1811. return -1;
  1812. }
  1813. if (sscanf(m, "audio %30d RTP/AVP %n", &portno, &len) != 1 || !len) {
  1814. ast_log(LOG_WARNING, "Malformed media stream descriptor: %s\n", m);
  1815. return -1;
  1816. }
  1817. sin.sin_family = AF_INET;
  1818. memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
  1819. sin.sin_port = htons(portno);
  1820. ast_sockaddr_from_sin(&sin_tmp, &sin);
  1821. ast_rtp_instance_set_remote_address(sub->rtp, &sin_tmp);
  1822. ast_debug(3, "Peer RTP is at port %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
  1823. /* Scan through the RTP payload types specified in a "m=" line: */
  1824. ast_rtp_codecs_payloads_clear(ast_rtp_instance_get_codecs(sub->rtp), sub->rtp);
  1825. codecs = ast_strdupa(m + len);
  1826. while (!ast_strlen_zero(codecs)) {
  1827. if (sscanf(codecs, "%30d%n", &codec, &len) != 1) {
  1828. if (codec_count) {
  1829. break;
  1830. }
  1831. ast_log(LOG_WARNING, "Error in codec string '%s' at '%s'\n", m, codecs);
  1832. return -1;
  1833. }
  1834. ast_rtp_codecs_payloads_set_m_type(ast_rtp_instance_get_codecs(sub->rtp), sub->rtp, codec);
  1835. codec_count++;
  1836. codecs += len;
  1837. }
  1838. /* Next, scan through each "a=rtpmap:" line, noting each */
  1839. /* specified RTP payload type (with corresponding MIME subtype): */
  1840. sdpLineNum_iterator_init(&iterator);
  1841. while ((a = get_sdp_iterate(&iterator, req, "a"))[0] != '\0') {
  1842. char* mimeSubtype = ast_strdupa(a); /* ensures we have enough space */
  1843. if (sscanf(a, "rtpmap: %30d %127[^/]/", &codec, mimeSubtype) != 2)
  1844. continue;
  1845. /* Note: should really look at the 'freq' and '#chans' params too */
  1846. ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(sub->rtp), sub->rtp, codec, "audio", mimeSubtype, 0);
  1847. }
  1848. /* Now gather all of the codecs that were asked for: */
  1849. if (!(peercap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
  1850. return -1;
  1851. }
  1852. ast_rtp_codecs_payload_formats(ast_rtp_instance_get_codecs(sub->rtp), peercap, &peerNonCodecCapability);
  1853. ast_format_cap_get_compatible(global_capability, peercap, p->cap);
  1854. ast_debug(1, "Capabilities: us - %s, them - %s, combined - %s\n",
  1855. ast_format_cap_get_names(global_capability, &global_buf),
  1856. ast_format_cap_get_names(peercap, &peer_buf),
  1857. ast_format_cap_get_names(p->cap, &pvt_buf));
  1858. ao2_ref(peercap, -1);
  1859. ast_debug(1, "Non-codec capabilities: us - %d, them - %d, combined - %d\n",
  1860. nonCodecCapability, peerNonCodecCapability, p->nonCodecCapability);
  1861. if (!ast_format_cap_count(p->cap)) {
  1862. ast_log(LOG_WARNING, "No compatible codecs!\n");
  1863. return -1;
  1864. }
  1865. return 0;
  1866. }
  1867. static int add_header(struct mgcp_request *req, const char *var, const char *value)
  1868. {
  1869. if (req->len >= sizeof(req->data) - 4) {
  1870. ast_log(LOG_WARNING, "Out of space, can't add anymore\n");
  1871. return -1;
  1872. }
  1873. if (req->lines) {
  1874. ast_log(LOG_WARNING, "Can't add more headers when lines have been added\n");
  1875. return -1;
  1876. }
  1877. req->header[req->headers] = req->data + req->len;
  1878. snprintf(req->header[req->headers], sizeof(req->data) - req->len, "%s: %s\r\n", var, value);
  1879. req->len += strlen(req->header[req->headers]);
  1880. if (req->headers < MGCP_MAX_HEADERS) {
  1881. req->headers++;
  1882. } else {
  1883. ast_log(LOG_WARNING, "Out of header space\n");
  1884. return -1;
  1885. }
  1886. return 0;
  1887. }
  1888. static int add_line(struct mgcp_request *req, char *line)
  1889. {
  1890. if (req->len >= sizeof(req->data) - 4) {
  1891. ast_log(LOG_WARNING, "Out of space, can't add anymore\n");
  1892. return -1;
  1893. }
  1894. if (!req->lines) {
  1895. /* Add extra empty return */
  1896. ast_copy_string(req->data + req->len, "\r\n", sizeof(req->data) - req->len);
  1897. req->len += strlen(req->data + req->len);
  1898. }
  1899. req->line[req->lines] = req->data + req->len;
  1900. snprintf(req->line[req->lines], sizeof(req->data) - req->len, "%s", line);
  1901. req->len += strlen(req->line[req->lines]);
  1902. if (req->lines < MGCP_MAX_LINES) {
  1903. req->lines++;
  1904. } else {
  1905. ast_log(LOG_WARNING, "Out of line space\n");
  1906. return -1;
  1907. }
  1908. return 0;
  1909. }
  1910. static int init_resp(struct mgcp_request *req, char *resp, struct mgcp_request *orig, char *resprest)
  1911. {
  1912. /* Initialize a response */
  1913. if (req->headers || req->len) {
  1914. ast_log(LOG_WARNING, "Request already initialized?!?\n");
  1915. return -1;
  1916. }
  1917. req->header[req->headers] = req->data + req->len;
  1918. snprintf(req->header[req->headers], sizeof(req->data) - req->len, "%s %s %s\r\n", resp, orig->identifier, resprest);
  1919. req->len += strlen(req->header[req->headers]);
  1920. if (req->headers < MGCP_MAX_HEADERS) {
  1921. req->headers++;
  1922. } else {
  1923. ast_log(LOG_WARNING, "Out of header space\n");
  1924. }
  1925. return 0;
  1926. }
  1927. static int init_req(struct mgcp_endpoint *p, struct mgcp_request *req, char *verb, unsigned int oseq)
  1928. {
  1929. /* Initialize a response */
  1930. if (req->headers || req->len) {
  1931. ast_log(LOG_WARNING, "Request already initialized?!?\n");
  1932. return -1;
  1933. }
  1934. req->header[req->headers] = req->data + req->len;
  1935. /* check if we need brackets around the gw name */
  1936. if (p->parent->isnamedottedip) {
  1937. snprintf(req->header[req->headers], sizeof(req->data) - req->len, "%s %u %s@[%s] MGCP 1.0%s\r\n", verb, oseq, p->name, p->parent->name, p->ncs ? " NCS 1.0" : "");
  1938. } else {
  1939. + snprintf(req->header[req->headers], sizeof(req->data) - req->len, "%s %u %s@%s MGCP 1.0%s\r\n", verb, oseq, p->name, p->parent->name, p->ncs ? " NCS 1.0" : "");
  1940. }
  1941. req->len += strlen(req->header[req->headers]);
  1942. if (req->headers < MGCP_MAX_HEADERS) {
  1943. req->headers++;
  1944. } else {
  1945. ast_log(LOG_WARNING, "Out of header space\n");
  1946. }
  1947. return 0;
  1948. }
  1949. static int respprep(struct mgcp_request *resp, struct mgcp_endpoint *p, char *msg, struct mgcp_request *req, char *msgrest)
  1950. {
  1951. memset(resp, 0, sizeof(*resp));
  1952. init_resp(resp, msg, req, msgrest);
  1953. return 0;
  1954. }
  1955. static int reqprep(struct mgcp_request *req, struct mgcp_endpoint *p, char *verb)
  1956. {
  1957. unsigned int oseq;
  1958. memset(req, 0, sizeof(struct mgcp_request));
  1959. ast_mutex_lock(&oseq_lock);
  1960. oseq_global++;
  1961. if (oseq_global > 999999999) {
  1962. oseq_global = 1;
  1963. }
  1964. oseq = oseq_global;
  1965. ast_mutex_unlock(&oseq_lock);
  1966. init_req(p, req, verb, oseq);
  1967. return oseq;
  1968. }
  1969. static int transmit_response(struct mgcp_subchannel *sub, char *msg, struct mgcp_request *req, char *msgrest)
  1970. {
  1971. struct mgcp_request resp;
  1972. struct mgcp_endpoint *p = sub->parent;
  1973. struct mgcp_response *mgr;
  1974. if (!sub) {
  1975. return -1;
  1976. }
  1977. respprep(&resp, p, msg, req, msgrest);
  1978. if (!(mgr = ast_calloc(1, sizeof(*mgr) + resp.len + 1))) {
  1979. return send_response(sub, &resp);
  1980. }
  1981. /* Store MGCP response in case we have to retransmit */
  1982. sscanf(req->identifier, "%30d", &mgr->seqno);
  1983. time(&mgr->whensent);
  1984. mgr->len = resp.len;
  1985. memcpy(mgr->buf, resp.data, resp.len);
  1986. mgr->buf[resp.len] = '\0';
  1987. mgr->next = p->parent->responses;
  1988. p->parent->responses = mgr;
  1989. return send_response(sub, &resp);
  1990. }
  1991. static int add_sdp(struct mgcp_request *resp, struct mgcp_subchannel *sub, struct ast_rtp_instance *rtp)
  1992. {
  1993. int len;
  1994. int codec;
  1995. char costr[80];
  1996. struct sockaddr_in sin;
  1997. struct ast_sockaddr sin_tmp;
  1998. char v[256];
  1999. char s[256];
  2000. char o[256];
  2001. char c[256];
  2002. char t[256];
  2003. char m[256] = "";
  2004. char a[1024] = "";
  2005. int x;
  2006. struct sockaddr_in dest = { 0, };
  2007. struct ast_sockaddr dest_tmp;
  2008. struct mgcp_endpoint *p = sub->parent;
  2009. /* XXX We break with the "recommendation" and send our IP, in order that our
  2010. peer doesn't have to ast_gethostbyname() us XXX */
  2011. len = 0;
  2012. if (!sub->rtp) {
  2013. ast_log(LOG_WARNING, "No way to add SDP without an RTP structure\n");
  2014. return -1;
  2015. }
  2016. ast_rtp_instance_get_local_address(sub->rtp, &sin_tmp);
  2017. ast_sockaddr_to_sin(&sin_tmp, &sin);
  2018. if (rtp) {
  2019. ast_rtp_instance_get_remote_address(sub->rtp, &dest_tmp);
  2020. ast_sockaddr_to_sin(&dest_tmp, &dest);
  2021. } else {
  2022. if (sub->tmpdest.sin_addr.s_addr) {
  2023. dest.sin_addr = sub->tmpdest.sin_addr;
  2024. dest.sin_port = sub->tmpdest.sin_port;
  2025. /* Reset temporary destination */
  2026. memset(&sub->tmpdest, 0, sizeof(sub->tmpdest));
  2027. } else {
  2028. dest.sin_addr = p->parent->ourip;
  2029. dest.sin_port = sin.sin_port;
  2030. }
  2031. }
  2032. ast_debug(1, "We're at %s port %d\n", ast_inet_ntoa(p->parent->ourip), ntohs(sin.sin_port));
  2033. ast_copy_string(v, "v=0\r\n", sizeof(v));
  2034. snprintf(o, sizeof(o), "o=root %d %d IN IP4 %s\r\n", (int)getpid(), (int)getpid(), ast_inet_ntoa(dest.sin_addr));
  2035. ast_copy_string(s, "s=session\r\n", sizeof(s));
  2036. snprintf(c, sizeof(c), "c=IN IP4 %s\r\n", ast_inet_ntoa(dest.sin_addr));
  2037. ast_copy_string(t, "t=0 0\r\n", sizeof(t));
  2038. snprintf(m, sizeof(m), "m=audio %d RTP/AVP", ntohs(dest.sin_port));
  2039. for (x = 0; x < ast_format_cap_count(p->cap); x++) {
  2040. struct ast_format *format = ast_format_cap_get_format(p->cap, x);
  2041. if (ast_format_get_type(format) != AST_MEDIA_TYPE_AUDIO) {
  2042. ao2_ref(format, -1);
  2043. continue;
  2044. }
  2045. ast_debug(1, "Answering with capability %s\n", ast_format_get_name(format));
  2046. codec = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(sub->rtp), 1, format, 0);
  2047. if (codec > -1) {
  2048. snprintf(costr, sizeof(costr), " %d", codec);
  2049. strncat(m, costr, sizeof(m) - strlen(m) - 1);
  2050. snprintf(costr, sizeof(costr), "a=rtpmap:%d %s/8000\r\n", codec, ast_rtp_lookup_mime_subtype2(1, format, 0, 0));
  2051. strncat(a, costr, sizeof(a) - strlen(a) - 1);
  2052. }
  2053. ao2_ref(format, -1);
  2054. }
  2055. for (x = 1LL; x <= AST_RTP_MAX; x <<= 1) {
  2056. if (p->nonCodecCapability & x) {
  2057. ast_debug(1, "Answering with non-codec capability %d\n", (int) x);
  2058. codec = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(sub->rtp), 0, NULL, x);
  2059. if (codec > -1) {
  2060. snprintf(costr, sizeof(costr), " %d", codec);
  2061. strncat(m, costr, sizeof(m) - strlen(m) - 1);
  2062. snprintf(costr, sizeof(costr), "a=rtpmap:%d %s/8000\r\n", codec, ast_rtp_lookup_mime_subtype2(0, NULL, x, 0));
  2063. strncat(a, costr, sizeof(a) - strlen(a) - 1);
  2064. if (x == AST_RTP_DTMF) {
  2065. /* Indicate we support DTMF... Not sure about 16,
  2066. but MSN supports it so dang it, we will too... */
  2067. snprintf(costr, sizeof costr, "a=fmtp:%d 0-16\r\n", codec);
  2068. strncat(a, costr, sizeof(a) - strlen(a) - 1);
  2069. }
  2070. }
  2071. }
  2072. }
  2073. strncat(m, "\r\n", sizeof(m) - strlen(m) - 1);
  2074. len = strlen(v) + strlen(s) + strlen(o) + strlen(c) + strlen(t) + strlen(m) + strlen(a);
  2075. snprintf(costr, sizeof(costr), "%d", len);
  2076. add_line(resp, v);
  2077. add_line(resp, o);
  2078. add_line(resp, s);
  2079. add_line(resp, c);
  2080. add_line(resp, t);
  2081. add_line(resp, m);
  2082. add_line(resp, a);
  2083. return 0;
  2084. }
  2085. static int transmit_modify_with_sdp(struct mgcp_subchannel *sub, struct ast_rtp_instance *rtp, const struct ast_format_cap *codecs)
  2086. {
  2087. struct mgcp_request resp;
  2088. char local[256];
  2089. char tmp[80];
  2090. struct mgcp_endpoint *p = sub->parent;
  2091. int i;
  2092. struct ast_sockaddr sub_tmpdest_tmp;
  2093. unsigned int oseq;
  2094. if (ast_strlen_zero(sub->cxident) && rtp) {
  2095. /* We don't have a CXident yet, store the destination and
  2096. wait a bit */
  2097. ast_rtp_instance_get_remote_address(rtp, &sub_tmpdest_tmp);
  2098. ast_sockaddr_to_sin(&sub_tmpdest_tmp, &sub->tmpdest);
  2099. return 0;
  2100. }
  2101. ast_copy_string(local, "e:on, s:off, p:20", sizeof(local));
  2102. for (i = 0; i < ast_format_cap_count(p->cap); i++) {
  2103. struct ast_format *format = ast_format_cap_get_format(p->cap, i);
  2104. if (ast_format_get_type(format) != AST_MEDIA_TYPE_AUDIO) {
  2105. ao2_ref(format, -1);
  2106. continue;
  2107. }
  2108. snprintf(tmp, sizeof(tmp), ", a:%s", ast_rtp_lookup_mime_subtype2(1, format, 0, 0));
  2109. strncat(local, tmp, sizeof(local) - strlen(local) - 1);
  2110. ao2_ref(format, -1);
  2111. }
  2112. if (sub->gate) {
  2113. if (sub->gate->state == GATE_ALLOCATED || sub->gate->state == GATE_OPEN) {
  2114. snprintf(tmp, sizeof(tmp), ", dq-gi:%x", sub->gate->gateid);
  2115. strncat(local, tmp, sizeof(local) - strlen(local) - 1);
  2116. sub->sdpsent = 1;
  2117. } else {
  2118. /* oops wait */
  2119. ast_debug(1, "Waiting for opened gate...\n");
  2120. sub->sdpsent = 0;
  2121. return 0;
  2122. }
  2123. }
  2124. oseq = reqprep(&resp, p, "MDCX");
  2125. add_header(&resp, "C", sub->callid);
  2126. add_header(&resp, "L", local);
  2127. add_header(&resp, "M", mgcp_cxmodes[sub->cxmode]);
  2128. /* X header should not be sent. kept for compatibility */
  2129. add_header(&resp, "X", sub->txident);
  2130. add_header(&resp, "I", sub->cxident);
  2131. /*add_header(&resp, "S", "");*/
  2132. add_sdp(&resp, sub, rtp);
  2133. /* fill in new fields */
  2134. resp.cmd = MGCP_CMD_MDCX;
  2135. resp.trid = oseq;
  2136. return send_request(p, sub, &resp, oseq);
  2137. }
  2138. static int transmit_connect_with_sdp(struct mgcp_subchannel *sub, struct ast_rtp_instance *rtp)
  2139. {
  2140. struct mgcp_request resp;
  2141. char local[256];
  2142. char tmp[80];
  2143. int i;
  2144. struct mgcp_endpoint *p = sub->parent;
  2145. unsigned int oseq;
  2146. ast_debug(3, "Creating connection for %s@%s-%d in cxmode: %s callid: %s\n",
  2147. p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode], sub->callid);
  2148. ast_copy_string(local, "e:on, s:off, p:20", sizeof(local));
  2149. for (i = 0; i < ast_format_cap_count(p->cap); i++) {
  2150. struct ast_format *format = ast_format_cap_get_format(p->cap, i);
  2151. if (ast_format_get_type(format) != AST_MEDIA_TYPE_AUDIO) {
  2152. ao2_ref(format, -1);
  2153. continue;
  2154. }
  2155. snprintf(tmp, sizeof(tmp), ", a:%s", ast_rtp_lookup_mime_subtype2(1, format, 0, 0));
  2156. strncat(local, tmp, sizeof(local) - strlen(local) - 1);
  2157. ao2_ref(format, -1);
  2158. }
  2159. if (sub->gate) {
  2160. if(sub->gate->state == GATE_ALLOCATED) {
  2161. snprintf(tmp, sizeof(tmp), ", dq-gi:%x", sub->gate->gateid);
  2162. strncat(local, tmp, sizeof(local) - strlen(local) - 1);
  2163. }
  2164. }
  2165. sub->sdpsent = 1;
  2166. oseq = reqprep(&resp, p, "CRCX");
  2167. add_header(&resp, "C", sub->callid);
  2168. add_header(&resp, "L", local);
  2169. add_header(&resp, "M", mgcp_cxmodes[sub->cxmode]);
  2170. /* X header should not be sent. kept for compatibility */
  2171. add_header(&resp, "X", sub->txident);
  2172. /*add_header(&resp, "S", "");*/
  2173. add_sdp(&resp, sub, rtp);
  2174. /* fill in new fields */
  2175. resp.cmd = MGCP_CMD_CRCX;
  2176. resp.trid = oseq;
  2177. return send_request(p, sub, &resp, oseq);
  2178. }
  2179. static int mgcp_pktcgate_remove(struct cops_gate *gate)
  2180. {
  2181. struct mgcp_subchannel *sub = gate->tech_pvt;
  2182. if (!sub) {
  2183. return 1;
  2184. }
  2185. ast_mutex_lock(&sub->lock);
  2186. ast_debug(1, "Pktc: gate 0x%x deleted\n", gate->gateid);
  2187. if (sub->gate->state != GATE_CLOSED && sub->parent->hangupongateremove) {
  2188. sub->gate = NULL;
  2189. if (sub->owner) {
  2190. ast_softhangup(sub->owner, AST_CAUSE_REQUESTED_CHAN_UNAVAIL);
  2191. ast_channel_unlock(sub->owner);
  2192. }
  2193. } else {
  2194. sub->gate = NULL;
  2195. }
  2196. ast_mutex_unlock(&sub->lock);
  2197. return 1;
  2198. }
  2199. static int mgcp_pktcgate_open(struct cops_gate *gate)
  2200. {
  2201. struct mgcp_subchannel *sub = gate->tech_pvt;
  2202. if (!sub) {
  2203. return 1;
  2204. }
  2205. ast_mutex_lock(&sub->lock);
  2206. ast_debug(1, "Pktc: gate 0x%x open\n", gate->gateid);
  2207. if (!sub->sdpsent) transmit_modify_with_sdp(sub, NULL, 0);
  2208. ast_mutex_unlock(&sub->lock);
  2209. return 1;
  2210. }
  2211. static int mgcp_alloc_pktcgate(struct mgcp_subchannel *sub)
  2212. {
  2213. struct mgcp_endpoint *p = sub->parent;
  2214. sub->gate = ast_pktccops_gate_alloc(GATE_SET, NULL, ntohl(p->parent->addr.sin_addr.s_addr),
  2215. 8, 128000, 232, 0, 0, NULL, &mgcp_pktcgate_remove);
  2216. if (!sub->gate) {
  2217. return 0;
  2218. }
  2219. sub->gate->tech_pvt = sub;
  2220. sub->gate->gate_open = &mgcp_pktcgate_open;
  2221. return 1;
  2222. }
  2223. static int transmit_connect(struct mgcp_subchannel *sub)
  2224. {
  2225. struct mgcp_request resp;
  2226. int x;
  2227. char local[256];
  2228. char tmp[80];
  2229. struct ast_format *tmpfmt;
  2230. struct mgcp_endpoint *p = sub->parent;
  2231. unsigned int oseq;
  2232. ast_copy_string(local, "p:20, s:off, e:on", sizeof(local));
  2233. for (x = 0; x < ast_format_cap_count(p->cap); x++) {
  2234. tmpfmt = ast_format_cap_get_format(p->cap, x);
  2235. snprintf(tmp, sizeof(tmp), ", a:%s", ast_rtp_lookup_mime_subtype2(1, tmpfmt, 0, 0));
  2236. strncat(local, tmp, sizeof(local) - strlen(local) - 1);
  2237. ao2_ref(tmpfmt, -1);
  2238. }
  2239. ast_debug(3, "Creating connection for %s@%s-%d in cxmode: %s callid: %s\n",
  2240. p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode], sub->callid);
  2241. sub->sdpsent = 0;
  2242. oseq = reqprep(&resp, p, "CRCX");
  2243. add_header(&resp, "C", sub->callid);
  2244. add_header(&resp, "L", local);
  2245. add_header(&resp, "M", "inactive");
  2246. /* X header should not be sent. kept for compatibility */
  2247. add_header(&resp, "X", sub->txident);
  2248. /*add_header(&resp, "S", "");*/
  2249. /* fill in new fields */
  2250. resp.cmd = MGCP_CMD_CRCX;
  2251. resp.trid = oseq;
  2252. return send_request(p, sub, &resp, oseq);
  2253. }
  2254. static int transmit_notify_request(struct mgcp_subchannel *sub, char *tone)
  2255. {
  2256. struct mgcp_request resp;
  2257. struct mgcp_endpoint *p = sub->parent;
  2258. unsigned int oseq;
  2259. ast_debug(3, "MGCP Asked to indicate tone: %s on %s@%s-%d in cxmode: %s\n",
  2260. tone, p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode]);
  2261. ast_copy_string(p->curtone, tone, sizeof(p->curtone));
  2262. oseq = reqprep(&resp, p, "RQNT");
  2263. add_header(&resp, "X", p->rqnt_ident);
  2264. switch (p->hookstate) {
  2265. case MGCP_ONHOOK:
  2266. add_header(&resp, "R", "L/hd(N)");
  2267. break;
  2268. case MGCP_OFFHOOK:
  2269. add_header_offhook(sub, &resp, tone);
  2270. break;
  2271. }
  2272. if (!ast_strlen_zero(tone)) {
  2273. add_header(&resp, "S", tone);
  2274. }
  2275. /* fill in new fields */
  2276. resp.cmd = MGCP_CMD_RQNT;
  2277. resp.trid = oseq;
  2278. return send_request(p, NULL, &resp, oseq);
  2279. }
  2280. static int transmit_notify_request_with_callerid(struct mgcp_subchannel *sub, char *tone, char *callernum, char *callername)
  2281. {
  2282. struct mgcp_request resp;
  2283. char tone2[256];
  2284. char *l, *n;
  2285. struct timeval t = ast_tvnow();
  2286. struct ast_tm tm;
  2287. struct mgcp_endpoint *p = sub->parent;
  2288. unsigned int oseq;
  2289. ast_localtime(&t, &tm, NULL);
  2290. n = callername;
  2291. l = callernum;
  2292. if (!n)
  2293. n = "";
  2294. if (!l)
  2295. l = "";
  2296. /* Keep track of last callerid for blacklist and callreturn */
  2297. ast_copy_string(p->lastcallerid, l, sizeof(p->lastcallerid));
  2298. snprintf(tone2, sizeof(tone2), "%s,L/ci(%02d/%02d/%02d/%02d,%s,%s)", tone,
  2299. tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, l, n);
  2300. ast_copy_string(p->curtone, tone, sizeof(p->curtone));
  2301. oseq = reqprep(&resp, p, "RQNT");
  2302. add_header(&resp, "X", p->rqnt_ident);
  2303. switch (p->hookstate) {
  2304. case MGCP_ONHOOK:
  2305. add_header(&resp, "R", "L/hd(N)");
  2306. break;
  2307. case MGCP_OFFHOOK:
  2308. add_header_offhook(sub, &resp, tone);
  2309. break;
  2310. }
  2311. if (!ast_strlen_zero(tone2)) {
  2312. add_header(&resp, "S", tone2);
  2313. }
  2314. ast_debug(3, "MGCP Asked to indicate tone: %s on %s@%s-%d in cxmode: %s\n",
  2315. tone2, p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode]);
  2316. /* fill in new fields */
  2317. resp.cmd = MGCP_CMD_RQNT;
  2318. resp.trid = oseq;
  2319. return send_request(p, NULL, &resp, oseq);
  2320. }
  2321. static int transmit_modify_request(struct mgcp_subchannel *sub)
  2322. {
  2323. struct mgcp_request resp;
  2324. struct mgcp_endpoint *p = sub->parent;
  2325. int i;
  2326. int fc = 1;
  2327. char local[256];
  2328. char tmp[80];
  2329. unsigned int oseq;
  2330. if (ast_strlen_zero(sub->cxident)) {
  2331. /* We don't have a CXident yet, store the destination and
  2332. wait a bit */
  2333. return 0;
  2334. }
  2335. ast_debug(3, "Modified %s@%s-%d with new mode: %s on callid: %s\n",
  2336. p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode], sub->callid);
  2337. ast_copy_string(local, "", sizeof(local));
  2338. for (i = 0; i < ast_format_cap_count(p->cap); i++) {
  2339. struct ast_format *format = ast_format_cap_get_format(p->cap, i);
  2340. if (p->ncs && !fc) {
  2341. ast_format_cap_remove_by_type(p->cap, AST_MEDIA_TYPE_UNKNOWN);
  2342. ast_format_cap_append(p->cap, format, 0); /* sb5120e bug */
  2343. ao2_ref(format, -1);
  2344. break;
  2345. } else {
  2346. fc = 0;
  2347. snprintf(tmp, sizeof(tmp), ", a:%s", ast_rtp_lookup_mime_subtype2(1, format, 0, 0));
  2348. }
  2349. strncat(local, tmp, sizeof(local) - strlen(local) - 1);
  2350. ao2_ref(format, -1);
  2351. }
  2352. if (!sub->sdpsent) {
  2353. if (sub->gate) {
  2354. if (sub->gate->state == GATE_ALLOCATED || sub->gate->state == GATE_OPEN) {
  2355. snprintf(tmp, sizeof(tmp), ", dq-gi:%x", sub->gate->gateid);
  2356. strncat(local, tmp, sizeof(local) - strlen(local) - 1);
  2357. } else {
  2358. /* we still don't have gateid wait */
  2359. return 0;
  2360. }
  2361. }
  2362. }
  2363. oseq = reqprep(&resp, p, "MDCX");
  2364. add_header(&resp, "C", sub->callid);
  2365. if (!sub->sdpsent) {
  2366. add_header(&resp, "L", local);
  2367. }
  2368. add_header(&resp, "M", mgcp_cxmodes[sub->cxmode]);
  2369. /* X header should not be sent. kept for compatibility */
  2370. add_header(&resp, "X", sub->txident);
  2371. add_header(&resp, "I", sub->cxident);
  2372. switch (sub->parent->hookstate) {
  2373. case MGCP_ONHOOK:
  2374. add_header(&resp, "R", "L/hd(N)");
  2375. break;
  2376. case MGCP_OFFHOOK:
  2377. add_header_offhook(sub, &resp, "");
  2378. break;
  2379. }
  2380. if (!sub->sdpsent) {
  2381. add_sdp(&resp, sub, NULL);
  2382. sub->sdpsent = 1;
  2383. }
  2384. /* fill in new fields */
  2385. resp.cmd = MGCP_CMD_MDCX;
  2386. resp.trid = oseq;
  2387. return send_request(p, sub, &resp, oseq);
  2388. }
  2389. static void add_header_offhook(struct mgcp_subchannel *sub, struct mgcp_request *resp, char *tone)
  2390. {
  2391. struct mgcp_endpoint *p = sub->parent;
  2392. char tone_indicate_end = 0;
  2393. /* We also should check the tone to indicate, because it have no sense
  2394. to request notify D/[0-9#*] (dtmf keys) if we are sending congestion
  2395. tone for example G/cg */
  2396. if (p && (!strcasecmp(tone, (p->ncs ? "L/ro" : "G/cg")))) {
  2397. tone_indicate_end = 1;
  2398. }
  2399. if (p && p->sub && p->sub->owner &&
  2400. ast_channel_state(p->sub->owner) >= AST_STATE_RINGING &&
  2401. (p->dtmfmode & (MGCP_DTMF_INBAND | MGCP_DTMF_HYBRID))) {
  2402. add_header(resp, "R", "L/hu(N),L/hf(N)");
  2403. } else if (!tone_indicate_end){
  2404. add_header(resp, "R", (p->ncs ? "L/hu(N),L/hf(N),L/[0-9#*](N)" : "L/hu(N),L/hf(N),D/[0-9#*](N)"));
  2405. } else {
  2406. ast_debug(1, "We don't want more digits if we will end the call\n");
  2407. add_header(resp, "R", "L/hu(N),L/hf(N)");
  2408. }
  2409. }
  2410. static int transmit_audit_endpoint(struct mgcp_endpoint *p)
  2411. {
  2412. struct mgcp_request resp;
  2413. unsigned int oseq;
  2414. oseq = reqprep(&resp, p, "AUEP");
  2415. /* removed unknown param VS */
  2416. /*add_header(&resp, "F", "A,R,D,S,X,N,I,T,O,ES,E,MD,M");*/
  2417. add_header(&resp, "F", "A");
  2418. /* fill in new fields */
  2419. resp.cmd = MGCP_CMD_AUEP;
  2420. resp.trid = oseq;
  2421. return send_request(p, NULL, &resp, oseq);
  2422. }
  2423. static int transmit_connection_del(struct mgcp_subchannel *sub)
  2424. {
  2425. struct mgcp_endpoint *p = sub->parent;
  2426. struct mgcp_request resp;
  2427. unsigned int oseq;
  2428. ast_debug(3, "Delete connection %s %s@%s-%d with new mode: %s on callid: %s\n",
  2429. sub->cxident, p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode], sub->callid);
  2430. oseq = reqprep(&resp, p, "DLCX");
  2431. /* check if call id is avail */
  2432. if (sub->callid[0])
  2433. add_header(&resp, "C", sub->callid);
  2434. /* X header should not be sent. kept for compatibility */
  2435. add_header(&resp, "X", sub->txident);
  2436. /* check if cxident is avail */
  2437. if (sub->cxident[0])
  2438. add_header(&resp, "I", sub->cxident);
  2439. /* fill in new fields */
  2440. resp.cmd = MGCP_CMD_DLCX;
  2441. resp.trid = oseq;
  2442. return send_request(p, sub, &resp, oseq);
  2443. }
  2444. static int transmit_connection_del_w_params(struct mgcp_endpoint *p, char *callid, char *cxident)
  2445. {
  2446. struct mgcp_request resp;
  2447. unsigned int oseq;
  2448. ast_debug(3, "Delete connection %s %s@%s on callid: %s\n",
  2449. cxident ? cxident : "", p->name, p->parent->name, callid ? callid : "");
  2450. oseq = reqprep(&resp, p, "DLCX");
  2451. /* check if call id is avail */
  2452. if (callid && *callid)
  2453. add_header(&resp, "C", callid);
  2454. /* check if cxident is avail */
  2455. if (cxident && *cxident)
  2456. add_header(&resp, "I", cxident);
  2457. /* fill in new fields */
  2458. resp.cmd = MGCP_CMD_DLCX;
  2459. resp.trid = oseq;
  2460. return send_request(p, p->sub, &resp, oseq);
  2461. }
  2462. /*! \brief dump_cmd_queues: (SC:) cleanup pending commands */
  2463. static void dump_cmd_queues(struct mgcp_endpoint *p, struct mgcp_subchannel *sub)
  2464. {
  2465. struct mgcp_request *t, *q;
  2466. if (p) {
  2467. ast_mutex_lock(&p->rqnt_queue_lock);
  2468. for (q = p->rqnt_queue; q; t = q->next, ast_free(q), q=t);
  2469. p->rqnt_queue = NULL;
  2470. ast_mutex_unlock(&p->rqnt_queue_lock);
  2471. ast_mutex_lock(&p->cmd_queue_lock);
  2472. for (q = p->cmd_queue; q; t = q->next, ast_free(q), q=t);
  2473. p->cmd_queue = NULL;
  2474. ast_mutex_unlock(&p->cmd_queue_lock);
  2475. ast_mutex_lock(&p->sub->cx_queue_lock);
  2476. for (q = p->sub->cx_queue; q; t = q->next, ast_free(q), q=t);
  2477. p->sub->cx_queue = NULL;
  2478. ast_mutex_unlock(&p->sub->cx_queue_lock);
  2479. ast_mutex_lock(&p->sub->next->cx_queue_lock);
  2480. for (q = p->sub->next->cx_queue; q; t = q->next, ast_free(q), q=t);
  2481. p->sub->next->cx_queue = NULL;
  2482. ast_mutex_unlock(&p->sub->next->cx_queue_lock);
  2483. } else if (sub) {
  2484. ast_mutex_lock(&sub->cx_queue_lock);
  2485. for (q = sub->cx_queue; q; t = q->next, ast_free(q), q=t);
  2486. sub->cx_queue = NULL;
  2487. ast_mutex_unlock(&sub->cx_queue_lock);
  2488. }
  2489. }
  2490. /*! \brief find_command: (SC:) remove command transaction from queue */
  2491. static struct mgcp_request *find_command(struct mgcp_endpoint *p, struct mgcp_subchannel *sub,
  2492. struct mgcp_request **queue, ast_mutex_t *l, int ident)
  2493. {
  2494. struct mgcp_request *prev, *req;
  2495. ast_mutex_lock(l);
  2496. for (prev = NULL, req = *queue; req; prev = req, req = req->next) {
  2497. if (req->trid == ident) {
  2498. /* remove from queue */
  2499. if (!prev)
  2500. *queue = req->next;
  2501. else
  2502. prev->next = req->next;
  2503. /* send next pending command */
  2504. if (*queue) {
  2505. ast_debug(1, "Posting Queued Request:\n%s to %s:%d\n", (*queue)->data,
  2506. ast_inet_ntoa(p->parent->addr.sin_addr), ntohs(p->parent->addr.sin_port));
  2507. mgcp_postrequest(p, sub, (*queue)->data, (*queue)->len, (*queue)->trid);
  2508. }
  2509. break;
  2510. }
  2511. }
  2512. ast_mutex_unlock(l);
  2513. return req;
  2514. }
  2515. /* modified for new transport mechanism */
  2516. static void handle_response(struct mgcp_endpoint *p, struct mgcp_subchannel *sub,
  2517. int result, unsigned int ident, struct mgcp_request *resp)
  2518. {
  2519. char *c;
  2520. struct mgcp_request *req;
  2521. struct mgcp_gateway *gw = p->parent;
  2522. if (result < 200) {
  2523. /* provisional response */
  2524. return;
  2525. }
  2526. if (p->slowsequence)
  2527. req = find_command(p, sub, &p->cmd_queue, &p->cmd_queue_lock, ident);
  2528. else if (sub)
  2529. req = find_command(p, sub, &sub->cx_queue, &sub->cx_queue_lock, ident);
  2530. else if (!(req = find_command(p, sub, &p->rqnt_queue, &p->rqnt_queue_lock, ident)))
  2531. req = find_command(p, sub, &p->cmd_queue, &p->cmd_queue_lock, ident);
  2532. if (!req) {
  2533. ast_verb(3, "No command found on [%s] for transaction %u. Ignoring...\n",
  2534. gw->name, ident);
  2535. return;
  2536. }
  2537. if (p && (result >= 400) && (result <= 599)) {
  2538. switch (result) {
  2539. case 401:
  2540. p->hookstate = MGCP_OFFHOOK;
  2541. break;
  2542. case 402:
  2543. p->hookstate = MGCP_ONHOOK;
  2544. break;
  2545. case 406:
  2546. ast_log(LOG_NOTICE, "Transaction %u timed out\n", ident);
  2547. break;
  2548. case 407:
  2549. ast_log(LOG_NOTICE, "Transaction %u aborted\n", ident);
  2550. break;
  2551. }
  2552. if (sub) {
  2553. if (!sub->cxident[0] && (req->cmd == MGCP_CMD_CRCX)) {
  2554. ast_log(LOG_NOTICE, "DLCX for all connections on %s due to error %d\n", gw->name, result);
  2555. transmit_connection_del(sub);
  2556. }
  2557. if (sub->owner) {
  2558. ast_log(LOG_NOTICE, "Terminating on result %d from %s@%s-%d\n",
  2559. result, p->name, p->parent->name, sub ? sub->id:-1);
  2560. mgcp_queue_hangup(sub);
  2561. }
  2562. } else {
  2563. if (p->sub->next->owner) {
  2564. ast_log(LOG_NOTICE, "Terminating on result %d from %s@%s-%d\n",
  2565. result, p->name, p->parent->name, sub ? sub->id:-1);
  2566. mgcp_queue_hangup(p->sub);
  2567. }
  2568. if (p->sub->owner) {
  2569. ast_log(LOG_NOTICE, "Terminating on result %d from %s@%s-%d\n",
  2570. result, p->name, p->parent->name, sub ? sub->id:-1);
  2571. mgcp_queue_hangup(p->sub);
  2572. }
  2573. dump_cmd_queues(p, NULL);
  2574. }
  2575. }
  2576. if (resp) {
  2577. /* responseAck: */
  2578. if (result == 200 && (req->cmd == MGCP_CMD_CRCX || req->cmd == MGCP_CMD_MDCX)) {
  2579. if (sub) {
  2580. transmit_response(sub, "000", resp, "OK");
  2581. if (sub->owner && ast_channel_state(sub->owner) == AST_STATE_RINGING) {
  2582. ast_queue_control(sub->owner, AST_CONTROL_RINGING);
  2583. }
  2584. }
  2585. }
  2586. if (req->cmd == MGCP_CMD_CRCX) {
  2587. if ((c = get_header(resp, "I"))) {
  2588. if (!ast_strlen_zero(c) && sub) {
  2589. /* if we are hanging up do not process this conn. */
  2590. if (sub->owner) {
  2591. if (!ast_strlen_zero(sub->cxident)) {
  2592. if (strcasecmp(c, sub->cxident)) {
  2593. ast_log(LOG_WARNING, "Subchannel already has a cxident. sub->cxident: %s requested %s\n", sub->cxident, c);
  2594. }
  2595. }
  2596. ast_copy_string(sub->cxident, c, sizeof(sub->cxident));
  2597. if (sub->tmpdest.sin_addr.s_addr) {
  2598. transmit_modify_with_sdp(sub, NULL, 0);
  2599. }
  2600. } else {
  2601. /* XXX delete this one
  2602. callid and conn id may already be lost.
  2603. so the following del conn may have a side effect of
  2604. cleaning up the next subchannel */
  2605. transmit_connection_del(sub);
  2606. }
  2607. }
  2608. }
  2609. }
  2610. if (req->cmd == MGCP_CMD_AUEP) {
  2611. /* check stale connection ids */
  2612. if ((c = get_header(resp, "I"))) {
  2613. char *v, *n;
  2614. int len;
  2615. while ((v = get_csv(c, &len, &n))) {
  2616. if (len) {
  2617. if (strncasecmp(v, p->sub->cxident, len) &&
  2618. strncasecmp(v, p->sub->next->cxident, len)) {
  2619. /* connection id not found. delete it */
  2620. char cxident[80] = "";
  2621. if (len > (sizeof(cxident) - 1))
  2622. len = sizeof(cxident) - 1;
  2623. ast_copy_string(cxident, v, len);
  2624. ast_verb(3, "Non existing connection id %s on %s@%s \n",
  2625. cxident, p->name, gw->name);
  2626. transmit_connection_del_w_params(p, NULL, cxident);
  2627. }
  2628. }
  2629. c = n;
  2630. }
  2631. }
  2632. /* Try to determine the hookstate returned from an audit endpoint command */
  2633. if ((c = get_header(resp, "ES"))) {
  2634. if (!ast_strlen_zero(c)) {
  2635. if (strstr(c, "hu")) {
  2636. if (p->hookstate != MGCP_ONHOOK) {
  2637. /* XXX cleanup if we think we are offhook XXX */
  2638. if ((p->sub->owner || p->sub->next->owner ) &&
  2639. p->hookstate == MGCP_OFFHOOK)
  2640. mgcp_queue_hangup(sub);
  2641. p->hookstate = MGCP_ONHOOK;
  2642. /* update the requested events according to the new hookstate */
  2643. transmit_notify_request(p->sub, "");
  2644. ast_verb(3, "Setting hookstate of %s@%s to ONHOOK\n", p->name, gw->name);
  2645. }
  2646. } else if (strstr(c, "hd")) {
  2647. if (p->hookstate != MGCP_OFFHOOK) {
  2648. p->hookstate = MGCP_OFFHOOK;
  2649. /* update the requested events according to the new hookstate */
  2650. transmit_notify_request(p->sub, "");
  2651. ast_verb(3, "Setting hookstate of %s@%s to OFFHOOK\n", p->name, gw->name);
  2652. }
  2653. }
  2654. }
  2655. }
  2656. }
  2657. if (resp && resp->lines) {
  2658. /* do not process sdp if we are hanging up. this may be a late response */
  2659. if (sub && sub->owner) {
  2660. if (!sub->rtp)
  2661. start_rtp(sub);
  2662. if (sub->rtp)
  2663. process_sdp(sub, resp);
  2664. }
  2665. }
  2666. }
  2667. ast_free(req);
  2668. }
  2669. static void start_rtp(struct mgcp_subchannel *sub)
  2670. {
  2671. struct ast_sockaddr bindaddr_tmp;
  2672. ast_mutex_lock(&sub->lock);
  2673. /* check again to be on the safe side */
  2674. if (sub->rtp) {
  2675. ast_rtp_instance_destroy(sub->rtp);
  2676. sub->rtp = NULL;
  2677. }
  2678. /* Allocate the RTP now */
  2679. ast_sockaddr_from_sin(&bindaddr_tmp, &bindaddr);
  2680. sub->rtp = ast_rtp_instance_new("asterisk", sched, &bindaddr_tmp, NULL);
  2681. if (sub->rtp && sub->owner)
  2682. ast_channel_set_fd(sub->owner, 0, ast_rtp_instance_fd(sub->rtp, 0));
  2683. if (sub->rtp) {
  2684. ast_rtp_instance_set_qos(sub->rtp, qos.tos_audio, qos.cos_audio, "MGCP RTP");
  2685. ast_rtp_instance_set_prop(sub->rtp, AST_RTP_PROPERTY_NAT, sub->nat);
  2686. }
  2687. /* Make a call*ID */
  2688. snprintf(sub->callid, sizeof(sub->callid), "%08lx%s", (unsigned long)ast_random(), sub->txident);
  2689. /* Transmit the connection create */
  2690. if(!sub->parent->pktcgatealloc) {
  2691. transmit_connect_with_sdp(sub, NULL);
  2692. } else {
  2693. transmit_connect(sub);
  2694. sub->gate = NULL;
  2695. if(!mgcp_alloc_pktcgate(sub))
  2696. mgcp_queue_hangup(sub);
  2697. }
  2698. ast_mutex_unlock(&sub->lock);
  2699. }
  2700. static void *mgcp_ss(void *data)
  2701. {
  2702. struct ast_channel *chan = data;
  2703. struct mgcp_subchannel *sub = ast_channel_tech_pvt(chan);
  2704. struct mgcp_endpoint *p = sub->parent;
  2705. /* char exten[AST_MAX_EXTENSION] = ""; */
  2706. int len = 0;
  2707. int timeout = firstdigittimeout;
  2708. int res= 0;
  2709. int getforward = 0;
  2710. int loop_pause = 100;
  2711. RAII_VAR(struct ast_features_pickup_config *, pickup_cfg, NULL, ao2_cleanup);
  2712. const char *pickupexten;
  2713. len = strlen(p->dtmf_buf);
  2714. ast_channel_lock(chan);
  2715. pickup_cfg = ast_get_chan_features_pickup_config(chan);
  2716. if (!pickup_cfg) {
  2717. ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension\n");
  2718. pickupexten = "";
  2719. } else {
  2720. pickupexten = ast_strdupa(pickup_cfg->pickupexten);
  2721. }
  2722. ast_channel_unlock(chan);
  2723. while (len < AST_MAX_EXTENSION - 1) {
  2724. ast_debug(1, "Dtmf buffer '%s' for '%s@%s'\n", p->dtmf_buf, p->name, p->parent->name);
  2725. res = 1; /* Assume that we will get a digit */
  2726. while (strlen(p->dtmf_buf) == len) {
  2727. ast_safe_sleep(chan, loop_pause);
  2728. timeout -= loop_pause;
  2729. if (timeout <= 0){
  2730. res = 0;
  2731. break;
  2732. }
  2733. res = 1;
  2734. }
  2735. timeout = 0;
  2736. len = strlen(p->dtmf_buf);
  2737. if (!ast_ignore_pattern(ast_channel_context(chan), p->dtmf_buf)) {
  2738. /*res = tone_zone_play_tone(p->subs[index].zfd, -1);*/
  2739. ast_indicate(chan, -1);
  2740. } else {
  2741. /* XXX Redundant? We should already be playing dialtone */
  2742. /*tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALTONE);*/
  2743. transmit_notify_request(sub, "L/dl");
  2744. }
  2745. if (ast_exists_extension(chan, ast_channel_context(chan), p->dtmf_buf, 1, p->cid_num)) {
  2746. if (!res || !ast_matchmore_extension(chan, ast_channel_context(chan), p->dtmf_buf, 1, p->cid_num)) {
  2747. if (getforward) {
  2748. /* Record this as the forwarding extension */
  2749. ast_copy_string(p->call_forward, p->dtmf_buf, sizeof(p->call_forward));
  2750. ast_verb(3, "Setting call forward to '%s' on channel %s\n",
  2751. p->call_forward, ast_channel_name(chan));
  2752. /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALRECALL);*/
  2753. transmit_notify_request(sub, "L/sl");
  2754. if (res)
  2755. break;
  2756. usleep(500000);
  2757. /*res = tone_zone_play_tone(p->subs[index].zfd, -1);*/
  2758. ast_indicate(chan, -1);
  2759. sleep(1);
  2760. memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
  2761. /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALTONE);*/
  2762. transmit_notify_request(sub, "L/dl");
  2763. len = 0;
  2764. getforward = 0;
  2765. } else {
  2766. /*res = tone_zone_play_tone(p->subs[index].zfd, -1);*/
  2767. ast_indicate(chan, -1);
  2768. ast_channel_lock(chan);
  2769. ast_channel_exten_set(chan, p->dtmf_buf);
  2770. ast_channel_dialed(chan)->number.str = ast_strdup(p->dtmf_buf);
  2771. memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
  2772. ast_set_callerid(chan,
  2773. p->hidecallerid ? "" : p->cid_num,
  2774. p->hidecallerid ? "" : p->cid_name,
  2775. ast_channel_caller(chan)->ani.number.valid ? NULL : p->cid_num);
  2776. ast_setstate(chan, AST_STATE_RING);
  2777. ast_channel_unlock(chan);
  2778. if (p->dtmfmode & MGCP_DTMF_HYBRID) {
  2779. p->dtmfmode |= MGCP_DTMF_INBAND;
  2780. ast_indicate(chan, -1);
  2781. }
  2782. res = ast_pbx_run(chan);
  2783. if (res) {
  2784. ast_log(LOG_WARNING, "PBX exited non-zero\n");
  2785. /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_CONGESTION);*/
  2786. /*transmit_notify_request(p, "nbz", 1);*/
  2787. transmit_notify_request(sub, p->ncs ? "L/cg" : "G/cg");
  2788. }
  2789. return NULL;
  2790. }
  2791. } else {
  2792. /* It's a match, but they just typed a digit, and there is an ambiguous match,
  2793. so just set the timeout to matchdigittimeout and wait some more */
  2794. timeout = matchdigittimeout;
  2795. }
  2796. } else if (res == 0) {
  2797. ast_debug(1, "not enough digits (and no ambiguous match)...\n");
  2798. /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_CONGESTION);*/
  2799. transmit_notify_request(sub, p->ncs ? "L/cg" : "G/cg");
  2800. /*dahdi_wait_event(p->subs[index].zfd);*/
  2801. ast_hangup(chan);
  2802. memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
  2803. return NULL;
  2804. } else if (p->hascallwaiting && p->callwaiting && !strcmp(p->dtmf_buf, "*70")) {
  2805. ast_verb(3, "Disabling call waiting on %s\n", ast_channel_name(chan));
  2806. /* Disable call waiting if enabled */
  2807. p->callwaiting = 0;
  2808. /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALRECALL);*/
  2809. transmit_notify_request(sub, "L/sl");
  2810. len = 0;
  2811. memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
  2812. timeout = firstdigittimeout;
  2813. } else if (!strcmp(p->dtmf_buf, pickupexten)) {
  2814. /* Scan all channels and see if any there
  2815. * ringing channqels with that have call groups
  2816. * that equal this channels pickup group
  2817. */
  2818. if (ast_pickup_call(chan)) {
  2819. ast_log(LOG_WARNING, "No call pickup possible...\n");
  2820. /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_CONGESTION);*/
  2821. transmit_notify_request(sub, p->ncs ? "L/cg" : "G/cg");
  2822. }
  2823. memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
  2824. ast_hangup(chan);
  2825. return NULL;
  2826. } else if (!p->hidecallerid && !strcmp(p->dtmf_buf, "*67")) {
  2827. ast_verb(3, "Disabling Caller*ID on %s\n", ast_channel_name(chan));
  2828. /* Disable Caller*ID if enabled */
  2829. p->hidecallerid = 1;
  2830. ast_set_callerid(chan, "", "", NULL);
  2831. /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALRECALL);*/
  2832. transmit_notify_request(sub, "L/sl");
  2833. len = 0;
  2834. memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
  2835. timeout = firstdigittimeout;
  2836. } else if (p->callreturn && !strcmp(p->dtmf_buf, "*69")) {
  2837. res = 0;
  2838. if (!ast_strlen_zero(p->lastcallerid)) {
  2839. res = ast_say_digit_str(chan, p->lastcallerid, "", ast_channel_language(chan));
  2840. }
  2841. if (!res)
  2842. /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALRECALL);*/
  2843. transmit_notify_request(sub, "L/sl");
  2844. break;
  2845. } else if (!strcmp(p->dtmf_buf, "*78")) {
  2846. /* Do not disturb */
  2847. ast_verb(3, "Enabled DND on channel %s\n", ast_channel_name(chan));
  2848. /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALRECALL);*/
  2849. transmit_notify_request(sub, "L/sl");
  2850. p->dnd = 1;
  2851. getforward = 0;
  2852. memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
  2853. len = 0;
  2854. } else if (!strcmp(p->dtmf_buf, "*79")) {
  2855. /* Do not disturb */
  2856. ast_verb(3, "Disabled DND on channel %s\n", ast_channel_name(chan));
  2857. /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALRECALL);*/
  2858. transmit_notify_request(sub, "L/sl");
  2859. p->dnd = 0;
  2860. getforward = 0;
  2861. memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
  2862. len = 0;
  2863. } else if (p->cancallforward && !strcmp(p->dtmf_buf, "*72")) {
  2864. /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALRECALL);*/
  2865. transmit_notify_request(sub, "L/sl");
  2866. getforward = 1;
  2867. memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
  2868. len = 0;
  2869. } else if (p->cancallforward && !strcmp(p->dtmf_buf, "*73")) {
  2870. ast_verb(3, "Cancelling call forwarding on channel %s\n", ast_channel_name(chan));
  2871. /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALRECALL);*/
  2872. transmit_notify_request(sub, "L/sl");
  2873. memset(p->call_forward, 0, sizeof(p->call_forward));
  2874. getforward = 0;
  2875. memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
  2876. len = 0;
  2877. } else if (ast_parking_provider_registered() && ast_parking_is_exten_park(ast_channel_context(chan), p->dtmf_buf) &&
  2878. sub->next->owner) {
  2879. RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
  2880. /* This is a three way call, the main call being a real channel,
  2881. and we're parking the first call. */
  2882. ast_channel_lock(chan);
  2883. bridge_channel = ast_channel_get_bridge_channel(chan);
  2884. ast_channel_unlock(chan);
  2885. if (bridge_channel && !ast_parking_blind_transfer_park(bridge_channel, ast_channel_context(chan), p->dtmf_buf, NULL, NULL)) {
  2886. ast_verb(3, "Parking call to '%s'\n", ast_channel_name(chan));
  2887. }
  2888. break;
  2889. } else if (!ast_strlen_zero(p->lastcallerid) && !strcmp(p->dtmf_buf, "*60")) {
  2890. ast_verb(3, "Blacklisting number %s\n", p->lastcallerid);
  2891. res = ast_db_put("blacklist", p->lastcallerid, "1");
  2892. if (!res) {
  2893. /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALRECALL);*/
  2894. transmit_notify_request(sub, "L/sl");
  2895. memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
  2896. len = 0;
  2897. }
  2898. } else if (p->hidecallerid && !strcmp(p->dtmf_buf, "*82")) {
  2899. ast_verb(3, "Enabling Caller*ID on %s\n", ast_channel_name(chan));
  2900. /* Enable Caller*ID if enabled */
  2901. p->hidecallerid = 0;
  2902. ast_set_callerid(chan, p->cid_num, p->cid_name, NULL);
  2903. /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALRECALL);*/
  2904. transmit_notify_request(sub, "L/sl");
  2905. len = 0;
  2906. memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
  2907. timeout = firstdigittimeout;
  2908. } else if (!ast_canmatch_extension(chan, ast_channel_context(chan), p->dtmf_buf, 1,
  2909. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))
  2910. && ((p->dtmf_buf[0] != '*') || (strlen(p->dtmf_buf) > 2))) {
  2911. ast_debug(1, "Can't match %s from '%s' in context %s\n", p->dtmf_buf,
  2912. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, "<Unknown Caller>"),
  2913. ast_channel_context(chan));
  2914. break;
  2915. }
  2916. if (!timeout)
  2917. timeout = gendigittimeout;
  2918. if (len && !ast_ignore_pattern(ast_channel_context(chan), p->dtmf_buf))
  2919. /*tone_zone_play_tone(p->subs[index].zfd, -1);*/
  2920. ast_indicate(chan, -1);
  2921. }
  2922. #if 0
  2923. for (;;) {
  2924. res = ast_waitfordigit(chan, to);
  2925. if (!res) {
  2926. ast_debug(1, "Timeout...\n");
  2927. break;
  2928. }
  2929. if (res < 0) {
  2930. ast_debug(1, "Got hangup...\n");
  2931. ast_hangup(chan);
  2932. break;
  2933. }
  2934. exten[pos++] = res;
  2935. if (!ast_ignore_pattern(chan->context, exten))
  2936. ast_indicate(chan, -1);
  2937. if (ast_matchmore_extension(chan, chan->context, exten, 1, chan->callerid)) {
  2938. if (ast_exists_extension(chan, chan->context, exten, 1, chan->callerid))
  2939. to = 3000;
  2940. else
  2941. to = 8000;
  2942. } else
  2943. break;
  2944. }
  2945. if (ast_exists_extension(chan, chan->context, exten, 1, chan->callerid)) {
  2946. ast_copy_string(chan->exten, exten, sizeof(chan->exten)1);
  2947. if (!p->rtp) {
  2948. start_rtp(p);
  2949. }
  2950. ast_setstate(chan, AST_STATE_RING);
  2951. chan->rings = 1;
  2952. if (ast_pbx_run(chan)) {
  2953. ast_log(LOG_WARNING, "Unable to launch PBX on %s\n", chan->name);
  2954. } else {
  2955. memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
  2956. return NULL;
  2957. }
  2958. }
  2959. #endif
  2960. ast_hangup(chan);
  2961. memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
  2962. return NULL;
  2963. }
  2964. /*! \brief Complete an attended transfer
  2965. *
  2966. * \param p The endpoint performing the attended transfer
  2967. * \param sub The sub-channel completing the attended transfer
  2968. *
  2969. * \note p->sub is the currently active sub-channel (the channel the phone is using)
  2970. * \note p->sub->next is the sub-channel not in use, potentially on hold
  2971. *
  2972. * \retval 0 when channel should be hung up
  2973. * \retval 1 when channel should not be hung up
  2974. */
  2975. static int attempt_transfer(struct mgcp_endpoint *p, struct mgcp_subchannel *sub)
  2976. {
  2977. enum ast_transfer_result res;
  2978. /* Ensure that the other channel goes off hold and that it is indicating properly */
  2979. ast_queue_unhold(sub->next->owner);
  2980. if (ast_channel_state(sub->owner) == AST_STATE_RINGING) {
  2981. ast_queue_control(sub->next->owner, AST_CONTROL_RINGING);
  2982. }
  2983. ast_mutex_unlock(&p->sub->next->lock);
  2984. ast_mutex_unlock(&p->sub->lock);
  2985. res = ast_bridge_transfer_attended(sub->owner, sub->next->owner);
  2986. /* Subs are only freed when the endpoint itself is destroyed, so they will continue to exist
  2987. * after ast_bridge_transfer_attended returns making this safe without reference counting
  2988. */
  2989. ast_mutex_lock(&p->sub->lock);
  2990. ast_mutex_lock(&p->sub->next->lock);
  2991. if (res != AST_BRIDGE_TRANSFER_SUCCESS) {
  2992. /* If transferring fails hang up the other channel if present and us */
  2993. if (sub->next->owner) {
  2994. ast_channel_softhangup_internal_flag_add(sub->next->owner, AST_SOFTHANGUP_DEV);
  2995. mgcp_queue_hangup(sub->next);
  2996. }
  2997. sub->next->alreadygone = 1;
  2998. return 0;
  2999. }
  3000. unalloc_sub(sub->next);
  3001. /* If the active sub is NOT the one completing the transfer change it to be, and hang up the other sub */
  3002. if (p->sub != sub) {
  3003. p->sub = sub;
  3004. return 1;
  3005. }
  3006. return 0;
  3007. }
  3008. static void handle_hd_hf(struct mgcp_subchannel *sub, char *ev)
  3009. {
  3010. struct mgcp_endpoint *p = sub->parent;
  3011. struct ast_channel *c;
  3012. pthread_t t;
  3013. /* Off hook / answer */
  3014. if (sub->outgoing) {
  3015. /* Answered */
  3016. if (sub->owner) {
  3017. ast_queue_unhold(sub->owner);
  3018. sub->cxmode = MGCP_CX_SENDRECV;
  3019. if (!sub->rtp) {
  3020. start_rtp(sub);
  3021. } else {
  3022. transmit_modify_request(sub);
  3023. }
  3024. /*transmit_notify_request(sub, "aw");*/
  3025. transmit_notify_request(sub, "");
  3026. mgcp_queue_control(sub, AST_CONTROL_ANSWER);
  3027. }
  3028. } else {
  3029. /* Start switch */
  3030. /*sub->cxmode = MGCP_CX_SENDRECV;*/
  3031. if (!sub->owner) {
  3032. if (!sub->rtp) {
  3033. start_rtp(sub);
  3034. } else {
  3035. transmit_modify_request(sub);
  3036. }
  3037. if (p->immediate) {
  3038. /* The channel is immediately up. Start right away */
  3039. #ifdef DLINK_BUGGY_FIRMWARE
  3040. transmit_notify_request(sub, "rt");
  3041. #else
  3042. transmit_notify_request(sub, p->ncs ? "L/rt" : "G/rt");
  3043. #endif
  3044. c = mgcp_new(sub, AST_STATE_RING, NULL, NULL);
  3045. if (!c) {
  3046. ast_log(LOG_WARNING, "Unable to start PBX on channel %s@%s\n", p->name, p->parent->name);
  3047. transmit_notify_request(sub, p->ncs ? "L/cg" : "G/cg");
  3048. ast_hangup(c);
  3049. }
  3050. } else {
  3051. if (has_voicemail(p)) {
  3052. transmit_notify_request(sub, "L/sl");
  3053. } else {
  3054. transmit_notify_request(sub, "L/dl");
  3055. }
  3056. c = mgcp_new(sub, AST_STATE_DOWN, NULL, NULL);
  3057. if (c) {
  3058. if (ast_pthread_create_detached(&t, NULL, mgcp_ss, c)) {
  3059. ast_log(LOG_WARNING, "Unable to create switch thread: %s\n", strerror(errno));
  3060. ast_hangup(c);
  3061. }
  3062. } else {
  3063. ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", p->name, p->parent->name);
  3064. }
  3065. }
  3066. } else {
  3067. if (p->hookstate == MGCP_OFFHOOK) {
  3068. ast_log(LOG_WARNING, "Off hook, but already have owner on %s@%s\n", p->name, p->parent->name);
  3069. } else {
  3070. ast_log(LOG_WARNING, "On hook, but already have owner on %s@%s\n", p->name, p->parent->name);
  3071. ast_log(LOG_WARNING, "If we're onhook why are we here trying to handle a hd or hf?\n");
  3072. }
  3073. ast_queue_unhold(sub->owner);
  3074. sub->cxmode = MGCP_CX_SENDRECV;
  3075. if (!sub->rtp) {
  3076. start_rtp(sub);
  3077. } else {
  3078. transmit_modify_request(sub);
  3079. }
  3080. /*transmit_notify_request(sub, "aw");*/
  3081. transmit_notify_request(sub, "");
  3082. /*ast_queue_control(sub->owner, AST_CONTROL_ANSWER);*/
  3083. }
  3084. }
  3085. }
  3086. static int handle_request(struct mgcp_subchannel *sub, struct mgcp_request *req, struct sockaddr_in *sin)
  3087. {
  3088. char *ev, *s;
  3089. struct ast_frame f = { 0, };
  3090. struct mgcp_endpoint *p = sub->parent;
  3091. struct mgcp_gateway *g = NULL;
  3092. int res;
  3093. ast_debug(1, "Handling request '%s' on %s@%s\n", req->verb, p->name, p->parent->name);
  3094. /* Clear out potential response */
  3095. if (!strcasecmp(req->verb, "RSIP")) {
  3096. /* Test if this RSIP request is just a keepalive */
  3097. if (!strcasecmp( get_header(req, "RM"), "X-keepalive")) {
  3098. ast_verb(3, "Received keepalive request from %s@%s\n", p->name, p->parent->name);
  3099. transmit_response(sub, "200", req, "OK");
  3100. } else {
  3101. dump_queue(p->parent, p);
  3102. dump_cmd_queues(p, NULL);
  3103. if ((strcmp(p->name, p->parent->wcardep) != 0)) {
  3104. ast_verb(3, "Resetting interface %s@%s\n", p->name, p->parent->name);
  3105. }
  3106. /* For RSIP on wildcard we reset all endpoints */
  3107. if (!strcmp(p->name, p->parent->wcardep)) {
  3108. /* Reset all endpoints */
  3109. struct mgcp_endpoint *tmp_ep;
  3110. g = p->parent;
  3111. for (tmp_ep = g->endpoints; tmp_ep; tmp_ep = tmp_ep->next) {
  3112. /*if ((strcmp(tmp_ep->name, "*") != 0) && (strcmp(tmp_ep->name, "aaln/" "*") != 0)) {*/
  3113. if (strcmp(tmp_ep->name, g->wcardep) != 0) {
  3114. struct mgcp_subchannel *tmp_sub, *first_sub;
  3115. ast_verb(3, "Resetting interface %s@%s\n", tmp_ep->name, p->parent->name);
  3116. first_sub = tmp_ep->sub;
  3117. tmp_sub = tmp_ep->sub;
  3118. while (tmp_sub) {
  3119. mgcp_queue_hangup(tmp_sub);
  3120. tmp_sub = tmp_sub->next;
  3121. if (tmp_sub == first_sub)
  3122. break;
  3123. }
  3124. }
  3125. }
  3126. } else if (sub->owner) {
  3127. mgcp_queue_hangup(sub);
  3128. }
  3129. transmit_response(sub, "200", req, "OK");
  3130. /* We don't send NTFY or AUEP to wildcard ep */
  3131. if (strcmp(p->name, p->parent->wcardep) != 0) {
  3132. transmit_notify_request(sub, "");
  3133. /* Audit endpoint.
  3134. Idea is to prevent lost lines due to race conditions
  3135. */
  3136. transmit_audit_endpoint(p);
  3137. }
  3138. }
  3139. } else if (!strcasecmp(req->verb, "NTFY")) {
  3140. /* Acknowledge and be sure we keep looking for the same things */
  3141. transmit_response(sub, "200", req, "OK");
  3142. /* Notified of an event */
  3143. ev = get_header(req, "O");
  3144. s = strchr(ev, '/');
  3145. if (s) ev = s + 1;
  3146. ast_debug(1, "Endpoint '%s@%s-%d' observed '%s'\n", p->name, p->parent->name, sub->id, ev);
  3147. /* Keep looking for events unless this was a hangup */
  3148. if (strcasecmp(ev, "hu") && strcasecmp(ev, "hd") && strcasecmp(ev, "ping")) {
  3149. transmit_notify_request(sub, p->curtone);
  3150. }
  3151. if (!strcasecmp(ev, "hd")) {
  3152. p->hookstate = MGCP_OFFHOOK;
  3153. sub->cxmode = MGCP_CX_SENDRECV;
  3154. if (p) {
  3155. /* When the endpoint have a Off hook transition we allways
  3156. starts without any previous dtmfs */
  3157. memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
  3158. }
  3159. handle_hd_hf(sub, ev);
  3160. } else if (!strcasecmp(ev, "hf")) {
  3161. /* We can assume we are offhook if we received a hookflash */
  3162. /* First let's just do call wait and ignore threeway */
  3163. /* We're currently in charge */
  3164. if (p->hookstate != MGCP_OFFHOOK) {
  3165. /* Cisco c7940 sends hf even if the phone is onhook */
  3166. /* Thanks to point on IRC for pointing this out */
  3167. return -1;
  3168. }
  3169. /* do not let * conference two down channels */
  3170. if (sub->owner && ast_channel_state(sub->owner) == AST_STATE_DOWN && !sub->next->owner)
  3171. return -1;
  3172. if (p->callwaiting || p->transfer || p->threewaycalling) {
  3173. ast_verb(3, "Swapping %d for %d on %s@%s\n", p->sub->id, p->sub->next->id, p->name, p->parent->name);
  3174. p->sub = p->sub->next;
  3175. /* transfer control to our next subchannel */
  3176. if (!sub->next->owner) {
  3177. /* plave the first call on hold and start up a new call */
  3178. sub->cxmode = MGCP_CX_MUTE;
  3179. ast_verb(3, "MGCP Muting %d on %s@%s\n", sub->id, p->name, p->parent->name);
  3180. transmit_modify_request(sub);
  3181. if (sub->owner) {
  3182. ast_queue_hold(sub->owner, NULL);
  3183. }
  3184. sub->next->cxmode = MGCP_CX_RECVONLY;
  3185. handle_hd_hf(sub->next, ev);
  3186. } else if (sub->owner && sub->next->owner) {
  3187. /* We've got two active calls lets decide whether or not to conference or just flip flop */
  3188. if ((!sub->outgoing) && (!sub->next->outgoing)) {
  3189. /* We made both calls lets conference */
  3190. ast_verb(3, "MGCP Conferencing %d and %d on %s@%s\n",
  3191. sub->id, sub->next->id, p->name, p->parent->name);
  3192. sub->cxmode = MGCP_CX_CONF;
  3193. sub->next->cxmode = MGCP_CX_CONF;
  3194. ast_queue_unhold(sub->next->owner);
  3195. transmit_modify_request(sub);
  3196. transmit_modify_request(sub->next);
  3197. } else {
  3198. /* Let's flipflop between calls */
  3199. /* XXX Need to check for state up ??? */
  3200. /* XXX Need a way to indicate the current call, or maybe the call that's waiting */
  3201. ast_verb(3, "We didn't make one of the calls FLIPFLOP %d and %d on %s@%s\n",
  3202. sub->id, sub->next->id, p->name, p->parent->name);
  3203. sub->cxmode = MGCP_CX_MUTE;
  3204. ast_verb(3, "MGCP Muting %d on %s@%s\n", sub->id, p->name, p->parent->name);
  3205. transmit_modify_request(sub);
  3206. ast_queue_hold(sub->owner, NULL);
  3207. ast_queue_hold(sub->next->owner, NULL);
  3208. handle_hd_hf(sub->next, ev);
  3209. }
  3210. } else {
  3211. /* We've most likely lost one of our calls find an active call and bring it up */
  3212. if (sub->owner) {
  3213. p->sub = sub;
  3214. } else if (sub->next->owner) {
  3215. p->sub = sub->next;
  3216. } else {
  3217. /* We seem to have lost both our calls */
  3218. /* XXX - What do we do now? */
  3219. return -1;
  3220. }
  3221. ast_queue_unhold(p->sub->owner);
  3222. p->sub->cxmode = MGCP_CX_SENDRECV;
  3223. transmit_modify_request(p->sub);
  3224. }
  3225. } else {
  3226. ast_log(LOG_WARNING, "Callwaiting, call transfer or threeway calling not enabled on endpoint %s@%s\n",
  3227. p->name, p->parent->name);
  3228. }
  3229. } else if (!strcasecmp(ev, "hu")) {
  3230. p->hookstate = MGCP_ONHOOK;
  3231. sub->cxmode = MGCP_CX_RECVONLY;
  3232. ast_debug(1, "MGCP %s@%s Went on hook\n", p->name, p->parent->name);
  3233. /* Do we need to send MDCX before a DLCX ?
  3234. if (sub->rtp) {
  3235. transmit_modify_request(sub);
  3236. }
  3237. */
  3238. if (p->transfer && (sub->owner && sub->next->owner) && ((!sub->outgoing) || (!sub->next->outgoing))) {
  3239. /* We're allowed to transfer, we have two avtive calls and */
  3240. /* we made at least one of the calls. Let's try and transfer */
  3241. ast_mutex_lock(&p->sub->next->lock);
  3242. res = attempt_transfer(p, sub);
  3243. if (res) {
  3244. ast_log(LOG_WARNING, "Transfer attempt failed\n");
  3245. ast_mutex_unlock(&p->sub->next->lock);
  3246. return -1;
  3247. }
  3248. ast_mutex_unlock(&p->sub->next->lock);
  3249. } else {
  3250. /* Hangup the current call */
  3251. /* If there is another active call, mgcp_hangup will ring the phone with the other call */
  3252. if (sub->owner) {
  3253. sub->alreadygone = 1;
  3254. mgcp_queue_hangup(sub);
  3255. } else {
  3256. ast_verb(3, "MGCP handle_request(%s@%s-%d) ast_channel already destroyed, resending DLCX.\n",
  3257. p->name, p->parent->name, sub->id);
  3258. /* Instruct the other side to remove the connection since it apparently *
  3259. * still thinks the channel is active. *
  3260. * For Cisco IAD2421 /BAK/ */
  3261. transmit_connection_del(sub);
  3262. }
  3263. }
  3264. if ((p->hookstate == MGCP_ONHOOK) && (!sub->rtp) && (!sub->next->rtp)) {
  3265. p->hidecallerid = 0;
  3266. if (p->hascallwaiting && !p->callwaiting) {
  3267. ast_verb(3, "Enabling call waiting on MGCP/%s@%s-%d\n", p->name, p->parent->name, sub->id);
  3268. p->callwaiting = -1;
  3269. }
  3270. if (has_voicemail(p)) {
  3271. ast_verb(3, "MGCP handle_request(%s@%s) set vmwi(+)\n", p->name, p->parent->name);
  3272. transmit_notify_request(sub, "L/vmwi(+)");
  3273. } else {
  3274. ast_verb(3, "MGCP handle_request(%s@%s) set vmwi(-)\n", p->name, p->parent->name);
  3275. transmit_notify_request(sub, "L/vmwi(-)");
  3276. }
  3277. }
  3278. } else if ((strlen(ev) == 1) &&
  3279. (((ev[0] >= '0') && (ev[0] <= '9')) ||
  3280. ((ev[0] >= 'A') && (ev[0] <= 'D')) ||
  3281. (ev[0] == '*') || (ev[0] == '#'))) {
  3282. if (sub && sub->owner && (ast_channel_state(sub->owner) >= AST_STATE_UP)) {
  3283. f.frametype = AST_FRAME_DTMF;
  3284. f.subclass.integer = ev[0];
  3285. f.src = "mgcp";
  3286. /* XXX MUST queue this frame to all subs in threeway call if threeway call is active */
  3287. mgcp_queue_frame(sub, &f);
  3288. ast_mutex_lock(&sub->next->lock);
  3289. if (sub->next->owner)
  3290. mgcp_queue_frame(sub->next, &f);
  3291. ast_mutex_unlock(&sub->next->lock);
  3292. if (strstr(p->curtone, (p->ncs ? "wt1" : "wt")) && (ev[0] == 'A')) {
  3293. memset(p->curtone, 0, sizeof(p->curtone));
  3294. }
  3295. } else {
  3296. p->dtmf_buf[strlen(p->dtmf_buf)] = ev[0];
  3297. p->dtmf_buf[strlen(p->dtmf_buf)] = '\0';
  3298. }
  3299. } else if (!strcasecmp(ev, "T")) {
  3300. /* Digit timeout -- unimportant */
  3301. } else if (!strcasecmp(ev, "ping")) {
  3302. /* ping -- unimportant */
  3303. } else {
  3304. ast_log(LOG_NOTICE, "Received unknown event '%s' from %s@%s\n", ev, p->name, p->parent->name);
  3305. }
  3306. } else {
  3307. ast_log(LOG_WARNING, "Unknown verb '%s' received from %s\n", req->verb, ast_inet_ntoa(sin->sin_addr));
  3308. transmit_response(sub, "510", req, "Unknown verb");
  3309. }
  3310. return 0;
  3311. }
  3312. static int find_and_retrans(struct mgcp_subchannel *sub, struct mgcp_request *req)
  3313. {
  3314. int seqno=0;
  3315. time_t now;
  3316. struct mgcp_response *prev = NULL, *cur, *next, *answer = NULL;
  3317. time(&now);
  3318. if (sscanf(req->identifier, "%30d", &seqno) != 1) {
  3319. seqno = 0;
  3320. }
  3321. for (cur = sub->parent->parent->responses, next = cur ? cur->next : NULL; cur; cur = next, next = cur ? cur->next : NULL) {
  3322. if (now - cur->whensent > RESPONSE_TIMEOUT) {
  3323. /* Delete this entry */
  3324. if (prev)
  3325. prev->next = next;
  3326. else
  3327. sub->parent->parent->responses = next;
  3328. ast_free(cur);
  3329. } else {
  3330. if (seqno == cur->seqno)
  3331. answer = cur;
  3332. prev = cur;
  3333. }
  3334. }
  3335. if (answer) {
  3336. resend_response(sub, answer);
  3337. return 1;
  3338. }
  3339. return 0;
  3340. }
  3341. static int mgcpsock_read(int *id, int fd, short events, void *ignore)
  3342. {
  3343. struct mgcp_request req;
  3344. struct sockaddr_in sin;
  3345. struct mgcp_subchannel *sub;
  3346. int res;
  3347. socklen_t len;
  3348. int result;
  3349. int ident;
  3350. len = sizeof(sin);
  3351. memset(&req, 0, sizeof(req));
  3352. res = recvfrom(mgcpsock, req.data, sizeof(req.data) - 1, 0, (struct sockaddr *)&sin, &len);
  3353. if (res < 0) {
  3354. if (errno != ECONNREFUSED)
  3355. ast_log(LOG_WARNING, "Recv error: %s\n", strerror(errno));
  3356. return 1;
  3357. }
  3358. req.data[res] = '\0';
  3359. req.len = res;
  3360. ast_debug(1, "MGCP read: \n%s\nfrom %s:%d\n", req.data, ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
  3361. parse(&req);
  3362. if (req.headers < 1) {
  3363. /* Must have at least one header */
  3364. return 1;
  3365. }
  3366. if (ast_strlen_zero(req.identifier)) {
  3367. ast_log(LOG_NOTICE, "Message from %s missing identifier\n", ast_inet_ntoa(sin.sin_addr));
  3368. return 1;
  3369. }
  3370. if (sscanf(req.verb, "%30d", &result) && sscanf(req.identifier, "%30d", &ident)) {
  3371. if (result < 200) {
  3372. ast_debug(1, "Ignoring provisional response on transaction %d\n", ident);
  3373. return 1;
  3374. }
  3375. /* Try to find who this message is for, if it's important */
  3376. sub = find_subchannel_and_lock(NULL, ident, &sin);
  3377. if (sub) {
  3378. struct mgcp_gateway *gw = sub->parent->parent;
  3379. struct mgcp_message *cur, *prev;
  3380. ast_mutex_unlock(&sub->lock);
  3381. ast_mutex_lock(&gw->msgs_lock);
  3382. for (prev = NULL, cur = gw->msgs; cur; prev = cur, cur = cur->next) {
  3383. if (cur->seqno == ident) {
  3384. ast_debug(1, "Got response back on transaction %d\n", ident);
  3385. if (prev)
  3386. prev->next = cur->next;
  3387. else
  3388. gw->msgs = cur->next;
  3389. break;
  3390. }
  3391. }
  3392. /* stop retrans timer if the queue is empty */
  3393. if (!gw->msgs) {
  3394. AST_SCHED_DEL(sched, gw->retransid);
  3395. }
  3396. ast_mutex_unlock(&gw->msgs_lock);
  3397. if (cur) {
  3398. handle_response(cur->owner_ep, cur->owner_sub, result, ident, &req);
  3399. ast_free(cur);
  3400. return 1;
  3401. }
  3402. ast_log(LOG_NOTICE, "Got response back on [%s] for transaction %d we aren't sending?\n",
  3403. gw->name, ident);
  3404. }
  3405. } else {
  3406. if (ast_strlen_zero(req.endpoint) ||
  3407. ast_strlen_zero(req.version) ||
  3408. ast_strlen_zero(req.verb)) {
  3409. ast_log(LOG_NOTICE, "Message must have a verb, an idenitifier, version, and endpoint\n");
  3410. return 1;
  3411. }
  3412. /* Process request, with iflock held */
  3413. sub = find_subchannel_and_lock(req.endpoint, 0, &sin);
  3414. if (sub) {
  3415. /* look first to find a matching response in the queue */
  3416. if (!find_and_retrans(sub, &req))
  3417. /* pass the request off to the currently mastering subchannel */
  3418. handle_request(sub, &req, &sin);
  3419. ast_mutex_unlock(&sub->lock);
  3420. }
  3421. }
  3422. return 1;
  3423. }
  3424. static int *mgcpsock_read_id = NULL;
  3425. static int mgcp_prune_realtime_gateway(struct mgcp_gateway *g)
  3426. {
  3427. struct mgcp_endpoint *enext, *e;
  3428. struct mgcp_subchannel *s, *sub;
  3429. int i, prune = 1;
  3430. if (g->ha || !g->realtime || ast_mutex_trylock(&g->msgs_lock) || g->msgs) {
  3431. ast_mutex_unlock(&g->msgs_lock);
  3432. return 0;
  3433. }
  3434. for (e = g->endpoints; e; e = e->next) {
  3435. ast_mutex_lock(&e->lock);
  3436. if (e->dsp || ast_mutex_trylock(&e->rqnt_queue_lock) || ast_mutex_trylock(&e->cmd_queue_lock)) {
  3437. prune = 0;
  3438. } else if (e->rqnt_queue || e->cmd_queue) {
  3439. prune = 0;
  3440. }
  3441. s = e->sub;
  3442. for (i = 0; (i < MAX_SUBS) && s; i++) {
  3443. ast_mutex_lock(&s->lock);
  3444. if (!ast_strlen_zero(s->cxident) || s->rtp || ast_mutex_trylock(&s->cx_queue_lock) || s->gate) {
  3445. prune = 0;
  3446. } else if (s->cx_queue) {
  3447. prune = 0;
  3448. }
  3449. s = s->next;
  3450. }
  3451. }
  3452. for (e = g->endpoints, sub = e->sub, enext = e->next; e; e = enext, enext = e->next) {
  3453. for (i = 0; (i < MAX_SUBS) && sub; i++) {
  3454. s = sub;
  3455. sub = sub->next;
  3456. ast_mutex_unlock(&s->lock);
  3457. ast_mutex_unlock(&s->cx_queue_lock);
  3458. if (prune) {
  3459. ast_mutex_destroy(&s->lock);
  3460. ast_mutex_destroy(&s->cx_queue_lock);
  3461. free(s);
  3462. }
  3463. }
  3464. ast_mutex_unlock(&e->lock);
  3465. ast_mutex_unlock(&e->rqnt_queue_lock);
  3466. ast_mutex_unlock(&e->cmd_queue_lock);
  3467. if (prune) {
  3468. ast_mutex_destroy(&e->lock);
  3469. ast_mutex_destroy(&e->rqnt_queue_lock);
  3470. ast_mutex_destroy(&e->cmd_queue_lock);
  3471. free(e);
  3472. }
  3473. }
  3474. if (prune) {
  3475. ast_debug(1, "***** MGCP REALTIME PRUNE GW: %s\n", g->name);
  3476. }
  3477. return prune;
  3478. }
  3479. static void *do_monitor(void *data)
  3480. {
  3481. int res;
  3482. int reloading;
  3483. struct mgcp_gateway *g, *gprev;
  3484. /*struct mgcp_gateway *g;*/
  3485. /*struct mgcp_endpoint *e;*/
  3486. /*time_t thispass = 0, lastpass = 0;*/
  3487. time_t lastrun = 0;
  3488. /* Add an I/O event to our UDP socket */
  3489. if (mgcpsock > -1) {
  3490. mgcpsock_read_id = ast_io_add(io, mgcpsock, mgcpsock_read, AST_IO_IN, NULL);
  3491. }
  3492. /* This thread monitors all the frame relay interfaces which are not yet in use
  3493. (and thus do not have a separate thread) indefinitely */
  3494. /* From here on out, we die whenever asked */
  3495. for (;;) {
  3496. /* Check for a reload request */
  3497. ast_mutex_lock(&mgcp_reload_lock);
  3498. reloading = mgcp_reloading;
  3499. mgcp_reloading = 0;
  3500. ast_mutex_unlock(&mgcp_reload_lock);
  3501. if (reloading) {
  3502. ast_verb(1, "Reloading MGCP\n");
  3503. reload_config(1);
  3504. /* Add an I/O event to our UDP socket */
  3505. if (mgcpsock > -1 && !mgcpsock_read_id) {
  3506. mgcpsock_read_id = ast_io_add(io, mgcpsock, mgcpsock_read, AST_IO_IN, NULL);
  3507. }
  3508. }
  3509. /* Check for interfaces needing to be killed */
  3510. /* Don't let anybody kill us right away. Nobody should lock the interface list
  3511. and wait for the monitor list, but the other way around is okay. */
  3512. ast_mutex_lock(&monlock);
  3513. /* Lock the network interface */
  3514. ast_mutex_lock(&netlock);
  3515. #if 0
  3516. /* XXX THIS IS COMPLETELY HOSED */
  3517. /* The gateway goes into a state of panic */
  3518. /* If the vmwi indicator is sent while it is reseting interfaces */
  3519. lastpass = thispass;
  3520. thispass = time(NULL);
  3521. g = gateways;
  3522. while(g) {
  3523. if (thispass != lastpass) {
  3524. e = g->endpoints;
  3525. while(e) {
  3526. if (e->type == TYPE_LINE) {
  3527. res = has_voicemail(e);
  3528. if ((e->msgstate != res) && (e->hookstate == MGCP_ONHOOK) && (!e->rtp)){
  3529. if (res) {
  3530. transmit_notify_request(e, "L/vmwi(+)");
  3531. } else {
  3532. transmit_notify_request(e, "L/vmwi(-)");
  3533. }
  3534. e->msgstate = res;
  3535. e->onhooktime = thispass;
  3536. }
  3537. }
  3538. e = e->next;
  3539. }
  3540. }
  3541. g = g->next;
  3542. }
  3543. #endif
  3544. /* pruning unused realtime gateways, running in every 60 seconds*/
  3545. if(time(NULL) > (lastrun + 60)) {
  3546. ast_mutex_lock(&gatelock);
  3547. g = gateways;
  3548. gprev = NULL;
  3549. while(g) {
  3550. if(g->realtime) {
  3551. if(mgcp_prune_realtime_gateway(g)) {
  3552. if(gprev) {
  3553. gprev->next = g->next;
  3554. } else {
  3555. gateways = g->next;
  3556. }
  3557. ast_mutex_unlock(&g->msgs_lock);
  3558. ast_mutex_destroy(&g->msgs_lock);
  3559. free(g);
  3560. } else {
  3561. ast_mutex_unlock(&g->msgs_lock);
  3562. gprev = g;
  3563. }
  3564. } else {
  3565. gprev = g;
  3566. }
  3567. g = g->next;
  3568. }
  3569. ast_mutex_unlock(&gatelock);
  3570. lastrun = time(NULL);
  3571. }
  3572. /* Okay, now that we know what to do, release the network lock */
  3573. ast_mutex_unlock(&netlock);
  3574. /* And from now on, we're okay to be killed, so release the monitor lock as well */
  3575. ast_mutex_unlock(&monlock);
  3576. pthread_testcancel();
  3577. /* Wait for sched or io */
  3578. res = ast_sched_wait(sched);
  3579. /* copied from chan_sip.c */
  3580. if ((res < 0) || (res > 1000)) {
  3581. res = 1000;
  3582. }
  3583. res = ast_io_wait(io, res);
  3584. ast_mutex_lock(&monlock);
  3585. if (res >= 0) {
  3586. ast_sched_runq(sched);
  3587. }
  3588. ast_mutex_unlock(&monlock);
  3589. }
  3590. /* Never reached */
  3591. return NULL;
  3592. }
  3593. static int restart_monitor(void)
  3594. {
  3595. /* If we're supposed to be stopped -- stay stopped */
  3596. if (monitor_thread == AST_PTHREADT_STOP)
  3597. return 0;
  3598. if (ast_mutex_lock(&monlock)) {
  3599. ast_log(LOG_WARNING, "Unable to lock monitor\n");
  3600. return -1;
  3601. }
  3602. if (monitor_thread == pthread_self()) {
  3603. ast_mutex_unlock(&monlock);
  3604. ast_log(LOG_WARNING, "Cannot kill myself\n");
  3605. return -1;
  3606. }
  3607. if (monitor_thread != AST_PTHREADT_NULL) {
  3608. /* Wake up the thread */
  3609. pthread_kill(monitor_thread, SIGURG);
  3610. } else {
  3611. /* Start a new monitor */
  3612. if (ast_pthread_create_background(&monitor_thread, NULL, do_monitor, NULL) < 0) {
  3613. ast_mutex_unlock(&monlock);
  3614. ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
  3615. return -1;
  3616. }
  3617. }
  3618. ast_mutex_unlock(&monlock);
  3619. return 0;
  3620. }
  3621. static struct ast_channel *mgcp_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *dest, int *cause)
  3622. {
  3623. struct mgcp_subchannel *sub;
  3624. struct ast_channel *tmpc = NULL;
  3625. char tmp[256];
  3626. if (!(ast_format_cap_iscompatible(cap, global_capability))) {
  3627. struct ast_str *cap_buf = ast_str_alloca(64);
  3628. ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%s'\n",
  3629. ast_format_cap_get_names(cap, &cap_buf));
  3630. /*return NULL;*/
  3631. }
  3632. ast_copy_string(tmp, dest, sizeof(tmp));
  3633. if (ast_strlen_zero(tmp)) {
  3634. ast_log(LOG_NOTICE, "MGCP Channels require an endpoint\n");
  3635. return NULL;
  3636. }
  3637. if (!(sub = find_subchannel_and_lock(tmp, 0, NULL))) {
  3638. ast_log(LOG_WARNING, "Unable to find MGCP endpoint '%s'\n", tmp);
  3639. *cause = AST_CAUSE_UNREGISTERED;
  3640. return NULL;
  3641. }
  3642. ast_verb(3, "MGCP mgcp_request(%s)\n", tmp);
  3643. ast_verb(3, "MGCP cw: %d, dnd: %d, so: %d, sno: %d\n",
  3644. sub->parent->callwaiting, sub->parent->dnd, sub->owner ? 1 : 0, sub->next->owner ? 1: 0);
  3645. /* Must be busy */
  3646. if (((sub->parent->callwaiting) && ((sub->owner) && (sub->next->owner))) ||
  3647. ((!sub->parent->callwaiting) && (sub->owner)) ||
  3648. (sub->parent->dnd && (ast_strlen_zero(sub->parent->call_forward)))) {
  3649. if (sub->parent->hookstate == MGCP_ONHOOK) {
  3650. if (has_voicemail(sub->parent)) {
  3651. transmit_notify_request(sub,"L/vmwi(+)");
  3652. } else {
  3653. transmit_notify_request(sub,"L/vmwi(-)");
  3654. }
  3655. }
  3656. *cause = AST_CAUSE_BUSY;
  3657. ast_mutex_unlock(&sub->lock);
  3658. return NULL;
  3659. }
  3660. tmpc = mgcp_new(sub->owner ? sub->next : sub, AST_STATE_DOWN, assignedids, requestor);
  3661. ast_mutex_unlock(&sub->lock);
  3662. if (!tmpc)
  3663. ast_log(LOG_WARNING, "Unable to make channel for '%s'\n", tmp);
  3664. restart_monitor();
  3665. return tmpc;
  3666. }
  3667. /* modified for reload support */
  3668. /*! \brief build_gateway: parse mgcp.conf and create gateway/endpoint structures */
  3669. static struct mgcp_gateway *build_gateway(char *cat, struct ast_variable *v)
  3670. {
  3671. struct mgcp_gateway *gw;
  3672. struct mgcp_endpoint *e;
  3673. struct mgcp_subchannel *sub;
  3674. struct ast_variable *chanvars = NULL;
  3675. /*char txident[80];*/
  3676. int i=0, y=0;
  3677. int gw_reload = 0;
  3678. int ep_reload = 0;
  3679. directmedia = DIRECTMEDIA;
  3680. /* locate existing gateway */
  3681. for (gw = gateways; gw; gw = gw->next) {
  3682. if (!strcasecmp(cat, gw->name)) {
  3683. /* gateway already exists */
  3684. gw->delme = 0;
  3685. gw_reload = 1;
  3686. break;
  3687. }
  3688. }
  3689. if (!gw && !(gw = ast_calloc(1, sizeof(*gw)))) {
  3690. return NULL;
  3691. }
  3692. if (!gw_reload) {
  3693. gw->expire = -1;
  3694. gw->realtime = 0;
  3695. gw->retransid = -1;
  3696. ast_mutex_init(&gw->msgs_lock);
  3697. ast_copy_string(gw->name, cat, sizeof(gw->name));
  3698. /* check if the name is numeric ip */
  3699. if ((strchr(gw->name, '.')) && inet_addr(gw->name) != INADDR_NONE)
  3700. gw->isnamedottedip = 1;
  3701. }
  3702. for (; v; v = v->next) {
  3703. if (!strcasecmp(v->name, "host")) {
  3704. if (!strcasecmp(v->value, "dynamic")) {
  3705. /* They'll register with us */
  3706. gw->dynamic = 1;
  3707. memset(&gw->addr.sin_addr, 0, 4);
  3708. if (gw->addr.sin_port) {
  3709. /* If we've already got a port, make it the default rather than absolute */
  3710. gw->defaddr.sin_port = gw->addr.sin_port;
  3711. gw->addr.sin_port = 0;
  3712. }
  3713. } else {
  3714. /* Non-dynamic. Make sure we become that way if we're not */
  3715. AST_SCHED_DEL(sched, gw->expire);
  3716. gw->dynamic = 0;
  3717. {
  3718. struct ast_sockaddr tmp;
  3719. ast_sockaddr_from_sin(&tmp, &gw->addr);
  3720. if (ast_get_ip(&tmp, v->value)) {
  3721. if (!gw_reload) {
  3722. ast_mutex_destroy(&gw->msgs_lock);
  3723. ast_free(gw);
  3724. }
  3725. return NULL;
  3726. }
  3727. ast_sockaddr_to_sin(&tmp, &gw->addr);
  3728. }
  3729. }
  3730. } else if (!strcasecmp(v->name, "defaultip")) {
  3731. struct ast_sockaddr tmp;
  3732. ast_sockaddr_from_sin(&tmp, &gw->defaddr);
  3733. if (ast_get_ip(&tmp, v->value)) {
  3734. if (!gw_reload) {
  3735. ast_mutex_destroy(&gw->msgs_lock);
  3736. ast_free(gw);
  3737. }
  3738. return NULL;
  3739. }
  3740. ast_sockaddr_to_sin(&tmp, &gw->defaddr);
  3741. } else if (!strcasecmp(v->name, "permit") ||
  3742. !strcasecmp(v->name, "deny")) {
  3743. gw->ha = ast_append_ha(v->name, v->value, gw->ha, NULL);
  3744. } else if (!strcasecmp(v->name, "port")) {
  3745. gw->addr.sin_port = htons(atoi(v->value));
  3746. } else if (!strcasecmp(v->name, "context")) {
  3747. ast_copy_string(context, v->value, sizeof(context));
  3748. } else if (!strcasecmp(v->name, "dtmfmode")) {
  3749. if (!strcasecmp(v->value, "inband"))
  3750. dtmfmode = MGCP_DTMF_INBAND;
  3751. else if (!strcasecmp(v->value, "rfc2833"))
  3752. dtmfmode = MGCP_DTMF_RFC2833;
  3753. else if (!strcasecmp(v->value, "hybrid"))
  3754. dtmfmode = MGCP_DTMF_HYBRID;
  3755. else if (!strcasecmp(v->value, "none"))
  3756. dtmfmode = 0;
  3757. else
  3758. ast_log(LOG_WARNING, "'%s' is not a valid DTMF mode at line %d\n", v->value, v->lineno);
  3759. } else if (!strcasecmp(v->name, "nat")) {
  3760. nat = ast_true(v->value);
  3761. } else if (!strcasecmp(v->name, "ncs")) {
  3762. ncs = ast_true(v->value);
  3763. } else if (!strcasecmp(v->name, "hangupongateremove")) {
  3764. hangupongateremove = ast_true(v->value);
  3765. } else if (!strcasecmp(v->name, "pktcgatealloc")) {
  3766. pktcgatealloc = ast_true(v->value);
  3767. } else if (!strcasecmp(v->name, "callerid")) {
  3768. if (!strcasecmp(v->value, "asreceived")) {
  3769. cid_num[0] = '\0';
  3770. cid_name[0] = '\0';
  3771. } else {
  3772. ast_callerid_split(v->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
  3773. }
  3774. } else if (!strcasecmp(v->name, "language")) {
  3775. ast_copy_string(language, v->value, sizeof(language));
  3776. } else if (!strcasecmp(v->name, "accountcode")) {
  3777. ast_copy_string(accountcode, v->value, sizeof(accountcode));
  3778. } else if (!strcasecmp(v->name, "amaflags")) {
  3779. y = ast_channel_string2amaflag(v->value);
  3780. if (y < 0) {
  3781. ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d\n", v->value, v->lineno);
  3782. } else {
  3783. amaflags = y;
  3784. }
  3785. } else if (!strcasecmp(v->name, "setvar")) {
  3786. chanvars = add_var(v->value, chanvars);
  3787. } else if (!strcasecmp(v->name, "clearvars")) {
  3788. if (chanvars) {
  3789. ast_variables_destroy(chanvars);
  3790. chanvars = NULL;
  3791. }
  3792. } else if (!strcasecmp(v->name, "musiconhold")) {
  3793. ast_copy_string(musicclass, v->value, sizeof(musicclass));
  3794. } else if (!strcasecmp(v->name, "parkinglot")) {
  3795. ast_copy_string(parkinglot, v->value, sizeof(parkinglot));
  3796. } else if (!strcasecmp(v->name, "callgroup")) {
  3797. cur_callergroup = ast_get_group(v->value);
  3798. } else if (!strcasecmp(v->name, "pickupgroup")) {
  3799. cur_pickupgroup = ast_get_group(v->value);
  3800. } else if (!strcasecmp(v->name, "immediate")) {
  3801. immediate = ast_true(v->value);
  3802. } else if (!strcasecmp(v->name, "cancallforward")) {
  3803. cancallforward = ast_true(v->value);
  3804. } else if (!strcasecmp(v->name, "singlepath")) {
  3805. singlepath = ast_true(v->value);
  3806. } else if (!strcasecmp(v->name, "directmedia") || !strcasecmp(v->name, "canreinvite")) {
  3807. directmedia = ast_true(v->value);
  3808. } else if (!strcasecmp(v->name, "mailbox")) {
  3809. ast_copy_string(mailbox, v->value, sizeof(mailbox));
  3810. } else if (!strcasecmp(v->name, "hasvoicemail")) {
  3811. if (ast_true(v->value) && ast_strlen_zero(mailbox)) {
  3812. /*
  3813. * hasvoicemail is a users.conf legacy voicemail enable method.
  3814. * hasvoicemail is only going to work for app_voicemail mailboxes.
  3815. */
  3816. if (strchr(gw->name, '@')) {
  3817. ast_copy_string(mailbox, gw->name, sizeof(mailbox));
  3818. } else {
  3819. snprintf(mailbox, sizeof(mailbox), "%s@default", gw->name);
  3820. }
  3821. }
  3822. } else if (!strcasecmp(v->name, "adsi")) {
  3823. adsi = ast_true(v->value);
  3824. } else if (!strcasecmp(v->name, "callreturn")) {
  3825. callreturn = ast_true(v->value);
  3826. } else if (!strcasecmp(v->name, "callwaiting")) {
  3827. callwaiting = ast_true(v->value);
  3828. } else if (!strcasecmp(v->name, "slowsequence")) {
  3829. slowsequence = ast_true(v->value);
  3830. } else if (!strcasecmp(v->name, "transfer")) {
  3831. transfer = ast_true(v->value);
  3832. } else if (!strcasecmp(v->name, "threewaycalling")) {
  3833. threewaycalling = ast_true(v->value);
  3834. } else if (!strcasecmp(v->name, "wcardep")) {
  3835. /* locate existing endpoint */
  3836. for (e = gw->endpoints; e; e = e->next) {
  3837. if (!strcasecmp(v->value, e->name)) {
  3838. /* endpoint already exists */
  3839. e->delme = 0;
  3840. ep_reload = 1;
  3841. break;
  3842. }
  3843. }
  3844. if (!e) {
  3845. /* Allocate wildcard endpoint */
  3846. e = ast_calloc(1, sizeof(*e));
  3847. ep_reload = 0;
  3848. }
  3849. if (e) {
  3850. if (!ep_reload) {
  3851. memset(e, 0, sizeof(struct mgcp_endpoint));
  3852. ast_mutex_init(&e->lock);
  3853. ast_mutex_init(&e->rqnt_queue_lock);
  3854. ast_mutex_init(&e->cmd_queue_lock);
  3855. e->cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  3856. ast_copy_string(e->name, v->value, sizeof(e->name));
  3857. e->needaudit = 1;
  3858. }
  3859. ast_copy_string(gw->wcardep, v->value, sizeof(gw->wcardep));
  3860. /* XXX Should we really check for uniqueness?? XXX */
  3861. ast_copy_string(e->accountcode, accountcode, sizeof(e->accountcode));
  3862. ast_copy_string(e->context, context, sizeof(e->context));
  3863. ast_copy_string(e->cid_num, cid_num, sizeof(e->cid_num));
  3864. ast_copy_string(e->cid_name, cid_name, sizeof(e->cid_name));
  3865. ast_copy_string(e->language, language, sizeof(e->language));
  3866. ast_copy_string(e->musicclass, musicclass, sizeof(e->musicclass));
  3867. ast_copy_string(e->mailbox, mailbox, sizeof(e->mailbox));
  3868. ast_copy_string(e->parkinglot, parkinglot, sizeof(e->parkinglot));
  3869. if (!ast_strlen_zero(e->mailbox)) {
  3870. struct stasis_topic *mailbox_specific_topic;
  3871. mailbox_specific_topic = ast_mwi_topic(e->mailbox);
  3872. if (mailbox_specific_topic) {
  3873. e->mwi_event_sub = stasis_subscribe(mailbox_specific_topic, mwi_event_cb, NULL);
  3874. }
  3875. }
  3876. snprintf(e->rqnt_ident, sizeof(e->rqnt_ident), "%08lx", (unsigned long)ast_random());
  3877. e->msgstate = -1;
  3878. e->amaflags = amaflags;
  3879. ast_format_cap_append_from_cap(e->cap, global_capability, AST_MEDIA_TYPE_UNKNOWN);
  3880. e->parent = gw;
  3881. e->ncs = ncs;
  3882. e->dtmfmode = dtmfmode;
  3883. if (!ep_reload && e->sub && e->sub->rtp) {
  3884. e->dtmfmode |= MGCP_DTMF_INBAND;
  3885. }
  3886. e->adsi = adsi;
  3887. e->type = TYPE_LINE;
  3888. e->immediate = immediate;
  3889. e->callgroup=cur_callergroup;
  3890. e->pickupgroup=cur_pickupgroup;
  3891. e->callreturn = callreturn;
  3892. e->cancallforward = cancallforward;
  3893. e->singlepath = singlepath;
  3894. e->directmedia = directmedia;
  3895. e->callwaiting = callwaiting;
  3896. e->hascallwaiting = callwaiting;
  3897. e->slowsequence = slowsequence;
  3898. e->transfer = transfer;
  3899. e->threewaycalling = threewaycalling;
  3900. e->onhooktime = time(NULL);
  3901. /* ASSUME we're onhook */
  3902. e->hookstate = MGCP_ONHOOK;
  3903. e->chanvars = copy_vars(chanvars);
  3904. if (!ep_reload) {
  3905. /*snprintf(txident, sizeof(txident), "%08lx", (unsigned long)ast_random());*/
  3906. for (i = 0; i < MAX_SUBS; i++) {
  3907. sub = ast_calloc(1, sizeof(*sub));
  3908. if (sub) {
  3909. ast_verb(3, "Allocating subchannel '%d' on %s@%s\n", i, e->name, gw->name);
  3910. ast_mutex_init(&sub->lock);
  3911. ast_mutex_init(&sub->cx_queue_lock);
  3912. sub->parent = e;
  3913. sub->id = i;
  3914. snprintf(sub->txident, sizeof(sub->txident), "%08lx", (unsigned long)ast_random());
  3915. /*stnrcpy(sub->txident, txident, sizeof(sub->txident) - 1);*/
  3916. sub->cxmode = MGCP_CX_INACTIVE;
  3917. sub->nat = nat;
  3918. sub->gate = NULL;
  3919. sub->sdpsent = 0;
  3920. sub->next = e->sub;
  3921. e->sub = sub;
  3922. } else {
  3923. /* XXX Should find a way to clean up our memory */
  3924. ast_log(LOG_WARNING, "Out of memory allocating subchannel\n");
  3925. return NULL;
  3926. }
  3927. }
  3928. /* Make out subs a circular linked list so we can always sping through the whole bunch */
  3929. /* find the end of the list */
  3930. for (sub = e->sub; sub && sub->next; sub = sub->next);
  3931. /* set the last sub->next to the first sub */
  3932. sub->next = e->sub;
  3933. e->next = gw->endpoints;
  3934. gw->endpoints = e;
  3935. }
  3936. }
  3937. } else if (!strcasecmp(v->name, "trunk") ||
  3938. !strcasecmp(v->name, "line")) {
  3939. /* locate existing endpoint */
  3940. for (e = gw->endpoints; e; e = e->next) {
  3941. if (!strcasecmp(v->value, e->name)) {
  3942. /* endpoint already exists */
  3943. e->delme = 0;
  3944. ep_reload = 1;
  3945. break;
  3946. }
  3947. }
  3948. if (!e) {
  3949. e = ast_calloc(1, sizeof(*e));
  3950. ep_reload = 0;
  3951. }
  3952. if (e) {
  3953. if (!ep_reload) {
  3954. ast_mutex_init(&e->lock);
  3955. ast_mutex_init(&e->rqnt_queue_lock);
  3956. ast_mutex_init(&e->cmd_queue_lock);
  3957. e->cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  3958. ast_copy_string(e->name, v->value, sizeof(e->name));
  3959. e->needaudit = 1;
  3960. }
  3961. /* XXX Should we really check for uniqueness?? XXX */
  3962. ast_copy_string(e->accountcode, accountcode, sizeof(e->accountcode));
  3963. ast_copy_string(e->context, context, sizeof(e->context));
  3964. ast_copy_string(e->cid_num, cid_num, sizeof(e->cid_num));
  3965. ast_copy_string(e->cid_name, cid_name, sizeof(e->cid_name));
  3966. ast_copy_string(e->language, language, sizeof(e->language));
  3967. ast_copy_string(e->musicclass, musicclass, sizeof(e->musicclass));
  3968. ast_copy_string(e->mailbox, mailbox, sizeof(e->mailbox));
  3969. ast_copy_string(e->parkinglot, parkinglot, sizeof(e->parkinglot));
  3970. if (!ast_strlen_zero(mailbox)) {
  3971. ast_verb(3, "Setting mailbox '%s' on %s@%s\n", mailbox, gw->name, e->name);
  3972. }
  3973. if (!ep_reload) {
  3974. /* XXX potential issue due to reload */
  3975. e->msgstate = -1;
  3976. e->parent = gw;
  3977. }
  3978. e->amaflags = amaflags;
  3979. ast_format_cap_append_from_cap(e->cap, global_capability, AST_MEDIA_TYPE_UNKNOWN);
  3980. e->dtmfmode = dtmfmode;
  3981. e->ncs = ncs;
  3982. e->pktcgatealloc = pktcgatealloc;
  3983. e->hangupongateremove = hangupongateremove;
  3984. e->adsi = adsi;
  3985. e->type = (!strcasecmp(v->name, "trunk")) ? TYPE_TRUNK : TYPE_LINE;
  3986. e->immediate = immediate;
  3987. e->callgroup=cur_callergroup;
  3988. e->pickupgroup=cur_pickupgroup;
  3989. e->callreturn = callreturn;
  3990. e->cancallforward = cancallforward;
  3991. e->directmedia = directmedia;
  3992. e->singlepath = singlepath;
  3993. e->callwaiting = callwaiting;
  3994. e->hascallwaiting = callwaiting;
  3995. e->slowsequence = slowsequence;
  3996. e->transfer = transfer;
  3997. e->threewaycalling = threewaycalling;
  3998. /* If we already have a valid chanvars, it's not a new endpoint (it's a reload),
  3999. so first, free previous mem
  4000. */
  4001. if (e->chanvars) {
  4002. ast_variables_destroy(e->chanvars);
  4003. e->chanvars = NULL;
  4004. }
  4005. e->chanvars = copy_vars(chanvars);
  4006. if (!ep_reload) {
  4007. e->onhooktime = time(NULL);
  4008. /* ASSUME we're onhook */
  4009. e->hookstate = MGCP_ONHOOK;
  4010. snprintf(e->rqnt_ident, sizeof(e->rqnt_ident), "%08lx", (unsigned long)ast_random());
  4011. }
  4012. for (i = 0, sub = NULL; i < MAX_SUBS; i++) {
  4013. if (!ep_reload) {
  4014. sub = ast_calloc(1, sizeof(*sub));
  4015. } else {
  4016. if (!sub) {
  4017. sub = e->sub;
  4018. } else {
  4019. sub = sub->next;
  4020. }
  4021. }
  4022. if (sub) {
  4023. if (!ep_reload) {
  4024. ast_verb(3, "Allocating subchannel '%d' on %s@%s\n", i, e->name, gw->name);
  4025. ast_mutex_init(&sub->lock);
  4026. ast_mutex_init(&sub->cx_queue_lock);
  4027. ast_copy_string(sub->magic, MGCP_SUBCHANNEL_MAGIC, sizeof(sub->magic));
  4028. sub->parent = e;
  4029. sub->id = i;
  4030. snprintf(sub->txident, sizeof(sub->txident), "%08lx", (unsigned long)ast_random());
  4031. sub->cxmode = MGCP_CX_INACTIVE;
  4032. sub->next = e->sub;
  4033. e->sub = sub;
  4034. }
  4035. sub->nat = nat;
  4036. } else {
  4037. /* XXX Should find a way to clean up our memory */
  4038. ast_log(LOG_WARNING, "Out of memory allocating subchannel\n");
  4039. return NULL;
  4040. }
  4041. }
  4042. if (!ep_reload) {
  4043. /* Make out subs a circular linked list so we can always sping through the whole bunch */
  4044. /* find the end of the list */
  4045. for (sub = e->sub; sub && sub->next; sub = sub->next);
  4046. /* set the last sub->next to the first sub */
  4047. sub->next = e->sub;
  4048. e->next = gw->endpoints;
  4049. gw->endpoints = e;
  4050. }
  4051. }
  4052. } else if (!strcasecmp(v->name, "name") || !strcasecmp(v->name, "lines")) {
  4053. /* just eliminate realtime warnings */
  4054. } else {
  4055. ast_log(LOG_WARNING, "Don't know keyword '%s' at line %d\n", v->name, v->lineno);
  4056. }
  4057. }
  4058. if (!ntohl(gw->addr.sin_addr.s_addr) && !gw->dynamic) {
  4059. ast_log(LOG_WARNING, "Gateway '%s' lacks IP address and isn't dynamic\n", gw->name);
  4060. if (!gw_reload) {
  4061. ast_mutex_destroy(&gw->msgs_lock);
  4062. ast_free(gw);
  4063. }
  4064. /* Return NULL */
  4065. gw_reload = 1;
  4066. } else {
  4067. gw->defaddr.sin_family = AF_INET;
  4068. gw->addr.sin_family = AF_INET;
  4069. if (gw->defaddr.sin_addr.s_addr && !ntohs(gw->defaddr.sin_port)) {
  4070. gw->defaddr.sin_port = htons(DEFAULT_MGCP_GW_PORT);
  4071. }
  4072. if (gw->addr.sin_addr.s_addr && !ntohs(gw->addr.sin_port)) {
  4073. gw->addr.sin_port = htons(DEFAULT_MGCP_GW_PORT);
  4074. }
  4075. {
  4076. struct ast_sockaddr tmp1, tmp2;
  4077. struct sockaddr_in tmp3 = {0,};
  4078. tmp3.sin_addr = gw->ourip;
  4079. ast_sockaddr_from_sin(&tmp1, &gw->addr);
  4080. ast_sockaddr_from_sin(&tmp2, &tmp3);
  4081. if (gw->addr.sin_addr.s_addr && ast_ouraddrfor(&tmp1, &tmp2)) {
  4082. memcpy(&gw->ourip, &__ourip, sizeof(gw->ourip));
  4083. } else {
  4084. ast_sockaddr_to_sin(&tmp2, &tmp3);
  4085. gw->ourip = tmp3.sin_addr;
  4086. }
  4087. }
  4088. }
  4089. if (chanvars) {
  4090. ast_variables_destroy(chanvars);
  4091. chanvars = NULL;
  4092. }
  4093. return (gw_reload ? NULL : gw);
  4094. }
  4095. static enum ast_rtp_glue_result mgcp_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
  4096. {
  4097. struct mgcp_subchannel *sub = NULL;
  4098. if (!(sub = ast_channel_tech_pvt(chan)) || !(sub->rtp))
  4099. return AST_RTP_GLUE_RESULT_FORBID;
  4100. *instance = sub->rtp ? ao2_ref(sub->rtp, +1), sub->rtp : NULL;
  4101. if (sub->parent->directmedia)
  4102. return AST_RTP_GLUE_RESULT_REMOTE;
  4103. else
  4104. return AST_RTP_GLUE_RESULT_LOCAL;
  4105. }
  4106. static int mgcp_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp, struct ast_rtp_instance *trtp, const struct ast_format_cap *cap, int nat_active)
  4107. {
  4108. /* XXX Is there such thing as video support with MGCP? XXX */
  4109. struct mgcp_subchannel *sub;
  4110. sub = ast_channel_tech_pvt(chan);
  4111. if (sub && !sub->alreadygone) {
  4112. transmit_modify_with_sdp(sub, rtp, cap);
  4113. return 0;
  4114. }
  4115. return -1;
  4116. }
  4117. static void mgcp_get_codec(struct ast_channel *chan, struct ast_format_cap *result)
  4118. {
  4119. struct mgcp_subchannel *sub = ast_channel_tech_pvt(chan);
  4120. struct mgcp_endpoint *p = sub->parent;
  4121. ast_format_cap_append_from_cap(result, p->cap, AST_MEDIA_TYPE_UNKNOWN);
  4122. }
  4123. static struct ast_rtp_glue mgcp_rtp_glue = {
  4124. .type = "MGCP",
  4125. .get_rtp_info = mgcp_get_rtp_peer,
  4126. .update_peer = mgcp_set_rtp_peer,
  4127. .get_codec = mgcp_get_codec,
  4128. };
  4129. static int acf_channel_read(struct ast_channel *chan, const char *funcname, char *args, char *buf, size_t buflen)
  4130. {
  4131. struct mgcp_subchannel *sub = ast_channel_tech_pvt(chan);
  4132. int res = 0;
  4133. /* Sanity check */
  4134. if (!chan || ast_channel_tech(chan) != &mgcp_tech) {
  4135. ast_log(LOG_ERROR, "This function requires a valid MGCP channel\n");
  4136. return -1;
  4137. }
  4138. if (!strcasecmp(args, "ncs")) {
  4139. snprintf(buf, buflen, "%s", sub->parent->ncs ? "yes":"no");
  4140. } else {
  4141. res = -1;
  4142. }
  4143. return res;
  4144. }
  4145. static void destroy_endpoint(struct mgcp_endpoint *e)
  4146. {
  4147. struct mgcp_subchannel *sub = e->sub->next, *s;
  4148. int i;
  4149. for (i = 0; i < MAX_SUBS; i++) {
  4150. ast_mutex_lock(&sub->lock);
  4151. if (!ast_strlen_zero(sub->cxident)) {
  4152. transmit_connection_del(sub);
  4153. }
  4154. if (sub->rtp) {
  4155. ast_rtp_instance_destroy(sub->rtp);
  4156. sub->rtp = NULL;
  4157. }
  4158. memset(sub->magic, 0, sizeof(sub->magic));
  4159. mgcp_queue_hangup(sub);
  4160. dump_cmd_queues(NULL, sub);
  4161. if(sub->gate) {
  4162. sub->gate->tech_pvt = NULL;
  4163. sub->gate->got_dq_gi = NULL;
  4164. sub->gate->gate_remove = NULL;
  4165. sub->gate->gate_open = NULL;
  4166. }
  4167. ast_mutex_unlock(&sub->lock);
  4168. sub = sub->next;
  4169. }
  4170. if (e->dsp) {
  4171. ast_dsp_free(e->dsp);
  4172. }
  4173. dump_queue(e->parent, e);
  4174. dump_cmd_queues(e, NULL);
  4175. sub = e->sub;
  4176. for (i = 0; (i < MAX_SUBS) && sub; i++) {
  4177. s = sub;
  4178. sub = sub->next;
  4179. ast_mutex_destroy(&s->lock);
  4180. ast_mutex_destroy(&s->cx_queue_lock);
  4181. ast_free(s);
  4182. }
  4183. if (e->mwi_event_sub) {
  4184. e->mwi_event_sub = stasis_unsubscribe(e->mwi_event_sub);
  4185. }
  4186. if (e->chanvars) {
  4187. ast_variables_destroy(e->chanvars);
  4188. e->chanvars = NULL;
  4189. }
  4190. ast_mutex_destroy(&e->lock);
  4191. ast_mutex_destroy(&e->rqnt_queue_lock);
  4192. ast_mutex_destroy(&e->cmd_queue_lock);
  4193. ao2_ref(e->cap, -1);
  4194. ast_free(e);
  4195. }
  4196. static void destroy_gateway(struct mgcp_gateway *g)
  4197. {
  4198. if (g->ha)
  4199. ast_free_ha(g->ha);
  4200. dump_queue(g, NULL);
  4201. ast_free(g);
  4202. }
  4203. static void prune_gateways(void)
  4204. {
  4205. struct mgcp_gateway *g, *z, *r;
  4206. struct mgcp_endpoint *e, *p, *t;
  4207. ast_mutex_lock(&gatelock);
  4208. /* prune gateways */
  4209. for (z = NULL, g = gateways; g;) {
  4210. /* prune endpoints */
  4211. for (p = NULL, e = g->endpoints; e; ) {
  4212. if (!g->realtime && (e->delme || g->delme)) {
  4213. t = e;
  4214. e = e->next;
  4215. if (!p)
  4216. g->endpoints = e;
  4217. else
  4218. p->next = e;
  4219. destroy_endpoint(t);
  4220. } else {
  4221. p = e;
  4222. e = e->next;
  4223. }
  4224. }
  4225. if (g->delme) {
  4226. r = g;
  4227. g = g->next;
  4228. if (!z)
  4229. gateways = g;
  4230. else
  4231. z->next = g;
  4232. destroy_gateway(r);
  4233. } else {
  4234. z = g;
  4235. g = g->next;
  4236. }
  4237. }
  4238. ast_mutex_unlock(&gatelock);
  4239. }
  4240. static struct ast_variable *add_var(const char *buf, struct ast_variable *list)
  4241. {
  4242. struct ast_variable *tmpvar = NULL;
  4243. char *varname = ast_strdupa(buf), *varval = NULL;
  4244. if ((varval = strchr(varname, '='))) {
  4245. *varval++ = '\0';
  4246. if ((tmpvar = ast_variable_new(varname, varval, ""))) {
  4247. tmpvar->next = list;
  4248. list = tmpvar;
  4249. }
  4250. }
  4251. return list;
  4252. }
  4253. /*! \brief
  4254. * duplicate a list of channel variables, \return the copy.
  4255. */
  4256. static struct ast_variable *copy_vars(struct ast_variable *src)
  4257. {
  4258. struct ast_variable *res = NULL, *tmp, *v = NULL;
  4259. for (v = src ; v ; v = v->next) {
  4260. if ((tmp = ast_variable_new(v->name, v->value, v->file))) {
  4261. tmp->next = res;
  4262. res = tmp;
  4263. }
  4264. }
  4265. return res;
  4266. }
  4267. static int reload_config(int reload)
  4268. {
  4269. struct ast_config *cfg;
  4270. struct ast_variable *v;
  4271. struct mgcp_gateway *g;
  4272. struct mgcp_endpoint *e;
  4273. char *cat;
  4274. struct ast_hostent ahp;
  4275. struct hostent *hp;
  4276. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  4277. if (gethostname(ourhost, sizeof(ourhost)-1)) {
  4278. ast_log(LOG_WARNING, "Unable to get hostname, MGCP disabled\n");
  4279. return 0;
  4280. }
  4281. cfg = ast_config_load(config, config_flags);
  4282. /* We *must* have a config file otherwise stop immediately */
  4283. if (!cfg) {
  4284. ast_log(LOG_NOTICE, "Unable to load config %s, MGCP disabled\n", config);
  4285. return 0;
  4286. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  4287. return 0;
  4288. } else if (cfg == CONFIG_STATUS_FILEINVALID) {
  4289. ast_log(LOG_ERROR, "Config file %s is in an invalid format. Aborting.\n", config);
  4290. return 0;
  4291. }
  4292. memset(&bindaddr, 0, sizeof(bindaddr));
  4293. dtmfmode = 0;
  4294. /* Copy the default jb config over global_jbconf */
  4295. memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
  4296. for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
  4297. /* handle jb conf */
  4298. if (!ast_jb_read_conf(&global_jbconf, v->name, v->value)) {
  4299. continue;
  4300. }
  4301. /* Create the interface list */
  4302. if (!strcasecmp(v->name, "bindaddr")) {
  4303. if (!(hp = ast_gethostbyname(v->value, &ahp))) {
  4304. ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
  4305. } else {
  4306. memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
  4307. }
  4308. } else if (!strcasecmp(v->name, "allow")) {
  4309. ast_format_cap_update_by_allow_disallow(global_capability, v->value, 1);
  4310. } else if (!strcasecmp(v->name, "disallow")) {
  4311. ast_format_cap_update_by_allow_disallow(global_capability, v->value, 0);
  4312. } else if (!strcasecmp(v->name, "tos")) {
  4313. if (ast_str2tos(v->value, &qos.tos)) {
  4314. ast_log(LOG_WARNING, "Invalid tos value at line %d, refer to QoS documentation\n", v->lineno);
  4315. }
  4316. } else if (!strcasecmp(v->name, "tos_audio")) {
  4317. if (ast_str2tos(v->value, &qos.tos_audio))
  4318. ast_log(LOG_WARNING, "Invalid tos_audio value at line %d, refer to QoS documentation\n", v->lineno);
  4319. } else if (!strcasecmp(v->name, "cos")) {
  4320. if (ast_str2cos(v->value, &qos.cos))
  4321. ast_log(LOG_WARNING, "Invalid cos value at line %d, refer to QoS documentation\n", v->lineno);
  4322. } else if (!strcasecmp(v->name, "cos_audio")) {
  4323. if (ast_str2cos(v->value, &qos.cos_audio))
  4324. ast_log(LOG_WARNING, "Invalid cos_audio value at line %d, refer to QoS documentation\n", v->lineno);
  4325. } else if (!strcasecmp(v->name, "port")) {
  4326. if (sscanf(v->value, "%5d", &ourport) == 1) {
  4327. bindaddr.sin_port = htons(ourport);
  4328. } else {
  4329. ast_log(LOG_WARNING, "Invalid port number '%s' at line %d of %s\n", v->value, v->lineno, config);
  4330. }
  4331. } else if (!strcasecmp(v->name, "firstdigittimeout")) {
  4332. firstdigittimeout = atoi(v->value);
  4333. } else if (!strcasecmp(v->name, "gendigittimeout")) {
  4334. gendigittimeout = atoi(v->value);
  4335. } else if (!strcasecmp(v->name, "matchdigittimeout")) {
  4336. matchdigittimeout = atoi(v->value);
  4337. }
  4338. }
  4339. /* mark existing entries for deletion */
  4340. ast_mutex_lock(&gatelock);
  4341. for (g = gateways; g; g = g->next) {
  4342. g->delme = 1;
  4343. for (e = g->endpoints; e; e = e->next) {
  4344. e->delme = 1;
  4345. }
  4346. }
  4347. ast_mutex_unlock(&gatelock);
  4348. for (cat = ast_category_browse(cfg, NULL); cat; cat = ast_category_browse(cfg, cat)) {
  4349. if (strcasecmp(cat, "general")) {
  4350. ast_mutex_lock(&gatelock);
  4351. if ((g = build_gateway(cat, ast_variable_browse(cfg, cat)))) {
  4352. ast_verb(3, "Added gateway '%s'\n", g->name);
  4353. g->next = gateways;
  4354. gateways = g;
  4355. }
  4356. ast_mutex_unlock(&gatelock);
  4357. /* FS: process queue and IO */
  4358. if (monitor_thread == pthread_self()) {
  4359. if (sched) ast_sched_runq(sched);
  4360. if (io) ast_io_wait(io, 10);
  4361. }
  4362. }
  4363. }
  4364. /* prune deleted entries etc. */
  4365. prune_gateways();
  4366. if (ntohl(bindaddr.sin_addr.s_addr)) {
  4367. memcpy(&__ourip, &bindaddr.sin_addr, sizeof(__ourip));
  4368. } else {
  4369. hp = ast_gethostbyname(ourhost, &ahp);
  4370. if (!hp) {
  4371. ast_log(LOG_WARNING, "Unable to get our IP address, MGCP disabled\n");
  4372. ast_config_destroy(cfg);
  4373. return 0;
  4374. }
  4375. memcpy(&__ourip, hp->h_addr, sizeof(__ourip));
  4376. }
  4377. if (!ntohs(bindaddr.sin_port))
  4378. bindaddr.sin_port = htons(DEFAULT_MGCP_CA_PORT);
  4379. bindaddr.sin_family = AF_INET;
  4380. ast_mutex_lock(&netlock);
  4381. if (mgcpsock > -1)
  4382. close(mgcpsock);
  4383. if (mgcpsock_read_id != NULL)
  4384. ast_io_remove(io, mgcpsock_read_id);
  4385. mgcpsock_read_id = NULL;
  4386. mgcpsock = socket(AF_INET, SOCK_DGRAM, 0);
  4387. if (mgcpsock < 0) {
  4388. ast_log(LOG_WARNING, "Unable to create MGCP socket: %s\n", strerror(errno));
  4389. } else {
  4390. if (bind(mgcpsock, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) < 0) {
  4391. ast_log(LOG_WARNING, "Failed to bind to %s:%d: %s\n",
  4392. ast_inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port),
  4393. strerror(errno));
  4394. close(mgcpsock);
  4395. mgcpsock = -1;
  4396. } else {
  4397. ast_verb(2, "MGCP Listening on %s:%d\n",
  4398. ast_inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port));
  4399. ast_set_qos(mgcpsock, qos.tos, qos.cos, "MGCP");
  4400. }
  4401. }
  4402. ast_mutex_unlock(&netlock);
  4403. ast_config_destroy(cfg);
  4404. /* send audit only to the new endpoints */
  4405. for (g = gateways; g; g = g->next) {
  4406. for (e = g->endpoints; e && e->needaudit; e = e->next) {
  4407. e->needaudit = 0;
  4408. transmit_audit_endpoint(e);
  4409. ast_verb(3, "MGCP Auditing endpoint %s@%s for hookstate\n", e->name, g->name);
  4410. }
  4411. }
  4412. return 0;
  4413. }
  4414. /*!
  4415. * \brief Load the module
  4416. *
  4417. * Module loading including tests for configuration or dependencies.
  4418. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  4419. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  4420. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  4421. * configuration file or other non-critical problem return
  4422. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  4423. */
  4424. static int load_module(void)
  4425. {
  4426. if (!(global_capability = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
  4427. return AST_MODULE_LOAD_FAILURE;
  4428. }
  4429. if (!(mgcp_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
  4430. ao2_ref(global_capability, -1);
  4431. return AST_MODULE_LOAD_FAILURE;
  4432. }
  4433. ast_format_cap_append(global_capability, ast_format_ulaw, 0);
  4434. ast_format_cap_append(mgcp_tech.capabilities, ast_format_ulaw, 0);
  4435. ast_format_cap_append(mgcp_tech.capabilities, ast_format_alaw, 0);
  4436. if (!(sched = ast_sched_context_create())) {
  4437. ast_log(LOG_WARNING, "Unable to create schedule context\n");
  4438. ao2_ref(global_capability, -1);
  4439. ao2_ref(mgcp_tech.capabilities, -1);
  4440. return AST_MODULE_LOAD_FAILURE;
  4441. }
  4442. if (!(io = io_context_create())) {
  4443. ast_log(LOG_WARNING, "Unable to create I/O context\n");
  4444. ast_sched_context_destroy(sched);
  4445. ao2_ref(global_capability, -1);
  4446. ao2_ref(mgcp_tech.capabilities, -1);
  4447. return AST_MODULE_LOAD_FAILURE;
  4448. }
  4449. if (reload_config(0)) {
  4450. ao2_ref(global_capability, -1);
  4451. ao2_ref(mgcp_tech.capabilities, -1);
  4452. return AST_MODULE_LOAD_DECLINE;
  4453. }
  4454. /* Make sure we can register our mgcp channel type */
  4455. if (ast_channel_register(&mgcp_tech)) {
  4456. ast_log(LOG_ERROR, "Unable to register channel class 'MGCP'\n");
  4457. io_context_destroy(io);
  4458. ast_sched_context_destroy(sched);
  4459. ao2_ref(global_capability, -1);
  4460. ao2_ref(mgcp_tech.capabilities, -1);
  4461. return AST_MODULE_LOAD_FAILURE;
  4462. }
  4463. ast_rtp_glue_register(&mgcp_rtp_glue);
  4464. ast_cli_register_multiple(cli_mgcp, sizeof(cli_mgcp) / sizeof(struct ast_cli_entry));
  4465. /* And start the monitor for the first time */
  4466. restart_monitor();
  4467. return AST_MODULE_LOAD_SUCCESS;
  4468. }
  4469. static char *mgcp_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4470. {
  4471. static int deprecated = 0;
  4472. if (e) {
  4473. switch (cmd) {
  4474. case CLI_INIT:
  4475. e->command = "mgcp reload";
  4476. e->usage =
  4477. "Usage: mgcp reload\n"
  4478. " 'mgcp reload' is deprecated. Please use 'reload chan_mgcp.so' instead.\n";
  4479. return NULL;
  4480. case CLI_GENERATE:
  4481. return NULL;
  4482. }
  4483. }
  4484. if (!deprecated && a && a->argc > 0) {
  4485. ast_log(LOG_WARNING, "'mgcp reload' is deprecated. Please use 'reload chan_mgcp.so' instead.\n");
  4486. deprecated = 1;
  4487. }
  4488. ast_mutex_lock(&mgcp_reload_lock);
  4489. if (mgcp_reloading) {
  4490. ast_verbose("Previous mgcp reload not yet done\n");
  4491. } else {
  4492. mgcp_reloading = 1;
  4493. }
  4494. ast_mutex_unlock(&mgcp_reload_lock);
  4495. restart_monitor();
  4496. return CLI_SUCCESS;
  4497. }
  4498. static int reload(void)
  4499. {
  4500. mgcp_reload(NULL, 0, NULL);
  4501. return 0;
  4502. }
  4503. static int unload_module(void)
  4504. {
  4505. struct mgcp_endpoint *e;
  4506. struct mgcp_gateway *g;
  4507. /* Check to see if we're reloading */
  4508. if (ast_mutex_trylock(&mgcp_reload_lock)) {
  4509. ast_log(LOG_WARNING, "MGCP is currently reloading. Unable to remove module.\n");
  4510. return -1;
  4511. } else {
  4512. mgcp_reloading = 1;
  4513. ast_mutex_unlock(&mgcp_reload_lock);
  4514. }
  4515. /* First, take us out of the channel loop */
  4516. ast_channel_unregister(&mgcp_tech);
  4517. /* Shut down the monitoring thread */
  4518. if (!ast_mutex_lock(&monlock)) {
  4519. if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
  4520. pthread_cancel(monitor_thread);
  4521. pthread_kill(monitor_thread, SIGURG);
  4522. pthread_join(monitor_thread, NULL);
  4523. }
  4524. monitor_thread = AST_PTHREADT_STOP;
  4525. ast_mutex_unlock(&monlock);
  4526. } else {
  4527. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  4528. /* We always want to leave this in a consistent state */
  4529. ast_channel_register(&mgcp_tech);
  4530. mgcp_reloading = 0;
  4531. mgcp_reload(NULL, 0, NULL);
  4532. return -1;
  4533. }
  4534. if (!ast_mutex_lock(&gatelock)) {
  4535. for (g = gateways; g; g = g->next) {
  4536. g->delme = 1;
  4537. for (e = g->endpoints; e; e = e->next) {
  4538. e->delme = 1;
  4539. }
  4540. }
  4541. prune_gateways();
  4542. ast_mutex_unlock(&gatelock);
  4543. } else {
  4544. ast_log(LOG_WARNING, "Unable to lock the gateways list.\n");
  4545. /* We always want to leave this in a consistent state */
  4546. ast_channel_register(&mgcp_tech);
  4547. /* Allow the monitor to restart */
  4548. monitor_thread = AST_PTHREADT_NULL;
  4549. mgcp_reloading = 0;
  4550. mgcp_reload(NULL, 0, NULL);
  4551. return -1;
  4552. }
  4553. close(mgcpsock);
  4554. ast_rtp_glue_unregister(&mgcp_rtp_glue);
  4555. ast_cli_unregister_multiple(cli_mgcp, sizeof(cli_mgcp) / sizeof(struct ast_cli_entry));
  4556. ast_sched_context_destroy(sched);
  4557. ao2_ref(global_capability, -1);
  4558. global_capability = NULL;
  4559. ao2_ref(mgcp_tech.capabilities, -1);
  4560. mgcp_tech.capabilities = NULL;
  4561. return 0;
  4562. }
  4563. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Media Gateway Control Protocol (MGCP)",
  4564. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  4565. .load = load_module,
  4566. .unload = unload_module,
  4567. .reload = reload,
  4568. .load_pri = AST_MODPRI_CHANNEL_DRIVER,
  4569. .nonoptreq = "res_pktccops",
  4570. );