chan_mgcp.c 137 KB

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