chan_mgcp.c 149 KB

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