chan_mgcp.c 145 KB

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