mohq_funcs.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2013 Robert Boisvert
  5. *
  6. * This file is part of the mohqueue module for sip-router, a free SIP server.
  7. *
  8. * The mohqueue module is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * The mohqueue module is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. #include <stdarg.h>
  24. #include "mohq.h"
  25. #include "mohq_db.h"
  26. #include "mohq_funcs.h"
  27. /**********
  28. * definitions
  29. **********/
  30. #define ALLOWHDR "Allow: INVITE, ACK, BYE, CANCEL, NOTIFY, PRACK"
  31. #define CLENHDR "Content-Length"
  32. #define SIPEOL "\r\n"
  33. #define USRAGNT "Kamailio MOH Queue v1.0"
  34. /**********
  35. * local constants
  36. **********/
  37. str p100rel [1] = {STR_STATIC_INIT ("100rel")};
  38. str pallq [1] = {STR_STATIC_INIT ("*")};
  39. str paudio [1] = {STR_STATIC_INIT ("audio")};
  40. str pbye [1] = {STR_STATIC_INIT ("BYE")};
  41. str pinvite [1] = {STR_STATIC_INIT ("INVITE")};
  42. str pmi_nolock [1] = {STR_STATIC_INIT ("Unable to lock queue")};
  43. str pmi_noqueue [1] = {STR_STATIC_INIT ("No matching queue name found")};
  44. str prefer [1] = {STR_STATIC_INIT ("REFER")};
  45. str presp_noaccept [1] = {STR_STATIC_INIT ("Not Acceptable Here")};
  46. str presp_noallow [1] = {STR_STATIC_INIT ("Method Not Allowed")};
  47. str presp_nocall [1] = {STR_STATIC_INIT ("Call/Transaction Does Not Exist")};
  48. str presp_ok [1] = {STR_STATIC_INIT ("OK")};
  49. str presp_reqpend [1] = {STR_STATIC_INIT ("Request Pending")};
  50. str presp_reqterm [1] = {STR_STATIC_INIT ("Request Terminated")};
  51. str presp_ring [1] = {STR_STATIC_INIT ("Ringing")};
  52. str psipfrag [1] = {STR_STATIC_INIT ("message/sipfrag")};
  53. str presp_srverr [1] = {STR_STATIC_INIT ("Server Internal Error")};
  54. str presp_unsupp [1] = {STR_STATIC_INIT ("Unsupported Media Type")};
  55. rtpmap prtpmap [] =
  56. {
  57. {9, "G722/8000"},
  58. {0, "PCMU/8000"},
  59. {8, "PCMA/8000"},
  60. {18, "G729/8000"},
  61. {3, "GSM/8000"},
  62. {4, "G723/8000"},
  63. {15, "G728/8000"},
  64. {5, "DVI4/8000"},
  65. {7, "LPC/8000"},
  66. {12, "QCELP/8000"},
  67. {13, "CN/8000"},
  68. {16, "DVI4/11025"},
  69. {6, "DVI4/16000"},
  70. {17, "DVI4/22050"},
  71. {10, "L16/44100"},
  72. {11, "L16/44100"},
  73. {14, "MPA/90000"},
  74. {0, 0}
  75. };
  76. rtpmap *pmohfiles [30]; // element count should be equal or greater than prtpmap
  77. str pallowhdr [1] = { STR_STATIC_INIT (ALLOWHDR SIPEOL) };
  78. char pbyemsg [] =
  79. {
  80. "%s"
  81. "Max-Forwards: 70" SIPEOL
  82. "Contact: <%s>" SIPEOL
  83. "User-Agent: " USRAGNT SIPEOL
  84. };
  85. str pextrahdr [1] =
  86. {
  87. STR_STATIC_INIT (
  88. ALLOWHDR SIPEOL
  89. "Supported: 100rel" SIPEOL
  90. "Accept-Language: en" SIPEOL
  91. "Content-Type: application/sdp" SIPEOL
  92. "User-Agent: " USRAGNT SIPEOL
  93. )
  94. };
  95. char pinvitesdp [] =
  96. {
  97. "v=0" SIPEOL
  98. "o=- %d %d IN %s" SIPEOL
  99. "s=" USRAGNT SIPEOL
  100. "c=IN %s" SIPEOL
  101. "t=0 0" SIPEOL
  102. "a=send%s" SIPEOL
  103. "m=audio %d RTP/AVP "
  104. };
  105. char prefermsg [] =
  106. {
  107. "%s"
  108. "Max-Forwards: 70" SIPEOL
  109. "Refer-To: <%s>" SIPEOL
  110. "Referred-By: <%.*s>" SIPEOL
  111. "User-Agent: " USRAGNT SIPEOL
  112. };
  113. char preinvitemsg [] =
  114. {
  115. "%s"
  116. "Max-Forwards: 70" SIPEOL
  117. "Contact: <%s>" SIPEOL
  118. ALLOWHDR SIPEOL
  119. "Supported: 100rel" SIPEOL
  120. "User-Agent: " USRAGNT SIPEOL
  121. "Accept-Language: en" SIPEOL
  122. "Content-Type: application/sdp" SIPEOL
  123. };
  124. char prtpsdp [] =
  125. {
  126. "v=0" SIPEOL
  127. // IP address and audio port faked since they will be replaced
  128. "o=- 1 1 IN IP4 1.1.1.1" SIPEOL
  129. "s=" USRAGNT SIPEOL
  130. "c=IN IP4 1.1.1.1" SIPEOL
  131. "t=0 0" SIPEOL
  132. "a=sendrecv" SIPEOL
  133. "m=audio 1 RTP/AVP"
  134. };
  135. /**********
  136. * local function declarations
  137. **********/
  138. void delete_call (call_lst *);
  139. void drop_call (sip_msg_t *, call_lst *);
  140. int find_call (sip_msg_t *, call_lst **);
  141. dlg_t *form_dialog (call_lst *, struct to_body *);
  142. int form_rtp_SDP (str *, call_lst *, char *);
  143. static void invite_cb (struct cell *, int, struct tmcb_params *);
  144. int refer_call (call_lst *, mohq_lock *);
  145. static void refer_cb (struct cell *, int, struct tmcb_params *);
  146. int send_prov_rsp (sip_msg_t *, call_lst *);
  147. int send_rtp_answer (sip_msg_t *, call_lst *);
  148. int search_hdr_ext (struct hdr_field *, str *);
  149. int start_stream (sip_msg_t *, call_lst *, int);
  150. /**********
  151. * local functions
  152. **********/
  153. /**********
  154. * Process ACK Message
  155. *
  156. * INPUT:
  157. * Arg (1) = SIP message pointer
  158. * Arg (2) = call pointer
  159. * OUTPUT: 0=failed
  160. **********/
  161. int ack_msg (sip_msg_t *pmsg, call_lst *pcall)
  162. {
  163. /**********
  164. * part of INVITE?
  165. **********/
  166. char *pfncname = "ack_msg: ";
  167. struct cell *ptrans;
  168. tm_api_t *ptm = pmod_data->ptm;
  169. if (pcall->call_state != CLSTA_INVITED)
  170. {
  171. /**********
  172. * ignore if from rejected re-INVITE
  173. **********/
  174. if (pcall->call_state != CLSTA_INQUEUE)
  175. { LM_ERR ("%sUnexpected ACK (%s)!", pfncname, pcall->call_from); }
  176. else
  177. {
  178. mohq_debug (pcall->pmohq, "%sACK from refused re-INVITE (%s)!",
  179. pfncname, pcall->call_from);
  180. }
  181. return 1;
  182. }
  183. /**********
  184. * o release INVITE transaction
  185. * o save SDP address info
  186. * o put in queue
  187. **********/
  188. if (ptm->t_lookup_ident (&ptrans, pcall->call_hash, pcall->call_label) < 0)
  189. {
  190. LM_ERR ("%sINVITE transaction missing for call (%s)!",
  191. pfncname, pcall->call_from);
  192. return 1;
  193. }
  194. else
  195. {
  196. if (ptm->t_release (pcall->call_pmsg) < 0)
  197. {
  198. LM_ERR ("%sRelease transaction failed for call (%s)!",
  199. pfncname, pcall->call_from);
  200. return 1;
  201. }
  202. }
  203. pcall->call_hash = pcall->call_label = 0;
  204. sprintf (pcall->call_addr, "%s %s",
  205. pmsg->rcv.dst_ip.af == AF_INET ? "IP4" : "IP6",
  206. ip_addr2a (&pmsg->rcv.dst_ip));
  207. pcall->call_state = CLSTA_INQUEUE;
  208. update_call_rec (pcall);
  209. pcall->call_cseq = 1;
  210. mohq_debug (pcall->pmohq,
  211. "%sACK received for call (%s); placed in queue (%s)",
  212. pfncname, pcall->call_from, pcall->pmohq->mohq_name);
  213. return 1;
  214. }
  215. /**********
  216. * BYE Callback
  217. *
  218. * INPUT:
  219. * Arg (1) = cell pointer
  220. * Arg (2) = callback type
  221. * Arg (3) = callback parms
  222. * OUTPUT: none
  223. **********/
  224. static void bye_cb
  225. (struct cell *ptrans, int ntype, struct tmcb_params *pcbp)
  226. {
  227. /**********
  228. * o error means must have hung after REFER
  229. * o delete the call
  230. **********/
  231. char *pfncname = "bye_cb: ";
  232. call_lst *pcall = (call_lst *)*pcbp->param;
  233. if (ntype == TMCB_ON_FAILURE)
  234. {
  235. LM_ERR ("%sCall (%s) did not respond to BYE", pfncname,
  236. pcall->call_from);
  237. }
  238. else
  239. {
  240. int nreply = pcbp->code;
  241. if ((nreply / 100) != 2)
  242. {
  243. LM_ERR ("%sCall (%s) BYE error (%d)", pfncname,
  244. pcall->call_from, nreply);
  245. }
  246. else
  247. {
  248. mohq_debug (pcall->pmohq, "%sCall (%s) BYE reply=%d", pfncname,
  249. pcall->call_from, nreply);
  250. }
  251. }
  252. delete_call (pcall);
  253. return;
  254. }
  255. /**********
  256. * Process BYE Message
  257. *
  258. * INPUT:
  259. * Arg (1) = SIP message pointer
  260. * Arg (2) = call pointer
  261. * OUTPUT: 0=failed
  262. **********/
  263. int bye_msg (sip_msg_t *pmsg, call_lst *pcall)
  264. {
  265. /**********
  266. * o send OK
  267. * o teardown call
  268. **********/
  269. char *pfncname = "bye_msg: ";
  270. if (pmod_data->psl->freply (pmsg, 200, presp_ok) < 0)
  271. {
  272. LM_ERR ("%sUnable to create reply to call (%s)", pfncname,
  273. pcall->call_from);
  274. return 1;
  275. }
  276. if (pcall->call_state >= CLSTA_INQUEUE)
  277. { drop_call (pmsg, pcall); }
  278. else
  279. {
  280. LM_ERR ("%sEnding call (%s) before placed in queue!",
  281. pfncname, pcall->call_from);
  282. delete_call (pcall);
  283. }
  284. return 1;
  285. }
  286. /**********
  287. * Process CANCEL Message
  288. *
  289. * INPUT:
  290. * Arg (1) = SIP message pointer
  291. * Arg (2) = call pointer
  292. * OUTPUT: 0=failed
  293. **********/
  294. int cancel_msg (sip_msg_t *pmsg, call_lst *pcall)
  295. {
  296. /**********
  297. * still in INVITE dialog?
  298. **********/
  299. char *pfncname = "cancel_msg: ";
  300. if (pcall->call_state < CLSTA_INQUEUE)
  301. {
  302. pcall->call_state = CLSTA_CANCEL;
  303. mohq_debug (pcall->pmohq, "%sCANCELed call (%s)",
  304. pfncname, pcall->call_from);
  305. if (pmod_data->psl->freply (pmsg, 487, presp_reqterm) < 0)
  306. { LM_ERR ("%sUnable to create reply!", pfncname); }
  307. }
  308. else
  309. {
  310. LM_ERR ("%sUnable to CANCEL because accepted INVITE for call (%s)!",
  311. pfncname, pcall->call_from);
  312. if (pmod_data->psl->freply (pmsg, 481, presp_nocall) < 0)
  313. { LM_ERR ("%sUnable to create reply!", pfncname); }
  314. }
  315. return 1;
  316. }
  317. /**********
  318. * Close the Call
  319. *
  320. * INPUT:
  321. * Arg (1) = SIP message pointer
  322. * Arg (2) = call pointer
  323. * OUTPUT: none
  324. **********/
  325. void close_call (sip_msg_t *pmsg, call_lst *pcall)
  326. {
  327. /**********
  328. * o destroy proxy connection
  329. * o create dialog
  330. **********/
  331. char *pfncname = "close_call: ";
  332. int bsent = 0;
  333. char *phdr = 0;
  334. if (pmsg != FAKED_REPLY)
  335. {
  336. mohq_debug (pcall->pmohq, "%sDestroying RTP link for call (%s)",
  337. pfncname, pcall->call_from);
  338. if (pmod_data->fn_rtp_destroy (pmsg, 0, 0) != 1)
  339. {
  340. LM_ERR ("%srtpproxy_destroy refused for call (%s)!",
  341. pfncname, pcall->call_from);
  342. }
  343. }
  344. struct to_body ptob [2];
  345. dlg_t *pdlg = form_dialog (pcall, ptob);
  346. if (!pdlg)
  347. { goto bye_err; }
  348. pdlg->state = DLG_CONFIRMED;
  349. /**********
  350. * form BYE header
  351. * o calculate size
  352. * o create buffer
  353. **********/
  354. tm_api_t *ptm = pmod_data->ptm;
  355. char *pquri = pcall->pmohq->mohq_uri;
  356. int npos1 = sizeof (pbyemsg) // BYE template
  357. + strlen (pcall->call_via) // Via
  358. + strlen (pquri); // Contact
  359. phdr = pkg_malloc (npos1);
  360. if (!phdr)
  361. {
  362. LM_ERR ("%sNo more memory!", pfncname);
  363. goto bye_err;
  364. }
  365. sprintf (phdr, pbyemsg,
  366. pcall->call_via, // Via
  367. pquri); // Contact
  368. str phdrs [1];
  369. phdrs->s = phdr;
  370. phdrs->len = strlen (phdr);
  371. /**********
  372. * send BYE request
  373. **********/
  374. uac_req_t puac [1];
  375. set_uac_req (puac, pbye, phdrs, 0, pdlg,
  376. TMCB_LOCAL_COMPLETED | TMCB_ON_FAILURE, bye_cb, pcall);
  377. pcall->call_state = CLSTA_BYE;
  378. if (ptm->t_request_within (puac) < 0)
  379. {
  380. LM_ERR ("%sUnable to create BYE request for call (%s)!",
  381. pfncname, pcall->call_from);
  382. goto bye_err;
  383. }
  384. mohq_debug (pcall->pmohq, "%sSent BYE request for call (%s)",
  385. pfncname, pcall->call_from);
  386. bsent = 1;
  387. /**********
  388. * o free memory
  389. * o delete call
  390. **********/
  391. bye_err:
  392. if (pdlg)
  393. { pkg_free (pdlg); }
  394. if (phdr)
  395. { pkg_free (phdr); }
  396. if (!bsent)
  397. { delete_call (pcall); }
  398. return;
  399. }
  400. /**********
  401. * Create New Call Record
  402. *
  403. * INPUT:
  404. * Arg (1) = queue index
  405. * Arg (2) = SIP message pointer
  406. * OUTPUT: call index; -1 if unable to create
  407. **********/
  408. int create_call (int mohq_idx, sip_msg_t *pmsg)
  409. {
  410. /**********
  411. * o lock calls
  412. * o already in use?
  413. * o find inactive slot
  414. **********/
  415. char *pfncname = "create_call: ";
  416. if (!mohq_lock_set (pmod_data->pcall_lock, 1, 2000))
  417. {
  418. LM_ERR ("%sUnable to lock calls!", pfncname);
  419. return -1;
  420. }
  421. call_lst *pcall;
  422. int ncall_idx = find_call (pmsg, &pcall);
  423. if (pcall)
  424. {
  425. mohq_lock_release (pmod_data->pcall_lock);
  426. LM_ERR ("%sCall already in use (%s)!", pfncname, pcall->call_from);
  427. return -1;
  428. }
  429. for (ncall_idx = 0; ncall_idx < pmod_data->call_cnt; ncall_idx++)
  430. {
  431. if (!pmod_data->pcall_lst [ncall_idx].call_active)
  432. { break; }
  433. }
  434. if (ncall_idx == pmod_data->call_cnt)
  435. {
  436. mohq_lock_release (pmod_data->pcall_lock);
  437. LM_ERR ("%sNo call slots available!", pfncname);
  438. return -1;
  439. }
  440. /**********
  441. * add values to new entry
  442. **********/
  443. pcall = &pmod_data->pcall_lst [ncall_idx];
  444. pcall->call_active = 1;
  445. pcall->pmohq = &pmod_data->pmohq_lst [mohq_idx];
  446. pcall->call_state = 0;
  447. str *pstr = &pmsg->callid->body;
  448. strncpy (pcall->call_id, pstr->s, pstr->len);
  449. pcall->call_id [pstr->len] = '\0';
  450. pstr = &pmsg->from->body;
  451. strncpy (pcall->call_from, pstr->s, pstr->len);
  452. pcall->call_from [pstr->len] = '\0';
  453. *pcall->call_tag = '\0';
  454. if (!pmsg->contact)
  455. { *pcall->call_contact = '\0'; }
  456. else
  457. {
  458. pstr = &pmsg->contact->body;
  459. strncpy (pcall->call_contact, pstr->s, pstr->len);
  460. pcall->call_contact [pstr->len] = '\0';
  461. }
  462. /**********
  463. * extract Via headers
  464. **********/
  465. hdr_field_t *phdr = pmsg->h_via1;
  466. if (phdr)
  467. {
  468. int npos1 = 0;
  469. while ((phdr = next_sibling_hdr (phdr)))
  470. {
  471. struct via_body *pvia;
  472. char *pviabuf;
  473. int bovrflow = 0;
  474. int npos2;
  475. int nvia_max = sizeof (pcall->call_via);
  476. for (pvia = (struct via_body *)phdr->parsed; pvia; pvia = pvia->next)
  477. {
  478. /**********
  479. * o skip trailing whitespace
  480. * o check if overflow
  481. **********/
  482. npos2 = pvia->bsize;
  483. pviabuf = pvia->name.s;
  484. while (npos2)
  485. {
  486. --npos2;
  487. if (pviabuf [npos2] == ' ' || pviabuf [npos2] == '\r'
  488. || pviabuf [npos2] == '\n' || pviabuf [npos2] == '\t' || pviabuf [npos2] == ',')
  489. { continue; }
  490. break;
  491. }
  492. if ((npos2 + npos1 + 7) >= nvia_max)
  493. {
  494. LM_WARN ("%sVia buffer overflowed!", pfncname);
  495. bovrflow = 1;
  496. break;
  497. }
  498. /**********
  499. * copy via
  500. **********/
  501. strcpy (&pcall->call_via [npos1], "Via: ");
  502. npos1 += 5;
  503. strncpy (&pcall->call_via [npos1], pviabuf, npos2);
  504. npos1 += npos2;
  505. strcpy (&pcall->call_via [npos1], SIPEOL);
  506. npos1 += 2;
  507. }
  508. if (bovrflow)
  509. { break; }
  510. }
  511. }
  512. /**********
  513. * o release call lock
  514. * o update DB
  515. * o lock MOH queue
  516. **********/
  517. pcall->call_state = CLSTA_ENTER;
  518. mohq_lock_release (pmod_data->pcall_lock);
  519. add_call_rec (ncall_idx);
  520. mohq_lock_set (pmod_data->pmohq_lock, 0, 0);
  521. mohq_debug (pcall->pmohq, "%sAdded call (%s) to queue (%s)",
  522. pfncname, pcall->call_from, pcall->pmohq->mohq_name);
  523. return ncall_idx;
  524. }
  525. /**********
  526. * Delete Call
  527. *
  528. * INPUT:
  529. * Arg (1) = call pointer
  530. * OUTPUT: none
  531. **********/
  532. void delete_call (call_lst *pcall)
  533. {
  534. /**********
  535. * release transaction
  536. **********/
  537. char *pfncname = "delete_call: ";
  538. struct cell *ptrans;
  539. tm_api_t *ptm = pmod_data->ptm;
  540. if (pcall->call_hash || pcall->call_label)
  541. {
  542. if (ptm->t_lookup_ident (&ptrans, pcall->call_hash, pcall->call_label) < 0)
  543. {
  544. LM_ERR ("%sLookup transaction failed for call (%s)!", pfncname,
  545. pcall->call_from);
  546. }
  547. else
  548. {
  549. if (ptm->t_release (pcall->call_pmsg) < 0)
  550. {
  551. LM_ERR ("%sRelease transaction failed for call (%s)!",
  552. pfncname, pcall->call_from);
  553. }
  554. }
  555. pcall->call_hash = pcall->call_label = 0;
  556. }
  557. /**********
  558. * o update DB
  559. * o inactivate slot
  560. * o release MOH queue
  561. **********/
  562. mohq_debug (pcall->pmohq, "delete_call: Deleting call (%s) from queue (%s)",
  563. pcall->call_from, pcall->pmohq->mohq_name);
  564. delete_call_rec (pcall);
  565. pcall->call_active = 0;
  566. mohq_lock_release (pmod_data->pmohq_lock);
  567. return;
  568. }
  569. /**********
  570. * Deny Method
  571. *
  572. * INPUT:
  573. * Arg (1) = SIP message pointer
  574. * Arg (2) = call pointer
  575. * OUTPUT: none
  576. **********/
  577. void deny_method (sip_msg_t *pmsg, call_lst *pcall)
  578. {
  579. /**********
  580. * RFC 3261 section 8.2.1
  581. * o get transaction
  582. * o respond with 405 and Allow header
  583. **********/
  584. char *pfncname = "deny_method: ";
  585. tm_api_t *ptm = pmod_data->ptm;
  586. if (ptm->t_newtran (pmsg) < 0)
  587. {
  588. LM_ERR ("%sUnable to create new transaction!", pfncname);
  589. if (pmod_data->psl->freply (pmsg, 500, presp_srverr) < 0)
  590. {
  591. LM_ERR ("%sUnable to create reply to %.*s!", pfncname,
  592. STR_FMT (&REQ_LINE (pmsg).method));
  593. }
  594. return;
  595. }
  596. if (!add_lump_rpl2 (pmsg, pallowhdr->s, pallowhdr->len, LUMP_RPL_HDR))
  597. { LM_ERR ("%sUnable to add Allow header!", pfncname); }
  598. LM_ERR ("%sRefused %.*s for call (%s)!", pfncname,
  599. STR_FMT (&REQ_LINE (pmsg).method), pcall->call_from);
  600. if (ptm->t_reply (pmsg, 405, presp_noallow->s) < 0)
  601. {
  602. LM_ERR ("%sUnable to create reply to %.*s!", pfncname,
  603. STR_FMT (&REQ_LINE (pmsg).method));
  604. }
  605. return;
  606. }
  607. /**********
  608. * Drop the Call
  609. *
  610. * INPUT:
  611. * Arg (1) = SIP message pointer
  612. * Arg (2) = call pointer
  613. * OUTPUT: none
  614. **********/
  615. void drop_call (sip_msg_t *pmsg, call_lst *pcall)
  616. {
  617. /**********
  618. * o destroy proxy connection
  619. * o delete call
  620. **********/
  621. char *pfncname = "drop_call: ";
  622. if (pmsg != FAKED_REPLY)
  623. {
  624. mohq_debug (pcall->pmohq, "%sDestroying RTP link for call (%s)",
  625. pfncname, pcall->call_from);
  626. if (pmod_data->fn_rtp_destroy (pmsg, 0, 0) != 1)
  627. {
  628. LM_ERR ("%srtpproxy_destroy refused for call (%s)!",
  629. pfncname, pcall->call_from);
  630. }
  631. }
  632. delete_call (pcall);
  633. return;
  634. }
  635. /**********
  636. * Find Call
  637. *
  638. * INPUT:
  639. * Arg (1) = SIP message pointer
  640. * Arg (2) = pointer to call pointer
  641. * OUTPUT: queue index; -1 if unable to find
  642. **********/
  643. int find_call (sip_msg_t *pmsg, call_lst **ppcall)
  644. {
  645. /**********
  646. * o find current RURI
  647. * o strip off parms or headers
  648. * o search MOH queue
  649. **********/
  650. str *pruri =
  651. pmsg->new_uri.s ? &pmsg->new_uri : &pmsg->first_line.u.request.uri;
  652. int nidx;
  653. str pstr [1];
  654. pstr->s = pruri->s;
  655. pstr->len = pruri->len;
  656. for (nidx = 0; nidx < pruri->len; nidx++)
  657. {
  658. if (pstr->s [nidx] == ';' || pstr->s [nidx] == '?')
  659. {
  660. pstr->len = nidx;
  661. break;
  662. }
  663. }
  664. mohq_lst *pqlst = pmod_data->pmohq_lst;
  665. int nqidx;
  666. for (nqidx = 0; nqidx < pmod_data->mohq_cnt; nqidx++)
  667. {
  668. str pmohstr [1];
  669. pmohstr->s = pqlst [nqidx].mohq_uri;
  670. pmohstr->len = strlen (pmohstr->s);
  671. if (STR_EQ (*pmohstr, *pstr))
  672. { break; }
  673. }
  674. *ppcall = 0;
  675. if (nqidx == pmod_data->mohq_cnt)
  676. { return -1;}
  677. /**********
  678. * o get to tag
  679. * o get callID
  680. * o ignore to tag if CANCEL on first INVITE
  681. * o search call queue
  682. **********/
  683. str *ptotag = &(get_to (pmsg)->tag_value);
  684. if (!ptotag->len)
  685. { ptotag = 0; }
  686. if (!pmsg->callid)
  687. { return -1; }
  688. str *pcallid = &pmsg->callid->body;
  689. if (!pcallid)
  690. { return -1; }
  691. for (nidx = 0; nidx < pmod_data->call_cnt; nidx++)
  692. {
  693. /**********
  694. * o call active?
  695. * o call timed out on ACK?
  696. * o callID matches?
  697. * o to tag matches?
  698. * o return call pointer
  699. **********/
  700. call_lst *pcall = &pmod_data->pcall_lst [nidx];
  701. if (!pcall->call_active)
  702. { continue; }
  703. if (pcall->call_time && (pcall->call_state < CLSTA_INQUEUE))
  704. {
  705. if ((pcall->call_time + 32) < time (0))
  706. {
  707. LM_ERR ("find_call: No ACK response for call (%s)", pcall->call_from);
  708. delete_call (pcall);
  709. continue;
  710. }
  711. }
  712. str tmpstr [1];
  713. tmpstr->s = pcall->call_id;
  714. tmpstr->len = strlen (tmpstr->s);
  715. if (!STR_EQ (*tmpstr, *pcallid))
  716. { continue; }
  717. if (ptotag)
  718. {
  719. tmpstr->s = pcall->call_tag;
  720. tmpstr->len = strlen (tmpstr->s);
  721. if (!STR_EQ (*tmpstr, *ptotag))
  722. { continue; }
  723. }
  724. *ppcall = pcall;
  725. return nqidx;
  726. }
  727. /**********
  728. * first INVITE?
  729. **********/
  730. if (pmsg->REQ_METHOD == METHOD_INVITE)
  731. { return 0; }
  732. return -1;
  733. }
  734. /**********
  735. * Find Queue
  736. *
  737. * INPUT:
  738. * Arg (1) = queue name str pointer
  739. * OUTPUT: queue index; -1 if unable to find
  740. **********/
  741. int find_queue (str *pqname)
  742. {
  743. char *pfncname = "find_queue: ";
  744. int nidx;
  745. str tmpstr;
  746. if (!mohq_lock_set (pmod_data->pmohq_lock, 0, 500))
  747. {
  748. LM_ERR ("%sUnable to lock queues!", pfncname);
  749. return -1;
  750. }
  751. for (nidx = 0; nidx < pmod_data->mohq_cnt; nidx++)
  752. {
  753. tmpstr.s = pmod_data->pmohq_lst [nidx].mohq_name;
  754. tmpstr.len = strlen (tmpstr.s);
  755. if (STR_EQ (tmpstr, *pqname))
  756. { break; }
  757. }
  758. if (nidx == pmod_data->mohq_cnt)
  759. {
  760. LM_ERR ("%sUnable to find queue (%.*s)!", pfncname, STR_FMT (pqname));
  761. nidx = -1;
  762. }
  763. mohq_lock_release (pmod_data->pmohq_lock);
  764. return nidx;
  765. }
  766. /**********
  767. * Find Referred Call
  768. *
  769. * INPUT:
  770. * Arg (1) = referred-by value
  771. * OUTPUT: call index; -1 if unable to find
  772. **********/
  773. int find_referred_call (str *pvalue)
  774. {
  775. /**********
  776. * get URI
  777. **********/
  778. char *pfncname = "find_referred_call: ";
  779. struct to_body pref [1];
  780. parse_to (pvalue->s, &pvalue->s [pvalue->len + 1], pref);
  781. if (pref->error != PARSE_OK)
  782. {
  783. // should never happen
  784. LM_ERR ("%sInvalid Referred-By URI (%.*s)!", pfncname, STR_FMT (pvalue));
  785. return -1;
  786. }
  787. if (pref->param_lst)
  788. { free_to_params (pref); }
  789. /**********
  790. * search calls for matching
  791. **********/
  792. int nidx;
  793. str tmpstr;
  794. struct to_body pfrom [1];
  795. for (nidx = 0; nidx < pmod_data->call_cnt; nidx++)
  796. {
  797. if (!pmod_data->pcall_lst [nidx].call_active)
  798. { continue; }
  799. tmpstr.s = pmod_data->pcall_lst [nidx].call_from;
  800. tmpstr.len = strlen (tmpstr.s);
  801. parse_to (tmpstr.s, &tmpstr.s [tmpstr.len + 1], pfrom);
  802. if (pfrom->error != PARSE_OK)
  803. {
  804. // should never happen
  805. LM_ERR ("%sInvalid From URI (%.*s)!", pfncname, STR_FMT (&tmpstr));
  806. continue;
  807. }
  808. if (pfrom->param_lst)
  809. { free_to_params (pfrom); }
  810. if (STR_EQ (pfrom->uri, pref->uri))
  811. { return nidx; }
  812. }
  813. return -1;
  814. }
  815. /**********
  816. * Process First INVITE Message
  817. *
  818. * INPUT:
  819. * Arg (1) = SIP message pointer
  820. * Arg (2) = queue index
  821. * OUTPUT: 0=failed
  822. **********/
  823. int first_invite_msg (sip_msg_t *pmsg, int mohq_idx)
  824. {
  825. /**********
  826. * create call record
  827. **********/
  828. char *pfncname = "first_invite_msg: ";
  829. int ncall_idx = create_call (mohq_idx, pmsg);
  830. if (ncall_idx == -1)
  831. { return 0; }
  832. call_lst *pcall = &pmod_data->pcall_lst [ncall_idx];
  833. /**********
  834. * o SDP exists?
  835. * o accepts REFER?
  836. * o send rtpproxy offer
  837. **********/
  838. if (!(pmsg->msg_flags & FL_SDP_BODY))
  839. {
  840. if (parse_sdp (pmsg))
  841. {
  842. LM_ERR ("%sINVITE lacks SDP (%s)!", pfncname, pcall->call_from);
  843. delete_call (pcall);
  844. return 0;
  845. }
  846. }
  847. if (pmsg->allow)
  848. {
  849. if (!search_hdr_ext (pmsg->allow, prefer))
  850. {
  851. LM_ERR ("%sMissing REFER support (%s)!", pfncname, pcall->call_from);
  852. delete_call (pcall);
  853. return 0;
  854. }
  855. }
  856. mohq_debug (pcall->pmohq, "%sMaking offer for RTP link for call (%s)",
  857. pfncname, pcall->call_from);
  858. if (pmod_data->fn_rtp_offer (pmsg, 0, 0) != 1)
  859. {
  860. LM_ERR ("%srtpproxy_offer refused for call (%s)!",
  861. pfncname, pcall->call_from);
  862. delete_call (pcall);
  863. return 0;
  864. }
  865. /**********
  866. * o create new transaction
  867. * o save To tag
  868. * o catch failures
  869. * o save transaction data
  870. **********/
  871. tm_api_t *ptm = pmod_data->ptm;
  872. if (ptm->t_newtran (pmsg) < 0)
  873. {
  874. LM_ERR ("%sUnable to create new transaction for call (%s)!",
  875. pfncname, pcall->call_from);
  876. delete_call (pcall);
  877. return 0;
  878. }
  879. struct cell *ptrans = ptm->t_gett ();
  880. pcall->call_hash = ptrans->hash_index;
  881. pcall->call_label = ptrans->label;
  882. str ptotag [1];
  883. if (ptm->t_get_reply_totag (pmsg, ptotag) != 1)
  884. {
  885. LM_ERR ("%sUnable to create totag for call (%s)!",
  886. pfncname, pcall->call_from);
  887. if (ptm->t_reply (pmsg, 500, presp_srverr->s) < 0)
  888. { LM_ERR ("%sUnable to reply to INVITE!", pfncname); }
  889. delete_call (pcall);
  890. return 1;
  891. }
  892. strncpy (pcall->call_tag, ptotag->s, ptotag->len);
  893. pcall->call_tag [ptotag->len] = '\0';
  894. pcall->call_cseq = 1;
  895. if (ptm->register_tmcb (pmsg, 0, TMCB_DESTROY | TMCB_ON_FAILURE,
  896. invite_cb, pcall, 0) < 0)
  897. {
  898. LM_ERR ("%sUnable to set callback for call (%s)!",
  899. pfncname, pcall->call_from);
  900. if (ptm->t_reply (pmsg, 500, presp_srverr->s) < 0)
  901. { LM_ERR ("%sUnable to reply to INVITE!", pfncname); }
  902. delete_call (pcall);
  903. return 1;
  904. }
  905. /**********
  906. * o add contact to reply
  907. * o supports/requires PRACK? (RFC 3262 section 3)
  908. * o exit if not ringing
  909. **********/
  910. str pcontact [1];
  911. char *pcontacthdr = "Contact: <%s>" SIPEOL;
  912. pcontact->s = pkg_malloc (strlen (pmod_data->pmohq_lst [mohq_idx].mohq_uri)
  913. + strlen (pcontacthdr));
  914. if (!pcontact->s)
  915. {
  916. LM_ERR ("%sNo more memory!", pfncname);
  917. delete_call (pcall);
  918. return 1;
  919. }
  920. sprintf (pcontact->s, pcontacthdr, pmod_data->pmohq_lst [mohq_idx].mohq_uri);
  921. pcontact->len = strlen (pcontact->s);
  922. if (!add_lump_rpl2 (pmsg, pcontact->s, pcontact->len, LUMP_RPL_HDR))
  923. {
  924. LM_ERR ("%sUnable to add contact (%s) to call (%s)!",
  925. pfncname, pcontact->s, pcall->call_from);
  926. }
  927. pkg_free (pcontact->s);
  928. pcall->call_pmsg = pmsg;
  929. if (search_hdr_ext (pmsg->require, p100rel))
  930. {
  931. if (!send_prov_rsp (pmsg, pcall))
  932. {
  933. delete_call (pcall);
  934. return 1;
  935. }
  936. }
  937. else
  938. {
  939. if (ptm->t_reply (pmsg, 180, presp_ring->s) < 0)
  940. {
  941. LM_ERR ("%sUnable to reply to INVITE!", pfncname);
  942. return 1;
  943. }
  944. else
  945. {
  946. pcall->call_state = CLSTA_RINGING;
  947. mohq_debug (pcall->pmohq, "%sSent RINGING for call (%s)",
  948. pfncname, pcall->call_from);
  949. }
  950. }
  951. /**********
  952. * o call cancelled?
  953. * o accept call with RTP
  954. **********/
  955. if (pcall->call_state == CLSTA_CANCEL)
  956. {
  957. delete_call (pcall);
  958. return 1;
  959. }
  960. if (!send_rtp_answer (pmsg, pcall))
  961. {
  962. if (pmod_data->psl->freply (pmsg, 500, presp_srverr) < 0)
  963. { LM_ERR ("%sUnable to create reply!", pfncname); }
  964. delete_call (pcall);
  965. }
  966. return 1;
  967. }
  968. /**********
  969. * Form Dialog
  970. *
  971. * INPUT:
  972. * Arg (1) = call pointer
  973. * Arg (2) = to_body [2] pointer
  974. * OUTPUT: dlg_t * if successful; 0=if not
  975. **********/
  976. dlg_t *form_dialog (call_lst *pcall, struct to_body *pto_body)
  977. {
  978. /**********
  979. * get from/to values
  980. **********/
  981. char *pfncname = "form_dialog: ";
  982. struct to_body *ptob = &pto_body [0];
  983. struct to_body *pcontact = &pto_body [1];
  984. parse_to (pcall->call_from,
  985. &pcall->call_from [strlen (pcall->call_from) + 1], ptob);
  986. if (ptob->error != PARSE_OK)
  987. {
  988. // should never happen
  989. LM_ERR ("%sInvalid from URI (%s)!", pfncname, pcall->call_from);
  990. return 0;
  991. }
  992. if (ptob->param_lst)
  993. { free_to_params (ptob); }
  994. str ptarget [1];
  995. if (!*pcall->call_contact)
  996. {
  997. ptarget->s = ptob->uri.s;
  998. ptarget->len = ptob->uri.len;
  999. }
  1000. else
  1001. {
  1002. parse_to (pcall->call_contact,
  1003. &pcall->call_contact [strlen (pcall->call_contact) + 1], pcontact);
  1004. if (pcontact->error != PARSE_OK)
  1005. {
  1006. // should never happen
  1007. LM_ERR ("%sInvalid contact (%s) for call (%s)!", pfncname,
  1008. pcall->call_contact, pcall->call_from);
  1009. return 0;
  1010. }
  1011. if (pcontact->param_lst)
  1012. { free_to_params (pcontact); }
  1013. ptarget->s = pcontact->uri.s;
  1014. ptarget->len = pcontact->uri.len;
  1015. }
  1016. /**********
  1017. * create dialog
  1018. **********/
  1019. dlg_t *pdlg = (dlg_t *)pkg_malloc (sizeof (dlg_t));
  1020. if (!pdlg)
  1021. {
  1022. LM_ERR ("%sNo more memory!", pfncname);
  1023. return 0;
  1024. }
  1025. memset (pdlg, 0, sizeof (dlg_t));
  1026. pdlg->loc_seq.value = pcall->call_cseq++;
  1027. pdlg->loc_seq.is_set = 1;
  1028. pdlg->id.call_id.s = pcall->call_id;
  1029. pdlg->id.call_id.len = strlen (pcall->call_id);
  1030. pdlg->id.loc_tag.s = pcall->call_tag;
  1031. pdlg->id.loc_tag.len = strlen (pcall->call_tag);
  1032. pdlg->id.rem_tag.s = ptob->tag_value.s;
  1033. pdlg->id.rem_tag.len = ptob->tag_value.len;
  1034. pdlg->rem_target.s = ptarget->s;
  1035. pdlg->rem_target.len = ptarget->len;
  1036. pdlg->loc_uri.s = pcall->pmohq->mohq_uri;
  1037. pdlg->loc_uri.len = strlen (pdlg->loc_uri.s);
  1038. pdlg->rem_uri.s = ptob->uri.s;
  1039. pdlg->rem_uri.len = ptob->uri.len;
  1040. return pdlg;
  1041. }
  1042. /**********
  1043. * Form RTP SDP String
  1044. *
  1045. * INPUT:
  1046. * Arg (1) = string pointer
  1047. * Arg (2) = call pointer
  1048. * Arg (3) = SDP body pointer
  1049. * OUTPUT: 0 if failed
  1050. **********/
  1051. int form_rtp_SDP (str *pstr, call_lst *pcall, char *pSDP)
  1052. {
  1053. /**********
  1054. * o find available files
  1055. * o calculate size of SDP
  1056. **********/
  1057. char *pfncname = "form_rtp_SDP: ";
  1058. rtpmap **pmohfiles = find_MOH (pcall->pmohq->mohq_mohdir,
  1059. pcall->pmohq->mohq_mohfile);
  1060. if (!pmohfiles [0])
  1061. {
  1062. LM_ERR ("%sUnable to find any MOH files for queue (%s)!", pfncname,
  1063. pcall->pmohq->mohq_name);
  1064. return 0;
  1065. }
  1066. int nsize = strlen (pSDP) + 2;
  1067. int nidx;
  1068. for (nidx = 0; pmohfiles [nidx]; nidx++)
  1069. {
  1070. nsize += strlen (pmohfiles [nidx]->pencode) // encode length
  1071. + 19; // space, type number, "a=rtpmap:%d ", EOL
  1072. }
  1073. /**********
  1074. * o allocate memory
  1075. * o form SDP
  1076. **********/
  1077. pstr->s = pkg_malloc (nsize + 1);
  1078. if (!pstr->s)
  1079. {
  1080. LM_ERR ("%sNo more memory!", pfncname);
  1081. return 0;
  1082. }
  1083. strcpy (pstr->s, pSDP);
  1084. nsize = strlen (pstr->s);
  1085. for (nidx = 0; pmohfiles [nidx]; nidx++)
  1086. {
  1087. /**********
  1088. * add payload types to media description
  1089. **********/
  1090. sprintf (&pstr->s [nsize], " %d", pmohfiles [nidx]->ntype);
  1091. nsize += strlen (&pstr->s [nsize]);
  1092. }
  1093. strcpy (&pstr->s [nsize], SIPEOL);
  1094. nsize += 2;
  1095. for (nidx = 0; pmohfiles [nidx]; nidx++)
  1096. {
  1097. /**********
  1098. * add rtpmap attributes
  1099. **********/
  1100. sprintf (&pstr->s [nsize], "a=rtpmap:%d %s %s",
  1101. pmohfiles [nidx]->ntype, pmohfiles [nidx]->pencode, SIPEOL);
  1102. nsize += strlen (&pstr->s [nsize]);
  1103. }
  1104. pstr->len = nsize;
  1105. return 1;
  1106. }
  1107. /**********
  1108. * Invite Callback
  1109. *
  1110. * INPUT:
  1111. * Arg (1) = cell pointer
  1112. * Arg (2) = callback type
  1113. * Arg (3) = callback parms
  1114. * OUTPUT: none
  1115. **********/
  1116. static void
  1117. invite_cb (struct cell *ptrans, int ntype, struct tmcb_params *pcbp)
  1118. {
  1119. call_lst *pcall = (call_lst *)*pcbp->param;
  1120. if (ntype == TMCB_DESTROY)
  1121. { pcall->call_hash = pcall->call_label = 0; }
  1122. LM_ERR ("invite_cb: INVITE failed for call (%s)!", pcall->call_from);
  1123. delete_call (pcall);
  1124. return;
  1125. }
  1126. /**********
  1127. * Process NOTIFY Message
  1128. *
  1129. * INPUT:
  1130. * Arg (1) = SIP message pointer
  1131. * Arg (2) = call pointer
  1132. * OUTPUT: 0=failed
  1133. **********/
  1134. int notify_msg (sip_msg_t *pmsg, call_lst *pcall)
  1135. {
  1136. /**********
  1137. * waiting on REFER?
  1138. **********/
  1139. char *pfncname = "notify_msg: ";
  1140. if (pcall->call_state != CLSTA_RFRWAIT)
  1141. {
  1142. LM_ERR ("%sNot waiting on a REFER for call (%s)!", pfncname,
  1143. pcall->call_from);
  1144. if (pmod_data->psl->freply (pmsg, 481, presp_nocall) < 0)
  1145. { LM_ERR ("%sUnable to create reply!", pfncname); }
  1146. return 1;
  1147. }
  1148. /**********
  1149. * o sipfrag?
  1150. * o get status from body
  1151. * o add CRLF so parser can go beyond first line
  1152. **********/
  1153. if (!search_hdr_ext (pmsg->content_type, psipfrag))
  1154. {
  1155. LM_ERR ("%sNot a %s type for call (%s)!", pfncname,
  1156. psipfrag->s, pcall->call_from);
  1157. if (pmod_data->psl->freply (pmsg, 415, presp_unsupp) < 0)
  1158. { LM_ERR ("%sUnable to create reply!", pfncname); }
  1159. return 1;
  1160. }
  1161. char *pfrag = get_body (pmsg);
  1162. if (!pfrag)
  1163. {
  1164. LM_ERR ("%s%s body missing for call (%s)!", pfncname,
  1165. psipfrag->s, pcall->call_from);
  1166. if (pmod_data->psl->freply (pmsg, 415, presp_unsupp) < 0)
  1167. { LM_ERR ("%sUnable to create reply!", pfncname); }
  1168. return 1;
  1169. }
  1170. str pbody [1];
  1171. pbody->len = pmsg->len - (int)(pfrag - pmsg->buf);
  1172. pbody->s = pkg_malloc (pbody->len + 2);
  1173. if (!pbody->s)
  1174. {
  1175. LM_ERR ("%sNo more memory!", pfncname);
  1176. return 1;
  1177. }
  1178. strncpy (pbody->s, pfrag, pbody->len);
  1179. if (pbody->s [pbody->len - 1] != '\n')
  1180. {
  1181. strncpy (&pbody->s [pbody->len], SIPEOL, 2);
  1182. pbody->len += 2;
  1183. }
  1184. struct msg_start pstart [1];
  1185. parse_first_line (pbody->s, pbody->len + 1, pstart);
  1186. pkg_free (pbody->s);
  1187. if (pstart->type != SIP_REPLY)
  1188. {
  1189. LM_ERR ("%sReply missing for call (%s)!", pfncname, pcall->call_from);
  1190. if (pmod_data->psl->freply (pmsg, 415, presp_unsupp) < 0)
  1191. { LM_ERR ("%sUnable to create reply!", pfncname); }
  1192. return 1;
  1193. }
  1194. /**********
  1195. * o send OK
  1196. * o REFER done?
  1197. **********/
  1198. if (pmod_data->psl->freply (pmsg, 200, presp_ok) < 0)
  1199. {
  1200. LM_ERR ("%sUnable to create reply for call (%s)!",
  1201. pfncname, pcall->call_from);
  1202. return 1;
  1203. }
  1204. int nreply = pstart->u.reply.statuscode;
  1205. mohq_debug (pcall->pmohq, "%sNOTIFY received reply (%d) for call (%s)",
  1206. pfncname, nreply, pcall->call_from);
  1207. switch (nreply / 100)
  1208. {
  1209. case 1:
  1210. break;
  1211. case 2:
  1212. close_call (pmsg, pcall);
  1213. break;
  1214. default:
  1215. LM_WARN ("%sUnable to redirect call (%s)!", pfncname, pcall->call_from);
  1216. if (nreply == 487)
  1217. {
  1218. /**********
  1219. * call was canceled
  1220. **********/
  1221. drop_call (pmsg, pcall);
  1222. return 1;
  1223. }
  1224. /**********
  1225. * return call to queue
  1226. **********/
  1227. pcall->call_state = CLSTA_INQUEUE;
  1228. update_call_rec (pcall);
  1229. break;
  1230. }
  1231. return 1;
  1232. }
  1233. /**********
  1234. * Process PRACK Message
  1235. *
  1236. * INPUT:
  1237. * Arg (1) = SIP message pointer
  1238. * Arg (2) = call pointer
  1239. * OUTPUT: 0=failed
  1240. **********/
  1241. int prack_msg (sip_msg_t *pmsg, call_lst *pcall)
  1242. {
  1243. /**********
  1244. * waiting on PRACK?
  1245. **********/
  1246. char *pfncname = "prack_msg: ";
  1247. tm_api_t *ptm = pmod_data->ptm;
  1248. if (pcall->call_state != CLSTA_PRACKSTRT)
  1249. {
  1250. LM_ERR ("%sUnexpected PRACK (%s)!", pfncname, pcall->call_from);
  1251. if (pmod_data->psl->freply (pmsg, 481, presp_nocall) < 0)
  1252. { LM_ERR ("%sUnable to create reply!", pfncname); }
  1253. return 1;
  1254. }
  1255. /**********
  1256. * o check RAck ??? need to check
  1257. * o accept PRACK
  1258. **********/
  1259. if (ptm->t_newtran (pmsg) < 0)
  1260. {
  1261. LM_ERR ("%sUnable to create new transaction for call (%s)!",
  1262. pfncname, pcall->call_from);
  1263. if (pmod_data->psl->freply (pmsg, 500, presp_srverr) < 0)
  1264. { LM_ERR ("%sUnable to create reply!", pfncname); }
  1265. return 1;
  1266. }
  1267. if (ptm->t_reply (pmsg, 200, presp_ok->s) < 0)
  1268. {
  1269. LM_ERR ("%sUnable to reply to PRACK for call (%s)!",
  1270. pfncname, pcall->call_from);
  1271. return 1;
  1272. }
  1273. pcall->call_state = CLSTA_PRACKRPLY;
  1274. return 1;
  1275. }
  1276. /**********
  1277. * Refer Call
  1278. *
  1279. * INPUT:
  1280. * Arg (1) = call pointer
  1281. * Arg (2) = lock pointer
  1282. * OUTPUT: 0 if failed
  1283. **********/
  1284. int refer_call (call_lst *pcall, mohq_lock *plock)
  1285. {
  1286. /**********
  1287. * create dialog
  1288. **********/
  1289. char *pfncname = "refer_call: ";
  1290. int nret = 0;
  1291. struct to_body ptob [2];
  1292. dlg_t *pdlg = form_dialog (pcall, ptob);
  1293. if (!pdlg)
  1294. {
  1295. mohq_lock_release (plock);
  1296. return 0;
  1297. }
  1298. pdlg->state = DLG_CONFIRMED;
  1299. /**********
  1300. * form REFER message
  1301. * o calculate basic size
  1302. * o create buffer
  1303. **********/
  1304. str puri [1];
  1305. puri->s = pcall->call_referto;
  1306. puri->len = strlen (puri->s);
  1307. int npos1 = sizeof (prefermsg) // REFER template
  1308. + strlen (pcall->call_via) // Via
  1309. + puri->len // Refer-To
  1310. + ptob->uri.len; // Referred-By
  1311. char *pbuf = pkg_malloc (npos1);
  1312. if (!pbuf)
  1313. {
  1314. LM_ERR ("%sNo more memory!", pfncname);
  1315. goto refererr;
  1316. }
  1317. sprintf (pbuf, prefermsg,
  1318. pcall->call_via, // Via
  1319. puri->s, // Refer-To
  1320. STR_FMT (&ptob->uri)); // Referred-By
  1321. /**********
  1322. * send REFER request
  1323. **********/
  1324. tm_api_t *ptm = pmod_data->ptm;
  1325. uac_req_t puac [1];
  1326. str phdrs [1];
  1327. phdrs->s = pbuf;
  1328. phdrs->len = strlen (pbuf);
  1329. set_uac_req (puac, prefer, phdrs, 0, pdlg,
  1330. TMCB_LOCAL_COMPLETED | TMCB_ON_FAILURE, refer_cb, pcall);
  1331. pcall->call_state = CLSTA_REFER;
  1332. update_call_rec (pcall);
  1333. mohq_lock_release (plock);
  1334. if (ptm->t_request_within (puac) < 0)
  1335. {
  1336. pcall->call_state = CLSTA_INQUEUE;
  1337. LM_ERR ("%sUnable to create REFER request for call (%s)!",
  1338. pfncname, pcall->call_from);
  1339. update_call_rec (pcall);
  1340. goto refererr;
  1341. }
  1342. mohq_debug (pcall->pmohq, "%sSent REFER request for call (%s) to %s",
  1343. pfncname, pcall->call_from, pcall->call_referto);
  1344. nret = -1;
  1345. refererr:
  1346. if (pdlg)
  1347. { pkg_free (pdlg); }
  1348. pkg_free (pbuf);
  1349. return nret;
  1350. }
  1351. /**********
  1352. * REFER Callback
  1353. *
  1354. * INPUT:
  1355. * Arg (1) = cell pointer
  1356. * Arg (2) = callback type
  1357. * Arg (3) = callback parms
  1358. * OUTPUT: none
  1359. **********/
  1360. static void refer_cb
  1361. (struct cell *ptrans, int ntype, struct tmcb_params *pcbp)
  1362. {
  1363. char *pfncname = "refer_cb: ";
  1364. call_lst *pcall = (call_lst *)*pcbp->param;
  1365. if ((ntype == TMCB_ON_FAILURE) || (pcbp->req == FAKED_REPLY))
  1366. {
  1367. LM_ERR ("%sCall (%s) did not respond to REFER", pfncname,
  1368. pcall->call_from);
  1369. drop_call (pcbp->req, pcall);
  1370. return;
  1371. }
  1372. int nreply = pcbp->code;
  1373. if ((nreply / 100) == 2)
  1374. {
  1375. pcall->call_state = CLSTA_RFRWAIT;
  1376. mohq_debug (pcall->pmohq, "%sCall (%s) REFER reply=%d",
  1377. pfncname, pcall->call_from, nreply);
  1378. }
  1379. else
  1380. {
  1381. LM_ERR ("%sCall (%s) REFER error (%d)", pfncname,
  1382. pcall->call_from, nreply);
  1383. if (nreply == 481)
  1384. { delete_call (pcall); }
  1385. else
  1386. {
  1387. pcall->call_state = CLSTA_INQUEUE;
  1388. update_call_rec (pcall);
  1389. }
  1390. }
  1391. return;
  1392. }
  1393. /**********
  1394. * Process re-INVITE Message
  1395. *
  1396. * INPUT:
  1397. * Arg (1) = SIP message pointer
  1398. * Arg (2) = call pointer
  1399. * OUTPUT: 0=failed
  1400. **********/
  1401. int reinvite_msg (sip_msg_t *pmsg, call_lst *pcall)
  1402. {
  1403. /**********
  1404. * RFC 3261 section 14.2
  1405. * o dialog pending?
  1406. * o get SDP
  1407. **********/
  1408. char *pfncname = "reinvite_msg: ";
  1409. if ((pcall->call_state / 100) < 2)
  1410. {
  1411. mohq_debug (pcall->pmohq, "%sINVITE still pending for call (%s)",
  1412. pfncname, pcall->call_from);
  1413. if (pmod_data->psl->freply (pmsg, 491, presp_reqpend) < 0)
  1414. { LM_ERR ("%sUnable to create reply!", pfncname); }
  1415. return 1;
  1416. }
  1417. if (!(pmsg->msg_flags & FL_SDP_BODY))
  1418. {
  1419. if (parse_sdp (pmsg))
  1420. {
  1421. LM_ERR ("%sre-INVITE lacks SDP (%s)!", pfncname, pcall->call_from);
  1422. if (pmod_data->psl->freply (pmsg, 488, presp_noaccept) < 0)
  1423. { LM_ERR ("%sUnable to create reply!", pfncname); }
  1424. return 1;
  1425. }
  1426. }
  1427. /**********
  1428. * o find available MOH files
  1429. * o look for hold condition and matching payload type
  1430. **********/
  1431. rtpmap **pmohfiles = find_MOH (pcall->pmohq->mohq_mohdir,
  1432. pcall->pmohq->mohq_mohfile);
  1433. int bhold = 0;
  1434. int bmatch = 0;
  1435. int nsession;
  1436. sdp_session_cell_t *psession;
  1437. for (nsession = 0; (psession = get_sdp_session (pmsg, nsession)); nsession++)
  1438. {
  1439. int nstream;
  1440. sdp_stream_cell_t *pstream;
  1441. for (nstream = 0; (pstream = get_sdp_stream (pmsg, nsession, nstream));
  1442. nstream++)
  1443. {
  1444. /**********
  1445. * o RTP?
  1446. * o audio?
  1447. * o hold?
  1448. * o at least one payload matches?
  1449. **********/
  1450. if (!pstream->is_rtp)
  1451. { continue; }
  1452. if (!STR_EQ (*paudio, pstream->media))
  1453. { continue; }
  1454. if (pstream->is_on_hold)
  1455. {
  1456. bhold = 1;
  1457. break;
  1458. }
  1459. if (bmatch)
  1460. { continue; }
  1461. /**********
  1462. * check payload types for a match
  1463. **********/
  1464. sdp_payload_attr_t *ppayload;
  1465. for (ppayload = pstream->payload_attr; ppayload; ppayload = ppayload->next)
  1466. {
  1467. int ntype = atoi (ppayload->rtp_payload.s);
  1468. int nidx;
  1469. for (nidx = 0; pmohfiles [nidx]; nidx++)
  1470. {
  1471. if (pmohfiles [nidx]->ntype == ntype)
  1472. {
  1473. bmatch = 1;
  1474. break;
  1475. }
  1476. }
  1477. }
  1478. }
  1479. }
  1480. /**********
  1481. * if no hold, allow re-INVITE if matching file
  1482. **********/
  1483. if (!bhold)
  1484. {
  1485. if (!bmatch)
  1486. {
  1487. LM_ERR ("%sre-INVITE refused because no matching payload for call (%s)!",
  1488. pfncname, pcall->call_from);
  1489. if (pmod_data->psl->freply (pmsg, 488, presp_noaccept) < 0)
  1490. {
  1491. LM_ERR ("%sUnable to create reply!", pfncname);
  1492. return 1;
  1493. }
  1494. }
  1495. else
  1496. {
  1497. mohq_debug (pcall->pmohq, "%sAccepted re-INVITE for call (%s)",
  1498. pfncname, pcall->call_from);
  1499. if (pmod_data->psl->freply (pmsg, 200, presp_ok) < 0)
  1500. {
  1501. LM_ERR ("%sUnable to create reply!", pfncname);
  1502. return 1;
  1503. }
  1504. }
  1505. return 1;
  1506. }
  1507. /**********
  1508. * hold not allowed, say good-bye
  1509. **********/
  1510. LM_ERR ("%sTerminating call (%s) because hold not allowed!",
  1511. pfncname, pcall->call_from);
  1512. if (pmod_data->psl->freply (pmsg, 200, presp_ok) < 0)
  1513. {
  1514. LM_ERR ("%sUnable to create reply!", pfncname);
  1515. return 1;
  1516. }
  1517. close_call (pmsg, pcall);
  1518. return 1;
  1519. }
  1520. /**********
  1521. * Search Header for Extension
  1522. *
  1523. * INPUT:
  1524. * Arg (1) = header field pointer
  1525. * Arg (2) = extension str pointer
  1526. * OUTPUT: 0=not found
  1527. **********/
  1528. int search_hdr_ext (struct hdr_field *phdr, str *pext)
  1529. {
  1530. if (!phdr)
  1531. { return 0; }
  1532. str *pstr = &phdr->body;
  1533. int npos1, npos2;
  1534. for (npos1 = 0; npos1 < pstr->len; npos1++)
  1535. {
  1536. /**********
  1537. * o find non-space
  1538. * o search to end, space or comma
  1539. * o same size?
  1540. * o same name?
  1541. **********/
  1542. if (pstr->s [npos1] == ' ')
  1543. { continue; }
  1544. for (npos2 = npos1++; npos1 < pstr->len; npos1++)
  1545. {
  1546. if (pstr->s [npos1] == ' ' || pstr->s [npos1] == ',')
  1547. { break; }
  1548. }
  1549. if (npos1 - npos2 != pext->len)
  1550. { continue; }
  1551. if (!strncasecmp (&pstr->s [npos2], pext->s, pext->len))
  1552. { return 1; }
  1553. }
  1554. return 0;
  1555. }
  1556. /**********
  1557. * Send Provisional Response
  1558. *
  1559. * INPUT:
  1560. * Arg (1) = SIP message pointer
  1561. * Arg (2) = call pointer
  1562. * OUTPUT: 0=unable to process; 1=processed
  1563. **********/
  1564. int send_prov_rsp (sip_msg_t *pmsg, call_lst *pcall)
  1565. {
  1566. /**********
  1567. * o send ringing response with require
  1568. * o update record
  1569. **********/
  1570. char *pfncname = "send_prov_rsp: ";
  1571. tm_api_t *ptm = pmod_data->ptm;
  1572. pcall->call_cseq = rand ();
  1573. char phdrtmp [200];
  1574. char *phdrtmplt =
  1575. "Accept-Language: en" SIPEOL
  1576. "Require: 100rel" SIPEOL
  1577. "RSeq: %d" SIPEOL
  1578. "User-Agent: " USRAGNT SIPEOL
  1579. ;
  1580. sprintf (phdrtmp, phdrtmplt, pcall->call_cseq);
  1581. struct lump_rpl **phdrlump = add_lump_rpl2 (pmsg, phdrtmp,
  1582. strlen (phdrtmp), LUMP_RPL_HDR);
  1583. if (!phdrlump)
  1584. {
  1585. LM_ERR ("%sUnable to create new header for call (%s)!",
  1586. pfncname, pcall->call_from);
  1587. if (pmod_data->psl->freply (pmsg, 500, presp_srverr) < 0)
  1588. { LM_ERR ("%sUnable to create reply!", pfncname); }
  1589. return 0;
  1590. }
  1591. if (ptm->t_reply (pmsg, 180, presp_ring->s) < 0)
  1592. {
  1593. LM_ERR ("%sUnable to reply to INVITE for call (%s)",
  1594. pfncname, pcall->call_from);
  1595. return 0;
  1596. }
  1597. pcall->call_state = CLSTA_PRACKSTRT;
  1598. mohq_debug (pcall->pmohq, "%sSent PRACK RINGING for call (%s)",
  1599. pfncname, pcall->call_from);
  1600. /**********
  1601. * o wait until PRACK (64*T1 RFC 3261 section 7.1.1)
  1602. * o remove header lump
  1603. **********/
  1604. time_t nstart = time (0) + 32;
  1605. while (1)
  1606. {
  1607. usleep (USLEEP_LEN);
  1608. if (pcall->call_state != CLSTA_PRACKSTRT)
  1609. { break; }
  1610. if (nstart < time (0))
  1611. {
  1612. LM_ERR ("%sNo PRACK response for call (%s)",
  1613. pfncname, pcall->call_from);
  1614. break;
  1615. }
  1616. }
  1617. unlink_lump_rpl (pmsg, *phdrlump);
  1618. if (pcall->call_state != CLSTA_PRACKRPLY)
  1619. { return 0; }
  1620. return 1;
  1621. }
  1622. /**********
  1623. * Send RTPProxy Answer
  1624. *
  1625. * INPUT:
  1626. * Arg (1) = SIP message pointer
  1627. * Arg (2) = call pointer
  1628. * OUTPUT: 0=unable to process; 1=processed
  1629. **********/
  1630. int send_rtp_answer (sip_msg_t *pmsg, call_lst *pcall)
  1631. {
  1632. /**********
  1633. * build response from request
  1634. **********/
  1635. char *pfncname = "send_rtp_answer: ";
  1636. int nret = 0;
  1637. tm_api_t *ptm = pmod_data->ptm;
  1638. struct cell *ptrans = ptm->t_gett ();
  1639. str ptotag [1];
  1640. ptotag->s = pcall->call_tag;
  1641. ptotag->len = strlen (pcall->call_tag);
  1642. str pbuf [1];
  1643. struct bookmark pBM [1];
  1644. pbuf->s = build_res_buf_from_sip_req (200, presp_ok, ptotag, ptrans->uas.request,
  1645. (unsigned int *)&pbuf->len, pBM);
  1646. if (!pbuf->s || !pbuf->len)
  1647. {
  1648. LM_ERR ("%sUnable to create SDP response for call (%s)!",
  1649. pfncname, pcall->call_from);
  1650. return 0;
  1651. }
  1652. /**********
  1653. * parse out first line and headers
  1654. **********/
  1655. char *pclenhdr = CLENHDR;
  1656. str pparse [20];
  1657. int npos1, npos2;
  1658. int nhdrcnt = 0;
  1659. for (npos1 = 0; npos1 < pbuf->len; npos1++)
  1660. {
  1661. /**********
  1662. * find EOL
  1663. **********/
  1664. for (npos2 = npos1++; npos1 < pbuf->len; npos1++)
  1665. {
  1666. /**********
  1667. * o not EOL? (CRLF assumed)
  1668. * o next line a continuation? (RFC 3261 section 7.3.1)
  1669. **********/
  1670. if (pbuf->s [npos1] != '\n')
  1671. { continue; }
  1672. if (npos1 + 1 == pbuf->len)
  1673. { break; }
  1674. if (pbuf->s [npos1 + 1] == ' '
  1675. || pbuf->s [npos1 + 1] == '\t')
  1676. { continue; }
  1677. break;
  1678. }
  1679. /**********
  1680. * o blank is end of header (RFC 3261 section 7)
  1681. * o ignore Content-Length (assume followed by colon)
  1682. * o save header
  1683. **********/
  1684. if (npos1 - npos2 == 1)
  1685. { break; }
  1686. if (npos1 - npos2 > 14)
  1687. {
  1688. if (!strncasecmp (&pbuf->s [npos2], pclenhdr, 14))
  1689. { continue; }
  1690. }
  1691. pparse [nhdrcnt].s = &pbuf->s [npos2];
  1692. pparse [nhdrcnt++].len = npos1 - npos2 + 1;
  1693. }
  1694. /**********
  1695. * recreate buffer with extra headers and SDP
  1696. * o form SDP
  1697. * o count hdrs, extra hdrs, content-length hdr, SDP
  1698. * o alloc new buffer
  1699. * o form new buffer
  1700. * o replace orig buffer
  1701. **********/
  1702. str pSDP [1] = {STR_NULL};
  1703. if (!form_rtp_SDP (pSDP, pcall, prtpsdp))
  1704. { goto answer_done; }
  1705. for (npos1 = npos2 = 0; npos2 < nhdrcnt; npos2++)
  1706. { npos1 += pparse [npos2].len; }
  1707. char pbodylen [30];
  1708. sprintf (pbodylen, "%s: %d\r\n\r\n", pclenhdr, pSDP->len);
  1709. npos1 += pextrahdr->len + strlen (pbodylen) + pSDP->len + 1;
  1710. char *pnewbuf = pkg_malloc (npos1);
  1711. if (!pnewbuf)
  1712. {
  1713. LM_ERR ("%sNo more memory!", pfncname);
  1714. goto answer_done;
  1715. }
  1716. for (npos1 = npos2 = 0; npos2 < nhdrcnt; npos2++)
  1717. {
  1718. memcpy (&pnewbuf [npos1], pparse [npos2].s, pparse [npos2].len);
  1719. npos1 += pparse [npos2].len;
  1720. }
  1721. npos2 = pextrahdr->len;
  1722. memcpy (&pnewbuf [npos1], pextrahdr->s, npos2);
  1723. npos1 += npos2;
  1724. npos2 = strlen (pbodylen);
  1725. memcpy (&pnewbuf [npos1], pbodylen, npos2);
  1726. npos1 += npos2;
  1727. npos2 = pSDP->len;
  1728. memcpy (&pnewbuf [npos1], pSDP->s, npos2);
  1729. npos1 += npos2;
  1730. pkg_free (pbuf->s);
  1731. pbuf->s = pnewbuf;
  1732. pbuf->len = npos1;
  1733. /**********
  1734. * build SIP msg
  1735. **********/
  1736. struct sip_msg pnmsg [1];
  1737. build_sip_msg_from_buf (pnmsg, pbuf->s, pbuf->len, 0);
  1738. memcpy (&pnmsg->rcv, &pmsg->rcv, sizeof (struct receive_info));
  1739. /**********
  1740. * o send rtpproxy answer
  1741. * o form stream file
  1742. * o send stream
  1743. **********/
  1744. mohq_debug (pcall->pmohq, "%sAnswering RTP link for call (%s)",
  1745. pfncname, pcall->call_from);
  1746. if (pmod_data->fn_rtp_answer (pnmsg, 0, 0) != 1)
  1747. {
  1748. LM_ERR ("%srtpproxy_answer refused for call (%s)!",
  1749. pfncname, pcall->call_from);
  1750. goto answer_done;
  1751. }
  1752. if (!start_stream (pnmsg, pcall, 0))
  1753. { goto answer_done; }
  1754. /**********
  1755. * o create buffer from response
  1756. * o find SDP
  1757. **********/
  1758. pbuf->s = build_res_buf_from_sip_res (pnmsg, (unsigned int *)&pbuf->len);
  1759. pkg_free (pnewbuf);
  1760. free_sip_msg (pnmsg);
  1761. if (!pbuf->s || !pbuf->len)
  1762. {
  1763. LM_ERR ("%sUnable to create SDP response for call (%s)!",
  1764. pfncname, pcall->call_from);
  1765. goto answer_done;
  1766. }
  1767. str pnewSDP [1];
  1768. for (npos1 = 0; npos1 < pbuf->len; npos1++)
  1769. {
  1770. if (pbuf->s [npos1] != '\n')
  1771. { continue; }
  1772. if (pbuf->s [npos1 - 3] == '\r')
  1773. { break; }
  1774. }
  1775. pnewSDP->s = &pbuf->s [npos1 + 1];
  1776. pnewSDP->len = pbuf->len - npos1 - 1;
  1777. /**********
  1778. * o save media port number
  1779. * o send adjusted reply
  1780. **********/
  1781. char *pfnd = strstr (pnewSDP->s, "m=audio ");
  1782. if (!pfnd)
  1783. {
  1784. // should not happen
  1785. LM_ERR ("%sUnable to find audio port for call (%s)!",
  1786. pfncname, pcall->call_from);
  1787. goto answer_done;
  1788. }
  1789. pcall->call_aport = strtol (pfnd + 8, NULL, 10);
  1790. if (!add_lump_rpl2 (pmsg, pextrahdr->s, pextrahdr->len, LUMP_RPL_HDR))
  1791. {
  1792. LM_ERR ("%sUnable to add header for call (%s)!",
  1793. pfncname, pcall->call_from);
  1794. goto answer_done;
  1795. }
  1796. if (!add_lump_rpl2 (pmsg, pnewSDP->s, pnewSDP->len, LUMP_RPL_BODY))
  1797. {
  1798. LM_ERR ("%sUnable to add SDP body for call (%s)!",
  1799. pfncname, pcall->call_from);
  1800. goto answer_done;
  1801. }
  1802. if (ptm->t_reply (pmsg, 200, presp_ok->s) < 0)
  1803. {
  1804. LM_ERR ("%sUnable to reply to INVITE for call (%s)!",
  1805. pfncname, pcall->call_from);
  1806. goto answer_done;
  1807. }
  1808. pcall->call_state = CLSTA_INVITED;
  1809. mohq_debug (pcall->pmohq, "%sResponded to INVITE with RTP for call (%s)",
  1810. pfncname, pcall->call_from);
  1811. nret = 1;
  1812. /**********
  1813. * free buffer and return
  1814. **********/
  1815. answer_done:
  1816. if (pSDP->s)
  1817. { pkg_free (pSDP->s); }
  1818. pkg_free (pbuf->s);
  1819. return nret;
  1820. }
  1821. /**********
  1822. * Start Streaming
  1823. *
  1824. * INPUT:
  1825. * Arg (1) = SIP message pointer
  1826. * Arg (2) = call pointer
  1827. * Arg (3) = server flag
  1828. * OUTPUT: 0 if failed
  1829. **********/
  1830. int start_stream (sip_msg_t *pmsg, call_lst *pcall, int bserver)
  1831. {
  1832. char *pfncname = "start_stream: ";
  1833. char pfile [MOHDIRLEN + MOHFILELEN + 2];
  1834. strcpy (pfile, pcall->pmohq->mohq_mohdir);
  1835. int npos = strlen (pfile);
  1836. pfile [npos++] = '/';
  1837. strcpy (&pfile [npos], pcall->pmohq->mohq_mohfile);
  1838. npos += strlen (&pfile [npos]);
  1839. str pMOH [1] = {{pfile, npos}};
  1840. pv_elem_t *pmodel;
  1841. pv_parse_format (pMOH, &pmodel);
  1842. cmd_function fn_stream = bserver ? pmod_data->fn_rtp_stream_s
  1843. : pmod_data->fn_rtp_stream_c;
  1844. mohq_debug (pcall->pmohq, "%sStarting RTP link for call (%s)",
  1845. pfncname, pcall->call_from);
  1846. if (fn_stream (pmsg, (char *)pmodel, (char *)-1) != 1)
  1847. {
  1848. LM_ERR ("%srtpproxy_stream refused for call (%s)!",
  1849. pfncname, pcall->call_from);
  1850. return 0;
  1851. }
  1852. return 1;
  1853. }
  1854. /**********
  1855. * Form Char Array from STR
  1856. *
  1857. * INPUT:
  1858. * Arg (1) = str pointer
  1859. * OUTPUT: char pointer; NULL if unable to allocate
  1860. **********/
  1861. char *form_tmpstr (str *pstr)
  1862. {
  1863. char *pcstr = malloc (pstr->len + 1);
  1864. if (!pcstr)
  1865. {
  1866. LM_ERR ("No more memory!");
  1867. return NULL;
  1868. }
  1869. memcpy (pcstr, pstr->s, pstr->len);
  1870. pcstr [pstr->len] = 0;
  1871. return pcstr;
  1872. }
  1873. /**********
  1874. * Release Char Array
  1875. *
  1876. * INPUT:
  1877. * Arg (1) = char pointer
  1878. * OUTPUT: none
  1879. **********/
  1880. void free_tmpstr (char *pcstr)
  1881. {
  1882. if (pcstr)
  1883. { free (pcstr); }
  1884. return;
  1885. }
  1886. /**********
  1887. * external functions
  1888. **********/
  1889. /**********
  1890. * Find MOH Files
  1891. *
  1892. * INPUT:
  1893. * Arg (1) = mohdir pointer
  1894. * Arg (2) = mohfile pointer
  1895. * OUTPUT: array of pointers for matching files; last element=0
  1896. **********/
  1897. rtpmap **find_MOH (char *pmohdir, char *pmohfile)
  1898. {
  1899. /**********
  1900. * form base file name
  1901. **********/
  1902. char pfile [MOHDIRLEN + MOHFILELEN + 6];
  1903. strcpy (pfile, pmohdir);
  1904. int nflen = strlen (pfile);
  1905. pfile [nflen++] = '/';
  1906. strcpy (&pfile [nflen], pmohfile);
  1907. nflen += strlen (&pfile [nflen]);
  1908. pfile [nflen++] = '.';
  1909. /**********
  1910. * find available files based on RTP payload type
  1911. **********/
  1912. int nidx;
  1913. int nfound = 0;
  1914. for (nidx = 0; prtpmap [nidx].pencode; nidx++)
  1915. {
  1916. /**********
  1917. * o form file name based on payload type
  1918. * o exists?
  1919. **********/
  1920. sprintf (&pfile [nflen], "%d", prtpmap [nidx].ntype);
  1921. struct stat psb [1];
  1922. if (lstat (pfile, psb))
  1923. { continue; }
  1924. pmohfiles [nfound++] = &prtpmap [nidx];
  1925. }
  1926. pmohfiles [nfound] = 0;
  1927. return pmohfiles;
  1928. }
  1929. /**********
  1930. * MI Debug
  1931. *
  1932. * PARAMETERS:
  1933. * queue name = queue to use
  1934. * state = 0=off, <>0=on
  1935. *
  1936. * INPUT:
  1937. * Arg (1) = command tree pointer
  1938. * Arg (2) = parms pointer
  1939. * OUTPUT: root pointer
  1940. **********/
  1941. struct mi_root *mi_debug (struct mi_root *pcmd_tree, void *parms)
  1942. {
  1943. /**********
  1944. * o parm count correct?
  1945. * o find queue
  1946. * o lock queue
  1947. **********/
  1948. struct mi_node *pnode = pcmd_tree->node.kids;
  1949. if (!pnode || !pnode->next || pnode->next->next)
  1950. { return init_mi_tree (400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN); }
  1951. int nq_idx = find_queue (&pnode->value);
  1952. if (nq_idx == -1)
  1953. { return init_mi_tree (400, pmi_noqueue->s, pmi_noqueue->len); }
  1954. char pint [20];
  1955. int nsize = (pnode->next->value.len >= sizeof (pint))
  1956. ? sizeof (pint) - 1 : pnode->next->value.len;
  1957. strncpy (pint, pnode->next->value.s, nsize);
  1958. pint [nsize] = '\0';
  1959. int bdebug = atoi (pint) ? 1 : 0;
  1960. if (!mohq_lock_set (pmod_data->pmohq_lock, 0, 5000))
  1961. { return init_mi_tree (400, pmi_nolock->s, pmi_nolock->len); }
  1962. /**********
  1963. * o set flag
  1964. * o update queue table
  1965. * o release lock
  1966. **********/
  1967. mohq_lst *pqueue = &pmod_data->pmohq_lst [nq_idx];
  1968. if (bdebug)
  1969. { pqueue->mohq_flags |= MOHQF_DBG; }
  1970. else
  1971. { pqueue->mohq_flags &= ~MOHQF_DBG; }
  1972. update_debug (pqueue, bdebug);
  1973. mohq_lock_release (pmod_data->pmohq_lock);
  1974. return init_mi_tree (200, MI_OK_S, MI_OK_LEN);
  1975. }
  1976. /**********
  1977. * MI Drop Call
  1978. *
  1979. * PARAMETERS:
  1980. * queue name = queue to use
  1981. * callID = *=all, otherwise callID
  1982. *
  1983. * INPUT:
  1984. * Arg (1) = command tree pointer
  1985. * Arg (2) = parms pointer
  1986. * OUTPUT: root pointer
  1987. **********/
  1988. struct mi_root *mi_drop_call (struct mi_root *pcmd_tree, void *parms)
  1989. {
  1990. /**********
  1991. * o parm count correct?
  1992. * o find queue
  1993. * o lock calls
  1994. **********/
  1995. struct mi_node *pnode = pcmd_tree->node.kids;
  1996. if (!pnode || !pnode->next || pnode->next->next)
  1997. { return init_mi_tree (400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN); }
  1998. int nq_idx = find_queue (&pnode->value);
  1999. if (nq_idx == -1)
  2000. { return init_mi_tree (400, pmi_noqueue->s, pmi_noqueue->len); }
  2001. if (!mohq_lock_set (pmod_data->pcall_lock, 0, 5000))
  2002. { return init_mi_tree (400, pmi_nolock->s, pmi_nolock->len); }
  2003. /**********
  2004. * o find matching calls
  2005. * o release lock
  2006. **********/
  2007. mohq_lst *pqueue = &pmod_data->pmohq_lst [nq_idx];
  2008. int nidx;
  2009. str *pcallid = &pnode->next->value;
  2010. for (nidx = 0; nidx < pmod_data->call_cnt; nidx++)
  2011. {
  2012. /**********
  2013. * o call active?
  2014. * o callID matches?
  2015. * o close call
  2016. **********/
  2017. call_lst *pcall = &pmod_data->pcall_lst [nidx];
  2018. if (!pcall->call_active)
  2019. { continue; }
  2020. if (pqueue->mohq_id != pcall->pmohq->mohq_id)
  2021. { continue; }
  2022. str tmpstr [1];
  2023. if (!STR_EQ (*pcallid, *pallq))
  2024. {
  2025. tmpstr->s = pcall->call_id;
  2026. tmpstr->len = strlen (tmpstr->s);
  2027. if (!STR_EQ (*tmpstr, *pcallid))
  2028. { continue; }
  2029. }
  2030. close_call (FAKED_REPLY, pcall);
  2031. }
  2032. mohq_lock_release (pmod_data->pcall_lock);
  2033. return init_mi_tree (200, MI_OK_S, MI_OK_LEN);
  2034. }
  2035. /**********
  2036. * Count Messages
  2037. *
  2038. * INPUT:
  2039. * Arg (1) = SIP message pointer
  2040. * Arg (2) = queue name
  2041. * Arg (3) = pv result name
  2042. * OUTPUT: -1 if no items in queue; else result = count
  2043. **********/
  2044. int mohq_count (sip_msg_t *pmsg, char *pqueue, pv_spec_t *presult)
  2045. {
  2046. /**********
  2047. * get queue and pv names
  2048. **********/
  2049. char *pfncname = "mohq_count: ";
  2050. str pqname [1];
  2051. if (!pqueue || !presult)
  2052. {
  2053. LM_ERR ("%sParameters missing!", pfncname);
  2054. return -1;
  2055. }
  2056. if (fixup_get_svalue (pmsg, (gparam_p)pqueue, pqname))
  2057. {
  2058. LM_ERR ("%sInvalid queue name!", pfncname);
  2059. return -1;
  2060. }
  2061. /**********
  2062. * o find queue
  2063. * o lock calls
  2064. * o count items in queue
  2065. **********/
  2066. int nq_idx = find_queue (pqname);
  2067. int ncount = 0;
  2068. call_lst *pcalls = pmod_data->pcall_lst;
  2069. int ncall_idx, mohq_id;
  2070. if (!mohq_lock_set (pmod_data->pcall_lock, 0, 200))
  2071. { LM_ERR ("%sUnable to lock calls!", pfncname); }
  2072. else
  2073. {
  2074. if (nq_idx != -1)
  2075. {
  2076. mohq_id = pmod_data->pmohq_lst [nq_idx].mohq_id;
  2077. for (ncall_idx = 0; ncall_idx < pmod_data->call_cnt; ncall_idx++)
  2078. {
  2079. if (!pcalls [ncall_idx].call_active)
  2080. { continue; }
  2081. if (pcalls [ncall_idx].pmohq->mohq_id == mohq_id
  2082. && pcalls [ncall_idx].call_state == CLSTA_INQUEUE)
  2083. { ncount++; }
  2084. }
  2085. }
  2086. mohq_lock_release (pmod_data->pcall_lock);
  2087. }
  2088. /**********
  2089. * o set pv result
  2090. * o exit with result
  2091. **********/
  2092. pv_value_t pavp_val [1];
  2093. memset (pavp_val, 0, sizeof (pv_value_t));
  2094. pavp_val->ri = ncount;
  2095. pavp_val->flags = PV_TYPE_INT | PV_VAL_INT;
  2096. if (presult->setf (pmsg, &presult->pvp, (int)EQ_T, pavp_val) < 0)
  2097. {
  2098. LM_ERR ("%sUnable to set pv value for mohq_count ()!", pfncname);
  2099. return -1;
  2100. }
  2101. return 1;
  2102. }
  2103. /**********
  2104. * Log Debug Statement
  2105. *
  2106. * INPUT:
  2107. * Arg (1) = MOH queue pointer
  2108. * Arg (2) = format pointer
  2109. * Arg (...) = optional format values
  2110. * OUTPUT: outputs debugging values
  2111. **********/
  2112. void mohq_debug (mohq_lst *pmohq, char *pfmt, ...)
  2113. {
  2114. /**********
  2115. * o get system and MOHQ log level
  2116. * o exit if no debug printing
  2117. * o force local debug
  2118. * o form message and log
  2119. * o reset log level
  2120. **********/
  2121. int nsys_log = get_debug_level (LOG_MNAME, LOG_MNAME_LEN);
  2122. int nmohq_log = (pmohq->mohq_flags & MOHQF_DBG) ? L_DBG : L_INFO;
  2123. if (nmohq_log < L_DBG && nsys_log < L_DBG)
  2124. { return; }
  2125. if (nsys_log < nmohq_log)
  2126. { set_local_debug_level (nmohq_log); }
  2127. char ptext [1024];
  2128. va_list ap;
  2129. va_start (ap, pfmt);
  2130. vsnprintf (ptext, sizeof (ptext), pfmt, ap);
  2131. va_end (ap);
  2132. LM_DBG ("%s", ptext);
  2133. if (nsys_log < nmohq_log)
  2134. { reset_local_debug_level (); }
  2135. return;
  2136. }
  2137. /**********
  2138. * Process Message
  2139. *
  2140. * INPUT:
  2141. * Arg (1) = SIP message pointer
  2142. * OUTPUT: -1=not directed to queue; 1=successfully processed
  2143. **********/
  2144. int mohq_process (sip_msg_t *pmsg)
  2145. {
  2146. /**********
  2147. * o parse headers
  2148. * o lock MOH queue
  2149. * o directed to message queue?
  2150. * o connect to database
  2151. **********/
  2152. char *pfncname = "mohq_process: ";
  2153. if (parse_headers (pmsg, HDR_EOH_F, 0) < 0)
  2154. {
  2155. LM_ERR ("%sUnable to parse header!", pfncname);
  2156. return -1;
  2157. }
  2158. if (!mohq_lock_set (pmod_data->pmohq_lock, 0, 2000))
  2159. {
  2160. LM_ERR ("%sUnable to lock calls!", pfncname);
  2161. return -1;
  2162. }
  2163. call_lst *pcall;
  2164. int mohq_idx = find_call (pmsg, &pcall);
  2165. db1_con_t *pconn = mohq_dbconnect ();
  2166. if (pconn)
  2167. {
  2168. /**********
  2169. * o last update older than 1 minute?
  2170. * o exclusively lock MOH queue
  2171. * o update queue
  2172. **********/
  2173. if (pmod_data->mohq_update + 60 < time (0))
  2174. {
  2175. if (mohq_lock_change (pmod_data->pmohq_lock, 1))
  2176. {
  2177. update_mohq_lst (pconn);
  2178. mohq_lock_change (pmod_data->pmohq_lock, 0);
  2179. pmod_data->mohq_update = time (0);
  2180. }
  2181. }
  2182. mohq_dbdisconnect (pconn);
  2183. }
  2184. if (mohq_idx < 0)
  2185. {
  2186. mohq_lock_release (pmod_data->pmohq_lock);
  2187. return -1;
  2188. }
  2189. /**********
  2190. * o process message
  2191. * o release MOH queue
  2192. **********/
  2193. mohq_debug (&pmod_data->pmohq_lst [mohq_idx],
  2194. "%sProcessing %.*s, queue (%s)", pfncname,
  2195. STR_FMT (&REQ_LINE (pmsg).method),
  2196. pmod_data->pmohq_lst [mohq_idx].mohq_name);
  2197. int ret;
  2198. switch (pmsg->REQ_METHOD)
  2199. {
  2200. case METHOD_INVITE:
  2201. /**********
  2202. * initial INVITE?
  2203. **********/
  2204. if (!pcall)
  2205. { ret = first_invite_msg (pmsg, mohq_idx); }
  2206. else
  2207. { ret = reinvite_msg (pmsg, pcall); }
  2208. break;
  2209. case METHOD_NOTIFY:
  2210. ret = notify_msg (pmsg, pcall);
  2211. break;
  2212. case METHOD_PRACK:
  2213. ret = prack_msg (pmsg, pcall);
  2214. break;
  2215. case METHOD_ACK:
  2216. ret = ack_msg (pmsg, pcall);
  2217. break;
  2218. case METHOD_BYE:
  2219. ret = bye_msg (pmsg, pcall);
  2220. break;
  2221. case METHOD_CANCEL:
  2222. ret = cancel_msg (pmsg, pcall);
  2223. break;
  2224. default:
  2225. deny_method (pmsg, pcall);
  2226. ret = 1;
  2227. break;
  2228. }
  2229. mohq_lock_release (pmod_data->pmohq_lock);
  2230. return ret ? 1 : -1;
  2231. }
  2232. /**********
  2233. * Retrieve Oldest Queued Call
  2234. *
  2235. * INPUT:
  2236. * Arg (1) = SIP message pointer
  2237. * Arg (2) = queue name
  2238. * Arg (3) = redirect URI
  2239. * OUTPUT: -1 if no items in queue or error; 1 redirects oldest call
  2240. **********/
  2241. int mohq_retrieve (sip_msg_t *pmsg, char *pqueue, char *pURI)
  2242. {
  2243. /**********
  2244. * o get queue name and URI
  2245. * o check URI
  2246. **********/
  2247. char *pfncname = "mohq_retrieve: ";
  2248. str puri [1], pqname [1];
  2249. if (!pqueue || !pURI)
  2250. {
  2251. LM_ERR ("%sParameters missing!", pfncname);
  2252. return -1;
  2253. }
  2254. if (fixup_get_svalue (pmsg, (gparam_p)pqueue, pqname))
  2255. {
  2256. LM_ERR ("%sInvalid queue name!", pfncname);
  2257. return -1;
  2258. }
  2259. if (fixup_get_svalue (pmsg, (gparam_p)pURI, puri))
  2260. {
  2261. LM_ERR ("%sInvalid URI!", pfncname);
  2262. return -1;
  2263. }
  2264. if (puri->len > URI_LEN)
  2265. {
  2266. LM_ERR ("%sURI too long!", pfncname);
  2267. return -1;
  2268. }
  2269. struct sip_uri puri_parsed [1];
  2270. if (parse_uri (puri->s, puri->len, puri_parsed))
  2271. {
  2272. LM_ERR ("%sInvalid URI (%.*s)!", pfncname, STR_FMT (puri));
  2273. return -1;
  2274. }
  2275. /**********
  2276. * o find queue
  2277. * o lock calls
  2278. * o find oldest call
  2279. **********/
  2280. int nq_idx = find_queue (pqname);
  2281. if (nq_idx == -1)
  2282. { return -1; }
  2283. if (!mohq_lock_set (pmod_data->pcall_lock, 0, 200))
  2284. {
  2285. LM_ERR ("%sUnable to lock calls!", pfncname);
  2286. return -1;
  2287. }
  2288. call_lst *pcall = 0;
  2289. int ncall_idx;
  2290. time_t ntime = 0;
  2291. int nfound = -1;
  2292. int mohq_id = pmod_data->pmohq_lst [nq_idx].mohq_id;
  2293. for (ncall_idx = 0; ncall_idx < pmod_data->call_cnt; ncall_idx++)
  2294. {
  2295. /**********
  2296. * o active call?
  2297. * o matching queue?
  2298. * o in queue?
  2299. * o check age
  2300. **********/
  2301. pcall = &pmod_data->pcall_lst [ncall_idx];
  2302. if (!pcall->call_active)
  2303. { continue; }
  2304. if (pcall->pmohq->mohq_id != mohq_id)
  2305. { continue; }
  2306. if (pcall->call_state != CLSTA_INQUEUE)
  2307. { continue; }
  2308. if (!ntime)
  2309. {
  2310. nfound = ncall_idx;
  2311. ntime = pcall->call_time;
  2312. }
  2313. else
  2314. {
  2315. if (pcall->call_time < ntime)
  2316. {
  2317. nfound = ncall_idx;
  2318. ntime = pcall->call_time;
  2319. }
  2320. }
  2321. }
  2322. if (nfound == -1)
  2323. {
  2324. LM_WARN ("%sNo calls in queue (%.*s)", pfncname, STR_FMT (pqname));
  2325. mohq_lock_release (pmod_data->pcall_lock);
  2326. return -1;
  2327. }
  2328. pcall = &pmod_data->pcall_lst [nfound];
  2329. /**********
  2330. * o save refer-to URI
  2331. * o send refer
  2332. **********/
  2333. strncpy (pcall->call_referto, puri->s, puri->len);
  2334. pcall->call_referto [puri->len] = '\0';
  2335. if (refer_call (pcall, pmod_data->pcall_lock))
  2336. { return 1; }
  2337. LM_ERR ("%sUnable to refer call (%s)!", pfncname, pcall->call_from);
  2338. return -1;
  2339. }
  2340. /**********
  2341. * Send Message to Queue
  2342. *
  2343. * INPUT:
  2344. * Arg (1) = SIP message pointer
  2345. * Arg (2) = queue name
  2346. * OUTPUT: -1 if no items in queue; 1 if successfull
  2347. **********/
  2348. int mohq_send (sip_msg_t *pmsg, char *pqueue)
  2349. {
  2350. /**********
  2351. * o first INVITE?
  2352. * o get queue name
  2353. **********/
  2354. char *pfncname = "mohq_send: ";
  2355. if (pmsg->REQ_METHOD != METHOD_INVITE)
  2356. {
  2357. LM_ERR ("%sNot an INVITE message!", pfncname);
  2358. return -1;
  2359. }
  2360. to_body_t *pto_body = get_to (pmsg);
  2361. if (pto_body->tag_value.len)
  2362. {
  2363. LM_ERR ("%sNot a first INVITE message!", pfncname);
  2364. return -1;
  2365. }
  2366. str pqname [1];
  2367. if (!pqueue)
  2368. {
  2369. LM_ERR ("%sParameters missing!", pfncname);
  2370. return -1;
  2371. }
  2372. if (fixup_get_svalue (pmsg, (gparam_p)pqueue, pqname))
  2373. {
  2374. LM_ERR ("%sInvalid queue name!", pfncname);
  2375. return -1;
  2376. }
  2377. /**********
  2378. * o find queue
  2379. * o change RURI
  2380. * o relay message
  2381. **********/
  2382. int nq_idx = find_queue (pqname);
  2383. if (nq_idx == -1)
  2384. { return -1; }
  2385. str pruri [1] = {{0, strlen (pmod_data->pmohq_lst [nq_idx].mohq_uri)}};
  2386. pruri->s = pkg_malloc (pruri->len + 1);
  2387. if (!pruri->s)
  2388. {
  2389. LM_ERR ("%sNo more memory!", pfncname);
  2390. return -1;
  2391. }
  2392. strcpy (pruri->s, pmod_data->pmohq_lst [nq_idx].mohq_uri);
  2393. if (pmsg->new_uri.s)
  2394. { pkg_free (pmsg->new_uri.s); }
  2395. pmsg->new_uri.s = pruri->s;
  2396. pmsg->new_uri.len = pruri->len;
  2397. pmsg->parsed_uri_ok = 0;
  2398. pmsg->parsed_orig_ruri_ok = 0;
  2399. if (pmod_data->ptm->t_relay (pmsg, 0, 0) < 0)
  2400. {
  2401. LM_ERR ("%sUnable to relay INVITE!", pfncname);
  2402. return -1;
  2403. }
  2404. return 1;
  2405. }