chan_mgcp.c 144 KB

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