chan_mgcp.c 147 KB

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