2
0

rtpproxy.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923
  1. /* $Id$
  2. *
  3. * Copyright (C) 2003-2008 Sippy Software, Inc., http://www.sippysoft.com
  4. *
  5. * This file is part of Kamailio, a free SIP server.
  6. *
  7. * Kamailio is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version
  11. *
  12. * Kamailio is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * History:
  22. * ---------
  23. * 2003-10-09 nat_uac_test introduced (jiri)
  24. *
  25. * 2003-11-06 nat_uac_test permitted from onreply_route (jiri)
  26. *
  27. * 2003-12-01 unforce_rtp_proxy introduced (sobomax)
  28. *
  29. * 2004-01-07 RTP proxy support updated to support new version of the
  30. * RTP proxy (20040107).
  31. *
  32. * force_rtp_proxy() now inserts a special flag
  33. * into the SDP body to indicate that this session already
  34. * proxied and ignores sessions with such flag.
  35. *
  36. * Added run-time check for version of command protocol
  37. * supported by the RTP proxy.
  38. *
  39. * 2004-01-16 Integrated slightly modified patch from Tristan Colgate,
  40. * force_rtp_proxy function with IP as a parameter (janakj)
  41. *
  42. * 2004-01-28 nat_uac_test extended to allow testing SDP body (sobomax)
  43. *
  44. * nat_uac_test extended to allow testing top Via (sobomax)
  45. *
  46. * 2004-02-21 force_rtp_proxy now accepts option argument, which
  47. * consists of string of chars, each of them turns "on"
  48. * some feature, currently supported ones are:
  49. *
  50. * `a' - flags that UA from which message is received
  51. * doesn't support symmetric RTP;
  52. * `l' - force "lookup", that is, only rewrite SDP when
  53. * corresponding session is already exists in the
  54. * RTP proxy. Only makes sense for SIP requests,
  55. * replies are always processed in "lookup" mode;
  56. * `i' - flags that message is received from UA in the
  57. * LAN. Only makes sense when RTP proxy is running
  58. * in the bridge mode.
  59. *
  60. * force_rtp_proxy can now be invoked without any arguments,
  61. * as previously, with one argument - in this case argument
  62. * is treated as option string and with two arguments, in
  63. * which case 1st argument is option string and the 2nd
  64. * one is IP address which have to be inserted into
  65. * SDP (IP address on which RTP proxy listens).
  66. *
  67. * 2004-03-12 Added support for IPv6 addresses in SDPs. Particularly,
  68. * force_rtp_proxy now can work with IPv6-aware RTP proxy,
  69. * replacing IPv4 address in SDP with IPv6 one and vice versa.
  70. * This allows creating full-fledged IPv4<->IPv6 gateway.
  71. * See 4to6.cfg file for example.
  72. *
  73. * Two new options added into force_rtp_proxy:
  74. *
  75. * `f' - instructs nathelper to ignore marks inserted
  76. * by another nathelper in transit to indicate
  77. * that the session is already goes through another
  78. * proxy. Allows creating chain of proxies.
  79. * `r' - flags that IP address in SDP should be trusted.
  80. * Without this flag, nathelper ignores address in the
  81. * SDP and uses source address of the SIP message
  82. * as media address which is passed to the RTP proxy.
  83. *
  84. * Protocol between nathelper and RTP proxy in bridge
  85. * mode has been slightly changed. Now RTP proxy expects SER
  86. * to provide 2 flags when creating or updating session
  87. * to indicate direction of this session. Each of those
  88. * flags can be either `e' or `i'. For example `ei' means
  89. * that we received INVITE from UA on the "external" network
  90. * network and will send it to the UA on "internal" one.
  91. * Also possible `ie' (internal->external), `ii'
  92. * (internal->internal) and `ee' (external->external). See
  93. * example file alg.cfg for details.
  94. *
  95. * 2004-03-15 If the rtp proxy test failed (wrong version or not started)
  96. * retry test from time to time, when some *rtpproxy* function
  97. * is invoked. Minimum interval between retries can be
  98. * configured via rtpproxy_disable_tout module parameter (default
  99. * is 60 seconds). Setting it to -1 will disable periodic
  100. * rechecks completely, setting it to 0 will force checks
  101. * for each *rtpproxy* function call. (andrei)
  102. *
  103. * 2004-03-22 Fix assignment of rtpproxy_retr and rtpproxy_tout module
  104. * parameters.
  105. *
  106. * 2004-03-22 Fix get_body position (should be called before get_callid)
  107. * (andrei)
  108. *
  109. * 2004-03-24 Fix newport for null ip address case (e.g onhold re-INVITE)
  110. * (andrei)
  111. *
  112. * 2004-09-30 added received port != via port test (andrei)
  113. *
  114. * 2004-10-10 force_socket option introduced (jiri)
  115. *
  116. * 2005-02-24 Added support for using more than one rtp proxy, in which
  117. * case traffic will be distributed evenly among them. In addition,
  118. * each such proxy can be assigned a weight, which will specify
  119. * which share of the traffic should be placed to this particular
  120. * proxy.
  121. *
  122. * Introduce failover mechanism, so that if SER detects that one
  123. * of many proxies is no longer available it temporarily decreases
  124. * its weight to 0, so that no traffic will be assigned to it.
  125. * Such "disabled" proxies are periodically checked to see if they
  126. * are back to normal in which case respective weight is restored
  127. * resulting in traffic being sent to that proxy again.
  128. *
  129. * Those features can be enabled by specifying more than one "URI"
  130. * in the rtpproxy_sock parameter, optionally followed by the weight,
  131. * which if absent is assumed to be 1, for example:
  132. *
  133. * rtpproxy_sock="unix:/foo/bar=4 udp:1.2.3.4:3456=3 udp:5.6.7.8:5432=1"
  134. *
  135. * 2005-02-25 Force for pinging the socket returned by USRLOC (bogdan)
  136. *
  137. * 2005-03-22 support for multiple media streams added (netch)
  138. *
  139. * 2005-07-11 SIP ping support added (bogdan)
  140. *
  141. * 2005-07-14 SDP origin (o=) IP may be also changed (bogdan)
  142. *
  143. * 2006-03-08 fix_nated_sdp() may take one more param to force a specific IP;
  144. * force_rtp_proxy() accepts a new flag 's' to swap creation/
  145. * confirmation between requests/replies;
  146. * add_rcv_param() may take as parameter a flag telling if the
  147. * parameter should go to the contact URI or contact header;
  148. * (bogdan)
  149. * 2006-03-28 Support for changing session-level SDP connection (c=) IP when
  150. * media-description also includes connection information (bayan)
  151. * 2007-04-13 Support multiple sets of rtpproxies and set selection added
  152. * (ancuta)
  153. * 2007-04-26 Added some MI commands:
  154. * nh_enable_ping used to enable or disable natping
  155. * nh_enable_rtpp used to enable or disable a specific rtp proxy
  156. * nh_show_rtpp used to display information for all rtp proxies
  157. * (ancuta)
  158. * 2007-05-09 New function start_recording() allowing to start recording RTP
  159. * session in the RTP proxy (Carsten Bock - ported from SER)
  160. * 2007-09-11 Separate timer process and support for multiple timer processes
  161. * (bogdan)
  162. * 2008-12-12 Support for RTCP attribute in the SDP
  163. * (Min Wang/BASIS AudioNet - ported from SER)
  164. * 2010-08-05 Core SDP parser integrated into nathelper (osas)
  165. * 2010-10-08 Removal of deprecated force_rtp_proxy and swap flag (osas)
  166. */
  167. #include <sys/types.h>
  168. #include <sys/socket.h>
  169. #include <sys/time.h>
  170. #include <netinet/in.h>
  171. #include <netinet/in_systm.h>
  172. #ifndef __USE_BSD
  173. #define __USE_BSD
  174. #endif
  175. #include <netinet/ip.h>
  176. #ifndef __FAVOR_BSD
  177. #define __FAVOR_BSD
  178. #endif
  179. #include <netinet/udp.h>
  180. #include <arpa/inet.h>
  181. #include <sys/uio.h>
  182. #include <sys/un.h>
  183. #include <ctype.h>
  184. #include <errno.h>
  185. #include <netdb.h>
  186. #include <poll.h>
  187. #include <stdio.h>
  188. #include <stdlib.h>
  189. #include <string.h>
  190. #include <unistd.h>
  191. #include "../../flags.h"
  192. #include "../../sr_module.h"
  193. #include "../../dprint.h"
  194. #include "../../data_lump.h"
  195. #include "../../data_lump_rpl.h"
  196. #include "../../error.h"
  197. #include "../../forward.h"
  198. #include "../../mem/mem.h"
  199. #include "../../parser/parse_from.h"
  200. #include "../../parser/parse_to.h"
  201. #include "../../parser/parse_uri.h"
  202. #include "../../parser/parser_f.h"
  203. #include "../../parser/sdp/sdp.h"
  204. #include "../../resolve.h"
  205. #include "../../timer.h"
  206. #include "../../trim.h"
  207. #include "../../ut.h"
  208. #include "../../pt.h"
  209. #include "../../timer_proc.h"
  210. #include "../../lib/kmi/mi.h"
  211. #include "../../pvar.h"
  212. #include "../../lvalue.h"
  213. #include "../../msg_translator.h"
  214. #include "../../usr_avp.h"
  215. #include "../../socket_info.h"
  216. #include "../../mod_fix.h"
  217. #include "../../dset.h"
  218. #include "../../route.h"
  219. #include "../../modules/tm/tm_load.h"
  220. #include "rtpproxy.h"
  221. #include "rtpproxy_funcs.h"
  222. #include "rtpproxy_stream.h"
  223. MODULE_VERSION
  224. #if !defined(AF_LOCAL)
  225. #define AF_LOCAL AF_UNIX
  226. #endif
  227. #if !defined(PF_LOCAL)
  228. #define PF_LOCAL PF_UNIX
  229. #endif
  230. /* NAT UAC test constants */
  231. #define NAT_UAC_TEST_C_1918 0x01
  232. #define NAT_UAC_TEST_RCVD 0x02
  233. #define NAT_UAC_TEST_V_1918 0x04
  234. #define NAT_UAC_TEST_S_1918 0x08
  235. #define NAT_UAC_TEST_RPORT 0x10
  236. #define DEFAULT_RTPP_SET_ID 0
  237. #define MI_SET_NATPING_STATE "nh_enable_ping"
  238. #define MI_DEFAULT_NATPING_STATE 1
  239. #define MI_ENABLE_RTP_PROXY "nh_enable_rtpp"
  240. #define MI_MIN_RECHECK_TICKS 0
  241. #define MI_MAX_RECHECK_TICKS (unsigned int)-1
  242. #define MI_SHOW_RTP_PROXIES "nh_show_rtpp"
  243. #define MI_RTP_PROXY_NOT_FOUND "RTP proxy not found"
  244. #define MI_RTP_PROXY_NOT_FOUND_LEN (sizeof(MI_RTP_PROXY_NOT_FOUND)-1)
  245. #define MI_PING_DISABLED "NATping disabled from script"
  246. #define MI_PING_DISABLED_LEN (sizeof(MI_PING_DISABLED)-1)
  247. #define MI_SET "set"
  248. #define MI_SET_LEN (sizeof(MI_SET)-1)
  249. #define MI_INDEX "index"
  250. #define MI_INDEX_LEN (sizeof(MI_INDEX)-1)
  251. #define MI_DISABLED "disabled"
  252. #define MI_DISABLED_LEN (sizeof(MI_DISABLED)-1)
  253. #define MI_WEIGHT "weight"
  254. #define MI_WEIGHT_LEN (sizeof(MI_WEIGHT)-1)
  255. #define MI_RECHECK_TICKS "recheck_ticks"
  256. #define MI_RECHECK_T_LEN (sizeof(MI_RECHECK_TICKS)-1)
  257. /* Supported version of the RTP proxy command protocol */
  258. #define SUP_CPROTOVER 20040107
  259. /* Required additional version of the RTP proxy command protocol */
  260. #define REQ_CPROTOVER "20050322"
  261. /* Additional version necessary for re-packetization support */
  262. #define REP_CPROTOVER "20071116"
  263. #define PTL_CPROTOVER "20081102"
  264. #define CPORT "22222"
  265. static int extract_mediaip(str *, str *, int *, char *);
  266. static int alter_mediaip(struct sip_msg *, str *, str *, int, str *, int, int);
  267. static int alter_mediaport(struct sip_msg *, str *, str *, str *, int);
  268. static int alter_rtcp(struct sip_msg *msg, str *body, str *oldport, str *newport);
  269. static char *gencookie();
  270. static int rtpp_test(struct rtpp_node*, int, int);
  271. static int unforce_rtp_proxy_f(struct sip_msg *, char *, char *);
  272. static int force_rtp_proxy(struct sip_msg *, char *, char *, int, int);
  273. static int start_recording_f(struct sip_msg *, char *, char *);
  274. static int rtpproxy_answer1_f(struct sip_msg *, char *, char *);
  275. static int rtpproxy_answer2_f(struct sip_msg *, char *, char *);
  276. static int rtpproxy_offer1_f(struct sip_msg *, char *, char *);
  277. static int rtpproxy_offer2_f(struct sip_msg *, char *, char *);
  278. static int rtpproxy_manage0(struct sip_msg *msg, char *flags, char *ip);
  279. static int rtpproxy_manage1(struct sip_msg *msg, char *flags, char *ip);
  280. static int rtpproxy_manage2(struct sip_msg *msg, char *flags, char *ip);
  281. static int add_rtpproxy_socks(struct rtpp_set * rtpp_list, char * rtpproxy);
  282. static int fixup_set_id(void ** param, int param_no);
  283. static int set_rtp_proxy_set_f(struct sip_msg * msg, char * str1, char * str2);
  284. static struct rtpp_set * select_rtpp_set(int id_set);
  285. static int rtpproxy_set_store(modparam_t type, void * val);
  286. static int rtpproxy_add_rtpproxy_set( char * rtp_proxies);
  287. static int mod_init(void);
  288. static int child_init(int);
  289. static void mod_destroy(void);
  290. /* Pseudo-Variables */
  291. static int pv_get_rtpstat_f(struct sip_msg *, pv_param_t *, pv_value_t *);
  292. /*mi commands*/
  293. static struct mi_root* mi_enable_rtp_proxy(struct mi_root* cmd_tree,
  294. void* param );
  295. static struct mi_root* mi_show_rtpproxies(struct mi_root* cmd_tree,
  296. void* param);
  297. static int rtpproxy_disable_tout = 60;
  298. static int rtpproxy_retr = 5;
  299. static int rtpproxy_tout = 1;
  300. static pid_t mypid;
  301. static unsigned int myseqn = 0;
  302. static str nortpproxy_str = str_init("a=nortpproxy:yes");
  303. static str extra_id_pv_param = {NULL, 0};
  304. static char ** rtpp_strings=0;
  305. static int rtpp_sets=0; /*used in rtpproxy_set_store()*/
  306. static int rtpp_set_count = 0;
  307. static unsigned int current_msg_id = (unsigned int)-1;
  308. /* RTP proxy balancing list */
  309. struct rtpp_set_head * rtpp_set_list =0;
  310. struct rtpp_set * selected_rtpp_set =0;
  311. struct rtpp_set * default_rtpp_set=0;
  312. static char *ice_candidate_priority_avp_param = NULL;
  313. static int ice_candidate_priority_avp_type;
  314. static int_str ice_candidate_priority_avp;
  315. /* array with the sockets used by rtpporxy (per process)*/
  316. static unsigned int rtpp_no = 0;
  317. static int *rtpp_socks = 0;
  318. typedef struct rtpp_set_link {
  319. struct rtpp_set *rset;
  320. pv_spec_t *rpv;
  321. } rtpp_set_link_t;
  322. /* tm */
  323. static struct tm_binds tmb;
  324. /*0-> disabled, 1 ->enabled*/
  325. unsigned int *natping_state=0;
  326. static str timeout_socket_str = {0, 0};
  327. static pv_elem_t *extra_id_pv = NULL;
  328. static cmd_export_t cmds[] = {
  329. {"set_rtp_proxy_set", (cmd_function)set_rtp_proxy_set_f, 1,
  330. fixup_set_id, 0,
  331. ANY_ROUTE},
  332. {"unforce_rtp_proxy", (cmd_function)unforce_rtp_proxy_f, 0,
  333. 0, 0,
  334. ANY_ROUTE},
  335. {"rtpproxy_destroy", (cmd_function)unforce_rtp_proxy_f, 0,
  336. 0, 0,
  337. ANY_ROUTE},
  338. {"unforce_rtp_proxy", (cmd_function)unforce_rtp_proxy_f, 1,
  339. 0, 0,
  340. ANY_ROUTE},
  341. {"rtpproxy_destroy", (cmd_function)unforce_rtp_proxy_f, 1,
  342. 0, 0,
  343. ANY_ROUTE},
  344. {"start_recording", (cmd_function)start_recording_f, 0,
  345. 0, 0,
  346. ANY_ROUTE },
  347. {"rtpproxy_offer", (cmd_function)rtpproxy_offer1_f, 0,
  348. 0, 0,
  349. ANY_ROUTE},
  350. {"rtpproxy_offer", (cmd_function)rtpproxy_offer1_f, 1,
  351. 0, 0,
  352. ANY_ROUTE},
  353. {"rtpproxy_offer", (cmd_function)rtpproxy_offer2_f, 2,
  354. 0, 0,
  355. ANY_ROUTE},
  356. {"rtpproxy_answer", (cmd_function)rtpproxy_answer1_f, 0,
  357. 0, 0,
  358. ANY_ROUTE},
  359. {"rtpproxy_answer", (cmd_function)rtpproxy_answer1_f, 1,
  360. 0, 0,
  361. ANY_ROUTE},
  362. {"rtpproxy_answer", (cmd_function)rtpproxy_answer2_f, 2,
  363. 0, 0,
  364. ANY_ROUTE},
  365. {"rtpproxy_stream2uac",(cmd_function)rtpproxy_stream2uac2_f, 2,
  366. fixup_var_str_int, 0,
  367. ANY_ROUTE },
  368. {"rtpproxy_stream2uas",(cmd_function)rtpproxy_stream2uas2_f, 2,
  369. fixup_var_str_int, 0,
  370. ANY_ROUTE },
  371. {"rtpproxy_stop_stream2uac",(cmd_function)rtpproxy_stop_stream2uac2_f,0,
  372. NULL, 0,
  373. ANY_ROUTE },
  374. {"rtpproxy_stop_stream2uas",(cmd_function)rtpproxy_stop_stream2uas2_f,0,
  375. NULL, 0,
  376. ANY_ROUTE },
  377. {"rtpproxy_manage", (cmd_function)rtpproxy_manage0, 0,
  378. 0, 0,
  379. ANY_ROUTE},
  380. {"rtpproxy_manage", (cmd_function)rtpproxy_manage1, 1,
  381. fixup_spve_null, fixup_free_spve_null,
  382. ANY_ROUTE},
  383. {"rtpproxy_manage", (cmd_function)rtpproxy_manage2, 2,
  384. fixup_spve_spve, fixup_free_spve_spve,
  385. ANY_ROUTE},
  386. {0, 0, 0, 0, 0, 0}
  387. };
  388. static pv_export_t mod_pvs[] = {
  389. {{"rtpstat", (sizeof("rtpstat")-1)}, /* RTP-Statistics */
  390. PVT_OTHER, pv_get_rtpstat_f, 0, 0, 0, 0, 0},
  391. {{0, 0}, 0, 0, 0, 0, 0, 0, 0}
  392. };
  393. static param_export_t params[] = {
  394. {"nortpproxy_str", STR_PARAM, &nortpproxy_str.s },
  395. {"rtpproxy_sock", STR_PARAM|USE_FUNC_PARAM,
  396. (void*)rtpproxy_set_store },
  397. {"rtpproxy_disable_tout", INT_PARAM, &rtpproxy_disable_tout },
  398. {"rtpproxy_retr", INT_PARAM, &rtpproxy_retr },
  399. {"rtpproxy_tout", INT_PARAM, &rtpproxy_tout },
  400. {"timeout_socket", STR_PARAM, &timeout_socket_str.s },
  401. {"ice_candidate_priority_avp", STR_PARAM,
  402. &ice_candidate_priority_avp_param},
  403. {"extra_id_pv", STR_PARAM, &extra_id_pv_param.s },
  404. {0, 0, 0}
  405. };
  406. static mi_export_t mi_cmds[] = {
  407. {MI_ENABLE_RTP_PROXY, mi_enable_rtp_proxy, 0, 0, 0},
  408. {MI_SHOW_RTP_PROXIES, mi_show_rtpproxies, MI_NO_INPUT_FLAG, 0, 0},
  409. { 0, 0, 0, 0, 0}
  410. };
  411. struct module_exports exports = {
  412. "rtpproxy",
  413. DEFAULT_DLFLAGS, /* dlopen flags */
  414. cmds,
  415. params,
  416. 0, /* exported statistics */
  417. mi_cmds, /* exported MI functions */
  418. mod_pvs, /* exported pseudo-variables */
  419. 0, /* extra processes */
  420. mod_init,
  421. 0, /* reply processing */
  422. mod_destroy, /* destroy function */
  423. child_init
  424. };
  425. static int rtpproxy_set_store(modparam_t type, void * val){
  426. char * p;
  427. int len;
  428. p = (char* )val;
  429. if(p==0 || *p=='\0'){
  430. return 0;
  431. }
  432. if(rtpp_sets==0){
  433. rtpp_strings = (char**)pkg_malloc(sizeof(char*));
  434. if(!rtpp_strings){
  435. LM_ERR("no pkg memory left\n");
  436. return -1;
  437. }
  438. } else {/*realloc to make room for the current set*/
  439. rtpp_strings = (char**)pkg_realloc(rtpp_strings,
  440. (rtpp_sets+1)* sizeof(char*));
  441. if(!rtpp_strings){
  442. LM_ERR("no pkg memory left\n");
  443. return -1;
  444. }
  445. }
  446. /*allocate for the current set of urls*/
  447. len = strlen(p);
  448. rtpp_strings[rtpp_sets] = (char*)pkg_malloc((len+1)*sizeof(char));
  449. if(!rtpp_strings[rtpp_sets]){
  450. LM_ERR("no pkg memory left\n");
  451. return -1;
  452. }
  453. memcpy(rtpp_strings[rtpp_sets], p, len);
  454. rtpp_strings[rtpp_sets][len] = '\0';
  455. rtpp_sets++;
  456. return 0;
  457. }
  458. static int add_rtpproxy_socks(struct rtpp_set * rtpp_list,
  459. char * rtpproxy){
  460. /* Make rtp proxies list. */
  461. char *p, *p1, *p2, *plim;
  462. struct rtpp_node *pnode;
  463. int weight;
  464. p = rtpproxy;
  465. plim = p + strlen(p);
  466. for(;;) {
  467. weight = 1;
  468. while (*p && isspace((int)*p))
  469. ++p;
  470. if (p >= plim)
  471. break;
  472. p1 = p;
  473. while (*p && !isspace((int)*p))
  474. ++p;
  475. if (p <= p1)
  476. break; /* may happen??? */
  477. /* Have weight specified? If yes, scan it */
  478. p2 = memchr(p1, '=', p - p1);
  479. if (p2 != NULL) {
  480. weight = strtoul(p2 + 1, NULL, 10);
  481. } else {
  482. p2 = p;
  483. }
  484. pnode = shm_malloc(sizeof(struct rtpp_node));
  485. if (pnode == NULL) {
  486. LM_ERR("no shm memory left\n");
  487. return -1;
  488. }
  489. memset(pnode, 0, sizeof(*pnode));
  490. pnode->idx = rtpp_no++;
  491. pnode->rn_recheck_ticks = 0;
  492. pnode->rn_weight = weight;
  493. pnode->rn_umode = 0;
  494. pnode->rn_disabled = 0;
  495. pnode->rn_url.s = shm_malloc(p2 - p1 + 1);
  496. if (pnode->rn_url.s == NULL) {
  497. shm_free(pnode);
  498. LM_ERR("no shm memory left\n");
  499. return -1;
  500. }
  501. memmove(pnode->rn_url.s, p1, p2 - p1);
  502. pnode->rn_url.s[p2 - p1] = 0;
  503. pnode->rn_url.len = p2-p1;
  504. LM_DBG("url is %s, len is %i\n", pnode->rn_url.s, pnode->rn_url.len);
  505. /* Leave only address in rn_address */
  506. pnode->rn_address = pnode->rn_url.s;
  507. if (strncasecmp(pnode->rn_address, "udp:", 4) == 0) {
  508. pnode->rn_umode = 1;
  509. pnode->rn_address += 4;
  510. } else if (strncasecmp(pnode->rn_address, "udp6:", 5) == 0) {
  511. pnode->rn_umode = 6;
  512. pnode->rn_address += 5;
  513. } else if (strncasecmp(pnode->rn_address, "unix:", 5) == 0) {
  514. pnode->rn_umode = 0;
  515. pnode->rn_address += 5;
  516. }
  517. if (rtpp_list->rn_first == NULL) {
  518. rtpp_list->rn_first = pnode;
  519. } else {
  520. rtpp_list->rn_last->rn_next = pnode;
  521. }
  522. rtpp_list->rn_last = pnode;
  523. rtpp_list->rtpp_node_count++;
  524. }
  525. return 0;
  526. }
  527. /* 0-succes
  528. * -1 - erorr
  529. * */
  530. static int rtpproxy_add_rtpproxy_set( char * rtp_proxies)
  531. {
  532. char *p,*p2;
  533. struct rtpp_set * rtpp_list;
  534. unsigned int my_current_id;
  535. str id_set;
  536. int new_list;
  537. /* empty definition? */
  538. p= rtp_proxies;
  539. if(!p || *p=='\0'){
  540. return 0;
  541. }
  542. for(;*p && isspace(*p);p++);
  543. if(*p=='\0'){
  544. return 0;
  545. }
  546. rtp_proxies = strstr(p, "==");
  547. if(rtp_proxies){
  548. if(*(rtp_proxies +2)=='\0'){
  549. LM_ERR("script error -invalid rtp proxy list!\n");
  550. return -1;
  551. }
  552. *rtp_proxies = '\0';
  553. p2 = rtp_proxies-1;
  554. for(;isspace(*p2); *p2 = '\0',p2--);
  555. id_set.s = p; id_set.len = p2 - p+1;
  556. if(id_set.len <= 0 ||str2int(&id_set, &my_current_id)<0 ){
  557. LM_ERR("script error -invalid set_id value!\n");
  558. return -1;
  559. }
  560. rtp_proxies+=2;
  561. }else{
  562. rtp_proxies = p;
  563. my_current_id = DEFAULT_RTPP_SET_ID;
  564. }
  565. for(;*rtp_proxies && isspace(*rtp_proxies);rtp_proxies++);
  566. if(!(*rtp_proxies)){
  567. LM_ERR("script error -empty rtp_proxy list\n");
  568. return -1;;
  569. }
  570. /*search for the current_id*/
  571. rtpp_list = rtpp_set_list ? rtpp_set_list->rset_first : 0;
  572. while( rtpp_list != 0 && rtpp_list->id_set!=my_current_id)
  573. rtpp_list = rtpp_list->rset_next;
  574. if(rtpp_list==NULL){ /*if a new id_set : add a new set of rtpp*/
  575. rtpp_list = shm_malloc(sizeof(struct rtpp_set));
  576. if(!rtpp_list){
  577. LM_ERR("no shm memory left\n");
  578. return -1;
  579. }
  580. memset(rtpp_list, 0, sizeof(struct rtpp_set));
  581. rtpp_list->id_set = my_current_id;
  582. new_list = 1;
  583. } else {
  584. new_list = 0;
  585. }
  586. if(add_rtpproxy_socks(rtpp_list, rtp_proxies)!= 0){
  587. /*if this list will not be inserted, clean it up*/
  588. goto error;
  589. }
  590. if (new_list) {
  591. if(!rtpp_set_list){/*initialize the list of set*/
  592. rtpp_set_list = shm_malloc(sizeof(struct rtpp_set_head));
  593. if(!rtpp_set_list){
  594. LM_ERR("no shm memory left\n");
  595. return -1;
  596. }
  597. memset(rtpp_set_list, 0, sizeof(struct rtpp_set_head));
  598. }
  599. /*update the list of set info*/
  600. if(!rtpp_set_list->rset_first){
  601. rtpp_set_list->rset_first = rtpp_list;
  602. }else{
  603. rtpp_set_list->rset_last->rset_next = rtpp_list;
  604. }
  605. rtpp_set_list->rset_last = rtpp_list;
  606. rtpp_set_count++;
  607. if(my_current_id == DEFAULT_RTPP_SET_ID){
  608. default_rtpp_set = rtpp_list;
  609. }
  610. }
  611. return 0;
  612. error:
  613. return -1;
  614. }
  615. static int fixup_set_id(void ** param, int param_no)
  616. {
  617. int int_val, err;
  618. struct rtpp_set* rtpp_list;
  619. rtpp_set_link_t *rtpl = NULL;
  620. str s;
  621. rtpl = (rtpp_set_link_t*)pkg_malloc(sizeof(rtpp_set_link_t));
  622. if(rtpl==NULL) {
  623. LM_ERR("no more pkg memory\n");
  624. return -1;
  625. }
  626. memset(rtpl, 0, sizeof(rtpp_set_link_t));
  627. s.s = (char*)*param;
  628. s.len = strlen(s.s);
  629. if(s.s[0] == PV_MARKER) {
  630. int_val = pv_locate_name(&s);
  631. if(int_val<0 || int_val!=s.len) {
  632. LM_ERR("invalid parameter %s\n", s.s);
  633. return -1;
  634. }
  635. rtpl->rpv = pv_cache_get(&s);
  636. if(rtpl->rpv == NULL) {
  637. LM_ERR("invalid pv parameter %s\n", s.s);
  638. return -1;
  639. }
  640. } else {
  641. int_val = str2s(*param, strlen(*param), &err);
  642. if (err == 0) {
  643. pkg_free(*param);
  644. if((rtpp_list = select_rtpp_set(int_val)) ==0){
  645. LM_ERR("rtpp_proxy set %i not configured\n", int_val);
  646. return E_CFG;
  647. }
  648. rtpl->rset = rtpp_list;
  649. } else {
  650. LM_ERR("bad number <%s>\n", (char *)(*param));
  651. return E_CFG;
  652. }
  653. }
  654. *param = (void*)rtpl;
  655. return 0;
  656. }
  657. static struct mi_root* mi_enable_rtp_proxy(struct mi_root* cmd_tree,
  658. void* param )
  659. { struct mi_node* node;
  660. str rtpp_url;
  661. unsigned int enable;
  662. struct rtpp_set * rtpp_list;
  663. struct rtpp_node * crt_rtpp;
  664. int found;
  665. found = 0;
  666. if(rtpp_set_list ==NULL)
  667. goto end;
  668. node = cmd_tree->node.kids;
  669. if(node == NULL)
  670. return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  671. if(node->value.s == NULL || node->value.len ==0)
  672. return init_mi_tree( 400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
  673. rtpp_url = node->value;
  674. node = node->next;
  675. if(node == NULL)
  676. return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  677. enable = 0;
  678. if( strno2int( &node->value, &enable) <0)
  679. goto error;
  680. for(rtpp_list = rtpp_set_list->rset_first; rtpp_list != NULL;
  681. rtpp_list = rtpp_list->rset_next){
  682. for(crt_rtpp = rtpp_list->rn_first; crt_rtpp != NULL;
  683. crt_rtpp = crt_rtpp->rn_next){
  684. /*found a matching rtpp*/
  685. if(crt_rtpp->rn_url.len == rtpp_url.len){
  686. if(strncmp(crt_rtpp->rn_url.s, rtpp_url.s, rtpp_url.len) == 0){
  687. /*set the enabled/disabled status*/
  688. found = 1;
  689. crt_rtpp->rn_recheck_ticks =
  690. enable? MI_MIN_RECHECK_TICKS : MI_MAX_RECHECK_TICKS;
  691. crt_rtpp->rn_disabled = enable?0:1;
  692. }
  693. }
  694. }
  695. }
  696. end:
  697. if(found)
  698. return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  699. return init_mi_tree(404,MI_RTP_PROXY_NOT_FOUND,MI_RTP_PROXY_NOT_FOUND_LEN);
  700. error:
  701. return init_mi_tree( 400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
  702. }
  703. #define add_rtpp_node_int_info(_parent, _name, _name_len, _value, _child,\
  704. _len, _string, _error)\
  705. do {\
  706. (_string) = int2str((_value), &(_len));\
  707. if((_string) == 0){\
  708. LM_ERR("cannot convert int value\n");\
  709. goto _error;\
  710. }\
  711. if(((_child) = add_mi_node_child((_parent), MI_DUP_VALUE, (_name), \
  712. (_name_len), (_string), (_len)) ) == 0)\
  713. goto _error;\
  714. }while(0);
  715. static struct mi_root* mi_show_rtpproxies(struct mi_root* cmd_tree,
  716. void* param)
  717. {
  718. struct mi_node* node, *crt_node, *child;
  719. struct mi_root* root;
  720. struct mi_attr * attr;
  721. struct rtpp_set * rtpp_list;
  722. struct rtpp_node * crt_rtpp;
  723. char * string, *id;
  724. int id_len, len;
  725. string = id = 0;
  726. root = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
  727. if (!root) {
  728. LM_ERR("the MI tree cannot be initialized!\n");
  729. return 0;
  730. }
  731. if(rtpp_set_list ==NULL)
  732. return root;
  733. node = &root->node;
  734. for(rtpp_list = rtpp_set_list->rset_first; rtpp_list != NULL;
  735. rtpp_list = rtpp_list->rset_next){
  736. for(crt_rtpp = rtpp_list->rn_first; crt_rtpp != NULL;
  737. crt_rtpp = crt_rtpp->rn_next){
  738. id = int2str(rtpp_list->id_set, &id_len);
  739. if(!id){
  740. LM_ERR("cannot convert set id\n");
  741. goto error;
  742. }
  743. if(!(crt_node = add_mi_node_child(node, 0, crt_rtpp->rn_url.s,
  744. crt_rtpp->rn_url.len, 0,0)) ) {
  745. LM_ERR("cannot add the child node to the tree\n");
  746. goto error;
  747. }
  748. LM_DBG("adding node name %s \n",crt_rtpp->rn_url.s );
  749. if((attr = add_mi_attr(crt_node, MI_DUP_VALUE, MI_SET, MI_SET_LEN,
  750. id, id_len))== 0){
  751. LM_ERR("cannot add attributes to the node\n");
  752. goto error;
  753. }
  754. add_rtpp_node_int_info(crt_node, MI_INDEX, MI_INDEX_LEN,
  755. crt_rtpp->idx, child, len,string,error);
  756. add_rtpp_node_int_info(crt_node, MI_DISABLED, MI_DISABLED_LEN,
  757. crt_rtpp->rn_disabled, child, len,string,error);
  758. add_rtpp_node_int_info(crt_node, MI_WEIGHT, MI_WEIGHT_LEN,
  759. crt_rtpp->rn_weight, child, len, string,error);
  760. add_rtpp_node_int_info(crt_node, MI_RECHECK_TICKS,MI_RECHECK_T_LEN,
  761. crt_rtpp->rn_recheck_ticks, child, len, string, error);
  762. }
  763. }
  764. return root;
  765. error:
  766. if (root)
  767. free_mi_tree(root);
  768. return 0;
  769. }
  770. static int
  771. mod_init(void)
  772. {
  773. int i;
  774. pv_spec_t avp_spec;
  775. str s;
  776. unsigned short avp_flags;
  777. if(register_mi_mod(exports.name, mi_cmds)!=0)
  778. {
  779. LM_ERR("failed to register MI commands\n");
  780. return -1;
  781. }
  782. /* any rtpproxy configured? */
  783. if(rtpp_set_list)
  784. default_rtpp_set = select_rtpp_set(DEFAULT_RTPP_SET_ID);
  785. if (nortpproxy_str.s==NULL || nortpproxy_str.s[0]==0) {
  786. nortpproxy_str.len = 0;
  787. nortpproxy_str.s = NULL;
  788. } else {
  789. nortpproxy_str.len = strlen(nortpproxy_str.s);
  790. while (nortpproxy_str.len > 0 && (nortpproxy_str.s[nortpproxy_str.len - 1] == '\r' ||
  791. nortpproxy_str.s[nortpproxy_str.len - 1] == '\n'))
  792. nortpproxy_str.len--;
  793. if (nortpproxy_str.len == 0)
  794. nortpproxy_str.s = NULL;
  795. }
  796. /* storing the list of rtp proxy sets in shared memory*/
  797. for(i=0;i<rtpp_sets;i++){
  798. if(rtpproxy_add_rtpproxy_set(rtpp_strings[i]) !=0){
  799. for(;i<rtpp_sets;i++)
  800. if(rtpp_strings[i])
  801. pkg_free(rtpp_strings[i]);
  802. pkg_free(rtpp_strings);
  803. return -1;
  804. }
  805. if(rtpp_strings[i])
  806. pkg_free(rtpp_strings[i]);
  807. }
  808. if (timeout_socket_str.s==NULL || timeout_socket_str.s[0]==0) {
  809. timeout_socket_str.len = 0;
  810. timeout_socket_str.s = NULL;
  811. } else {
  812. timeout_socket_str.len = strlen(timeout_socket_str.s);
  813. }
  814. if (ice_candidate_priority_avp_param) {
  815. s.s = ice_candidate_priority_avp_param; s.len = strlen(s.s);
  816. if (pv_parse_spec(&s, &avp_spec) == 0 || avp_spec.type != PVT_AVP) {
  817. LM_ERR("malformed or non AVP definition <%s>\n", ice_candidate_priority_avp_param);
  818. return -1;
  819. }
  820. if (pv_get_avp_name(0, &(avp_spec.pvp), &ice_candidate_priority_avp, &avp_flags) != 0) {
  821. LM_ERR("invalid AVP definition <%s>\n", ice_candidate_priority_avp_param);
  822. return -1;
  823. }
  824. ice_candidate_priority_avp_type = avp_flags;
  825. }
  826. if (extra_id_pv_param.s && *extra_id_pv_param.s) {
  827. extra_id_pv_param.len = strlen(extra_id_pv_param.s);
  828. if(pv_parse_format(&extra_id_pv_param, &extra_id_pv) < 0) {
  829. LM_ERR("malformed PV string: %s\n", extra_id_pv_param.s);
  830. return -1;
  831. }
  832. } else {
  833. extra_id_pv = NULL;
  834. }
  835. if (rtpp_strings)
  836. pkg_free(rtpp_strings);
  837. if (load_tm_api( &tmb ) < 0)
  838. {
  839. LM_DBG("could not load the TM-functions - answer-offer model"
  840. " auto-detection is disabled\n");
  841. memset(&tmb, 0, sizeof(struct tm_binds));
  842. }
  843. return 0;
  844. }
  845. static int
  846. child_init(int rank)
  847. {
  848. int n;
  849. char *cp;
  850. struct addrinfo hints, *res;
  851. struct rtpp_set *rtpp_list;
  852. struct rtpp_node *pnode;
  853. if(rtpp_set_list==NULL )
  854. return 0;
  855. /* Iterate known RTP proxies - create sockets */
  856. mypid = getpid();
  857. rtpp_socks = (int*)pkg_malloc( sizeof(int)*rtpp_no );
  858. if (rtpp_socks==NULL) {
  859. LM_ERR("no more pkg memory\n");
  860. return -1;
  861. }
  862. for(rtpp_list = rtpp_set_list->rset_first; rtpp_list != 0;
  863. rtpp_list = rtpp_list->rset_next){
  864. for (pnode=rtpp_list->rn_first; pnode!=0; pnode = pnode->rn_next){
  865. char *hostname;
  866. if (pnode->rn_umode == 0) {
  867. rtpp_socks[pnode->idx] = -1;
  868. goto rptest;
  869. }
  870. /*
  871. * This is UDP or UDP6. Detect host and port; lookup host;
  872. * do connect() in order to specify peer address
  873. */
  874. hostname = (char*)pkg_malloc(sizeof(char) * (strlen(pnode->rn_address) + 1));
  875. if (hostname==NULL) {
  876. LM_ERR("no more pkg memory\n");
  877. return -1;
  878. }
  879. strcpy(hostname, pnode->rn_address);
  880. cp = strrchr(hostname, ':');
  881. if (cp != NULL) {
  882. *cp = '\0';
  883. cp++;
  884. }
  885. if (cp == NULL || *cp == '\0')
  886. cp = CPORT;
  887. memset(&hints, 0, sizeof(hints));
  888. hints.ai_flags = 0;
  889. hints.ai_family = (pnode->rn_umode == 6) ? AF_INET6 : AF_INET;
  890. hints.ai_socktype = SOCK_DGRAM;
  891. if ((n = getaddrinfo(hostname, cp, &hints, &res)) != 0) {
  892. LM_ERR("%s\n", gai_strerror(n));
  893. pkg_free(hostname);
  894. return -1;
  895. }
  896. pkg_free(hostname);
  897. rtpp_socks[pnode->idx] = socket((pnode->rn_umode == 6)
  898. ? AF_INET6 : AF_INET, SOCK_DGRAM, 0);
  899. if ( rtpp_socks[pnode->idx] == -1) {
  900. LM_ERR("can't create socket\n");
  901. freeaddrinfo(res);
  902. return -1;
  903. }
  904. if (connect( rtpp_socks[pnode->idx], res->ai_addr, res->ai_addrlen) == -1) {
  905. LM_ERR("can't connect to a RTP proxy\n");
  906. close( rtpp_socks[pnode->idx] );
  907. rtpp_socks[pnode->idx] = -1;
  908. freeaddrinfo(res);
  909. return -1;
  910. }
  911. freeaddrinfo(res);
  912. rptest:
  913. pnode->rn_disabled = rtpp_test(pnode, 0, 1);
  914. }
  915. }
  916. return 0;
  917. }
  918. static void mod_destroy(void)
  919. {
  920. struct rtpp_set * crt_list, * last_list;
  921. struct rtpp_node * crt_rtpp, *last_rtpp;
  922. /*free the shared memory*/
  923. if (natping_state)
  924. shm_free(natping_state);
  925. if(rtpp_set_list == NULL)
  926. return;
  927. for(crt_list = rtpp_set_list->rset_first; crt_list != NULL; ){
  928. for(crt_rtpp = crt_list->rn_first; crt_rtpp != NULL; ){
  929. if(crt_rtpp->rn_url.s)
  930. shm_free(crt_rtpp->rn_url.s);
  931. last_rtpp = crt_rtpp;
  932. crt_rtpp = last_rtpp->rn_next;
  933. shm_free(last_rtpp);
  934. }
  935. last_list = crt_list;
  936. crt_list = last_list->rset_next;
  937. shm_free(last_list);
  938. }
  939. shm_free(rtpp_set_list);
  940. }
  941. static int
  942. isnulladdr(str *sx, int pf)
  943. {
  944. char *cp;
  945. if (pf == AF_INET6) {
  946. for(cp = sx->s; cp < sx->s + sx->len; cp++)
  947. if (*cp != '0' && *cp != ':')
  948. return 0;
  949. return 1;
  950. }
  951. return (sx->len == 7 && memcmp("0.0.0.0", sx->s, 7) == 0);
  952. }
  953. #define ADD_ADIRECTION 0x01
  954. #define FIX_MEDIP 0x02
  955. #define ADD_ANORTPPROXY 0x04
  956. #define FIX_ORGIP 0x08
  957. #define ADIRECTION "a=direction:active"
  958. #define ADIRECTION_LEN (sizeof(ADIRECTION) - 1)
  959. #define AOLDMEDIP "a=oldmediaip:"
  960. #define AOLDMEDIP_LEN (sizeof(AOLDMEDIP) - 1)
  961. #define AOLDMEDIP6 "a=oldmediaip6:"
  962. #define AOLDMEDIP6_LEN (sizeof(AOLDMEDIP6) - 1)
  963. #define AOLDMEDPRT "a=oldmediaport:"
  964. #define AOLDMEDPRT_LEN (sizeof(AOLDMEDPRT) - 1)
  965. static inline int
  966. replace_sdp_ip(struct sip_msg* msg, str *org_body, char *line, str *ip)
  967. {
  968. str body1, oldip, newip;
  969. str body = *org_body;
  970. unsigned hasreplaced = 0;
  971. int pf, pf1 = 0;
  972. str body2;
  973. char *bodylimit = body.s + body.len;
  974. /* Iterate all lines and replace ips in them. */
  975. if (!ip) {
  976. newip.s = ip_addr2a(&msg->rcv.src_ip);
  977. newip.len = strlen(newip.s);
  978. } else {
  979. newip = *ip;
  980. }
  981. body1 = body;
  982. for(;;) {
  983. if (extract_mediaip(&body1, &oldip, &pf,line) == -1)
  984. break;
  985. if (pf != AF_INET) {
  986. LM_ERR("not an IPv4 address in '%s' SDP\n",line);
  987. return -1;
  988. }
  989. if (!pf1)
  990. pf1 = pf;
  991. else if (pf != pf1) {
  992. LM_ERR("mismatching address families in '%s' SDP\n",line);
  993. return -1;
  994. }
  995. body2.s = oldip.s + oldip.len;
  996. body2.len = bodylimit - body2.s;
  997. if (alter_mediaip(msg, &body1, &oldip, pf, &newip, pf,1) == -1) {
  998. LM_ERR("can't alter '%s' IP\n",line);
  999. return -1;
  1000. }
  1001. hasreplaced = 1;
  1002. body1 = body2;
  1003. }
  1004. if (!hasreplaced) {
  1005. LM_ERR("can't extract '%s' IP from the SDP\n",line);
  1006. return -1;
  1007. }
  1008. return 0;
  1009. }
  1010. static int
  1011. extract_mediaip(str *body, str *mediaip, int *pf, char *line)
  1012. {
  1013. char *cp, *cp1;
  1014. int len, nextisip;
  1015. cp1 = NULL;
  1016. for (cp = body->s; (len = body->s + body->len - cp) > 0;) {
  1017. cp1 = ser_memmem(cp, line, len, 2);
  1018. if (cp1 == NULL || cp1[-1] == '\n' || cp1[-1] == '\r')
  1019. break;
  1020. cp = cp1 + 2;
  1021. }
  1022. if (cp1 == NULL)
  1023. return -1;
  1024. mediaip->s = cp1 + 2;
  1025. mediaip->len = eat_line(mediaip->s, body->s + body->len - mediaip->s) - mediaip->s;
  1026. trim_len(mediaip->len, mediaip->s, *mediaip);
  1027. nextisip = 0;
  1028. for (cp = mediaip->s; cp < mediaip->s + mediaip->len;) {
  1029. len = eat_token_end(cp, mediaip->s + mediaip->len) - cp;
  1030. if (nextisip == 1) {
  1031. mediaip->s = cp;
  1032. mediaip->len = len;
  1033. nextisip++;
  1034. break;
  1035. }
  1036. if (len == 3 && memcmp(cp, "IP", 2) == 0) {
  1037. switch (cp[2]) {
  1038. case '4':
  1039. nextisip = 1;
  1040. *pf = AF_INET;
  1041. break;
  1042. case '6':
  1043. nextisip = 1;
  1044. *pf = AF_INET6;
  1045. break;
  1046. default:
  1047. break;
  1048. }
  1049. }
  1050. cp = eat_space_end(cp + len, mediaip->s + mediaip->len);
  1051. }
  1052. if (nextisip != 2 || mediaip->len == 0) {
  1053. LM_ERR("no `IP[4|6]' in `%s' field\n",line);
  1054. return -1;
  1055. }
  1056. return 1;
  1057. }
  1058. static int
  1059. alter_mediaip(struct sip_msg *msg, str *body, str *oldip, int oldpf,
  1060. str *newip, int newpf, int preserve)
  1061. {
  1062. char *buf;
  1063. int offset;
  1064. struct lump* anchor;
  1065. str omip, nip, oip;
  1066. /* check that updating mediaip is really necessary */
  1067. if (oldpf == newpf && isnulladdr(oldip, oldpf))
  1068. return 0;
  1069. if (newip->len == oldip->len &&
  1070. memcmp(newip->s, oldip->s, newip->len) == 0)
  1071. return 0;
  1072. if (preserve != 0) {
  1073. anchor = anchor_lump(msg, body->s + body->len - msg->buf, 0, 0);
  1074. if (anchor == NULL) {
  1075. LM_ERR("anchor_lump failed\n");
  1076. return -1;
  1077. }
  1078. if (oldpf == AF_INET6) {
  1079. omip.s = AOLDMEDIP6;
  1080. omip.len = AOLDMEDIP6_LEN;
  1081. } else {
  1082. omip.s = AOLDMEDIP;
  1083. omip.len = AOLDMEDIP_LEN;
  1084. }
  1085. buf = pkg_malloc(omip.len + oldip->len + CRLF_LEN);
  1086. if (buf == NULL) {
  1087. LM_ERR("out of pkg memory\n");
  1088. return -1;
  1089. }
  1090. memcpy(buf, CRLF, CRLF_LEN);
  1091. memcpy(buf + CRLF_LEN, omip.s, omip.len);
  1092. memcpy(buf + CRLF_LEN + omip.len, oldip->s, oldip->len);
  1093. if (insert_new_lump_after(anchor, buf,
  1094. omip.len + oldip->len + CRLF_LEN, 0) == NULL) {
  1095. LM_ERR("insert_new_lump_after failed\n");
  1096. pkg_free(buf);
  1097. return -1;
  1098. }
  1099. }
  1100. if (oldpf == newpf) {
  1101. nip.len = newip->len;
  1102. nip.s = pkg_malloc(nip.len);
  1103. if (nip.s == NULL) {
  1104. LM_ERR("out of pkg memory\n");
  1105. return -1;
  1106. }
  1107. memcpy(nip.s, newip->s, newip->len);
  1108. } else {
  1109. nip.len = newip->len + 2;
  1110. nip.s = pkg_malloc(nip.len);
  1111. if (nip.s == NULL) {
  1112. LM_ERR("out of pkg memory\n");
  1113. return -1;
  1114. }
  1115. memcpy(nip.s + 2, newip->s, newip->len);
  1116. nip.s[0] = (newpf == AF_INET6) ? '6' : '4';
  1117. nip.s[1] = ' ';
  1118. }
  1119. oip = *oldip;
  1120. if (oldpf != newpf) {
  1121. do {
  1122. oip.s--;
  1123. oip.len++;
  1124. } while (*oip.s != '6' && *oip.s != '4');
  1125. }
  1126. offset = oip.s - msg->buf;
  1127. anchor = del_lump(msg, offset, oip.len, 0);
  1128. if (anchor == NULL) {
  1129. LM_ERR("del_lump failed\n");
  1130. pkg_free(nip.s);
  1131. return -1;
  1132. }
  1133. if (insert_new_lump_after(anchor, nip.s, nip.len, 0) == 0) {
  1134. LM_ERR("insert_new_lump_after failed\n");
  1135. pkg_free(nip.s);
  1136. return -1;
  1137. }
  1138. return 0;
  1139. }
  1140. static int
  1141. alter_mediaport(struct sip_msg *msg, str *body, str *oldport, str *newport,
  1142. int preserve)
  1143. {
  1144. char *buf;
  1145. int offset;
  1146. struct lump* anchor;
  1147. /* check that updating mediaport is really necessary */
  1148. if (newport->len == oldport->len &&
  1149. memcmp(newport->s, oldport->s, newport->len) == 0)
  1150. return 0;
  1151. /*
  1152. * Since rewriting the same info twice will mess SDP up,
  1153. * apply simple anti foot shooting measure - put flag on
  1154. * messages that have been altered and check it when
  1155. * another request comes.
  1156. */
  1157. #if 0
  1158. /* disabled: - it propagates to the reply and we don't want this
  1159. * -- andrei */
  1160. if (msg->msg_flags & FL_SDP_PORT_AFS) {
  1161. LM_ERR("you can't rewrite the same SDP twice, check your config!\n");
  1162. return -1;
  1163. }
  1164. #endif
  1165. if (preserve != 0) {
  1166. anchor = anchor_lump(msg, body->s + body->len - msg->buf, 0, 0);
  1167. if (anchor == NULL) {
  1168. LM_ERR("anchor_lump failed\n");
  1169. return -1;
  1170. }
  1171. buf = pkg_malloc(AOLDMEDPRT_LEN + oldport->len + CRLF_LEN);
  1172. if (buf == NULL) {
  1173. LM_ERR("out of pkg memory\n");
  1174. return -1;
  1175. }
  1176. memcpy(buf, CRLF, CRLF_LEN);
  1177. memcpy(buf + CRLF_LEN, AOLDMEDPRT, AOLDMEDPRT_LEN);
  1178. memcpy(buf + CRLF_LEN + AOLDMEDPRT_LEN, oldport->s, oldport->len);
  1179. if (insert_new_lump_after(anchor, buf,
  1180. AOLDMEDPRT_LEN + oldport->len + CRLF_LEN, 0) == NULL) {
  1181. LM_ERR("insert_new_lump_after failed\n");
  1182. pkg_free(buf);
  1183. return -1;
  1184. }
  1185. }
  1186. buf = pkg_malloc(newport->len);
  1187. if (buf == NULL) {
  1188. LM_ERR("out of pkg memory\n");
  1189. return -1;
  1190. }
  1191. offset = oldport->s - msg->buf;
  1192. anchor = del_lump(msg, offset, oldport->len, 0);
  1193. if (anchor == NULL) {
  1194. LM_ERR("del_lump failed\n");
  1195. pkg_free(buf);
  1196. return -1;
  1197. }
  1198. memcpy(buf, newport->s, newport->len);
  1199. if (insert_new_lump_after(anchor, buf, newport->len, 0) == 0) {
  1200. LM_ERR("insert_new_lump_after failed\n");
  1201. pkg_free(buf);
  1202. return -1;
  1203. }
  1204. #if 0
  1205. msg->msg_flags |= FL_SDP_PORT_AFS;
  1206. #endif
  1207. return 0;
  1208. }
  1209. /*
  1210. * this function is ported from SER
  1211. */
  1212. static int
  1213. alter_rtcp(struct sip_msg *msg, str *body, str *oldport, str *newport)
  1214. {
  1215. char *buf;
  1216. int offset;
  1217. struct lump* anchor;
  1218. /* check that updating rtcpport is really necessary */
  1219. if (newport->len == oldport->len &&
  1220. memcmp(newport->s, oldport->s, newport->len) == 0)
  1221. return 0;
  1222. buf = pkg_malloc(newport->len);
  1223. if (buf == NULL) {
  1224. LM_ERR("alter_rtcp: out of memory\n");
  1225. return -1;
  1226. }
  1227. offset = oldport->s - msg->buf;
  1228. anchor = del_lump(msg, offset, oldport->len, 0);
  1229. if (anchor == NULL) {
  1230. LM_ERR("alter_rtcp: del_lump failed\n");
  1231. pkg_free(buf);
  1232. return -1;
  1233. }
  1234. memcpy(buf, newport->s, newport->len);
  1235. if (insert_new_lump_after(anchor, buf, newport->len, 0) == 0) {
  1236. LM_ERR("alter_rtcp: insert_new_lump_after failed\n");
  1237. pkg_free(buf);
  1238. return -1;
  1239. }
  1240. return 0;
  1241. }
  1242. static char *
  1243. append_filtered_ip(char *at, str *ip)
  1244. {
  1245. int i;
  1246. for (i = 0; i < ip->len; i++) {
  1247. if (isdigit(ip->s[i])) {
  1248. append_chr(at, ip->s[i]);
  1249. }
  1250. }
  1251. return at;
  1252. }
  1253. static int
  1254. insert_candidates(struct sip_msg *msg, char *where, str *ip, unsigned int port,
  1255. int priority)
  1256. {
  1257. char *buf, *at;
  1258. struct lump* anchor;
  1259. str rtp_port;
  1260. str rtcp_port;
  1261. rtcp_port.s = int2str(port+1, &rtcp_port.len); /* beware static buffer */
  1262. buf = pkg_malloc(24 + 78 + 14 + 24 + 2*ip->len + 2 + 2*rtcp_port.len + 24);
  1263. if (buf == NULL) {
  1264. LM_ERR("insert_candidates: out of memory\n");
  1265. return -1;
  1266. }
  1267. at = buf;
  1268. append_str(at, "a=candidate:", 12);
  1269. at = append_filtered_ip(at, ip);
  1270. append_str(at, " 2 UDP ", 7);
  1271. if (priority == 2) {
  1272. append_str(at, "16777214 ", 9);
  1273. } else {
  1274. append_str(at, "2197815294 ", 11);
  1275. }
  1276. append_str(at, ip->s, ip->len);
  1277. append_chr(at, ' ');
  1278. append_str(at, rtcp_port.s, rtcp_port.len);
  1279. append_str(at, " typ relay\r\n", 12);
  1280. rtp_port.s = int2str(port, &rtp_port.len); /* beware static buffer */
  1281. append_str(at, "a=candidate:", 12);
  1282. at = append_filtered_ip(at, ip);
  1283. append_str(at, " 1 UDP ", 7);
  1284. if (priority == 2) {
  1285. append_str(at, "16777215 ", 9);
  1286. } else {
  1287. append_str(at, "2197815295 ", 11);
  1288. }
  1289. append_str(at, ip->s, ip->len);
  1290. append_chr(at, ' ');
  1291. append_str(at, rtp_port.s, rtp_port.len);
  1292. append_str(at, " typ relay\r\n", 12);
  1293. LM_DBG("inserting '%.*s'\n", (int)(at - buf), buf);
  1294. anchor = anchor_lump(msg, where - msg->buf, 0, 0);
  1295. if (anchor == 0) {
  1296. LOG(L_ERR, "insert_candidates: can't get anchor\n");
  1297. pkg_free(buf);
  1298. return -1;
  1299. }
  1300. if (insert_new_lump_before(anchor, buf, at - buf, 0) == 0) {
  1301. LM_ERR("insert_candidates: insert_new_lump_before failed\n");
  1302. pkg_free(buf);
  1303. return -1;
  1304. }
  1305. return 0;
  1306. }
  1307. static char * gencookie(void)
  1308. {
  1309. static char cook[34];
  1310. sprintf(cook, "%d_%u ", (int)mypid, myseqn);
  1311. myseqn++;
  1312. return cook;
  1313. }
  1314. static int
  1315. rtpp_checkcap(struct rtpp_node *node, char *cap, int caplen)
  1316. {
  1317. char *cp;
  1318. struct iovec vf[4] = {{NULL, 0}, {"VF", 2}, {" ", 1}, {NULL, 0}};
  1319. vf[3].iov_base = cap;
  1320. vf[3].iov_len = caplen;
  1321. cp = send_rtpp_command(node, vf, 4);
  1322. if (cp == NULL)
  1323. return -1;
  1324. if (cp[0] == 'E' || atoi(cp) != 1)
  1325. return 0;
  1326. return 1;
  1327. }
  1328. static int
  1329. rtpp_test(struct rtpp_node *node, int isdisabled, int force)
  1330. {
  1331. int rtpp_ver, rval;
  1332. char *cp;
  1333. struct iovec v[2] = {{NULL, 0}, {"V", 1}};
  1334. if(node->rn_recheck_ticks == MI_MAX_RECHECK_TICKS){
  1335. LM_DBG("rtpp %s disabled for ever\n", node->rn_url.s);
  1336. return 1;
  1337. }
  1338. if (force == 0) {
  1339. if (isdisabled == 0)
  1340. return 0;
  1341. if (node->rn_recheck_ticks > get_ticks())
  1342. return 1;
  1343. }
  1344. cp = send_rtpp_command(node, v, 2);
  1345. if (cp == NULL) {
  1346. LM_WARN("can't get version of the RTP proxy\n");
  1347. goto error;
  1348. }
  1349. rtpp_ver = atoi(cp);
  1350. if (rtpp_ver != SUP_CPROTOVER) {
  1351. LM_WARN("unsupported version of RTP proxy <%s> found: %d supported,"
  1352. "%d present\n", node->rn_url.s, SUP_CPROTOVER, rtpp_ver);
  1353. goto error;
  1354. }
  1355. rval = rtpp_checkcap(node, REQ_CPROTOVER, sizeof(REQ_CPROTOVER) - 1);
  1356. if (rval == -1) {
  1357. LM_WARN("RTP proxy went down during version query\n");
  1358. goto error;
  1359. }
  1360. if (rval == 0) {
  1361. LM_WARN("of RTP proxy <%s> doesn't support required protocol version"
  1362. "%s\n", node->rn_url.s, REQ_CPROTOVER);
  1363. goto error;
  1364. }
  1365. LM_INFO("rtp proxy <%s> found, support for it %senabled\n",
  1366. node->rn_url.s, force == 0 ? "re-" : "");
  1367. /* Check for optional capabilities */
  1368. rval = rtpp_checkcap(node, REP_CPROTOVER, sizeof(REP_CPROTOVER) - 1);
  1369. if (rval != -1) {
  1370. node->rn_rep_supported = rval;
  1371. } else {
  1372. node->rn_rep_supported = 0;
  1373. }
  1374. rval = rtpp_checkcap(node, PTL_CPROTOVER, sizeof(PTL_CPROTOVER) - 1);
  1375. if (rval != -1) {
  1376. node->rn_ptl_supported = rval;
  1377. } else {
  1378. node->rn_ptl_supported = 0;
  1379. }
  1380. return 0;
  1381. error:
  1382. LM_WARN("support for RTP proxy <%s> has been disabled%s\n", node->rn_url.s,
  1383. rtpproxy_disable_tout < 0 ? "" : " temporarily");
  1384. if (rtpproxy_disable_tout >= 0)
  1385. node->rn_recheck_ticks = get_ticks() + rtpproxy_disable_tout;
  1386. return 1;
  1387. }
  1388. char *
  1389. send_rtpp_command(struct rtpp_node *node, struct iovec *v, int vcnt)
  1390. {
  1391. struct sockaddr_un addr;
  1392. int fd, len, i;
  1393. char *cp;
  1394. static char buf[256];
  1395. struct pollfd fds[1];
  1396. len = 0;
  1397. cp = buf;
  1398. if (node->rn_umode == 0) {
  1399. memset(&addr, 0, sizeof(addr));
  1400. addr.sun_family = AF_LOCAL;
  1401. strncpy(addr.sun_path, node->rn_address,
  1402. sizeof(addr.sun_path) - 1);
  1403. #ifdef HAVE_SOCKADDR_SA_LEN
  1404. addr.sun_len = strlen(addr.sun_path);
  1405. #endif
  1406. fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  1407. if (fd < 0) {
  1408. LM_ERR("can't create socket\n");
  1409. goto badproxy;
  1410. }
  1411. if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1412. close(fd);
  1413. LM_ERR("can't connect to RTP proxy\n");
  1414. goto badproxy;
  1415. }
  1416. do {
  1417. len = writev(fd, v + 1, vcnt - 1);
  1418. } while (len == -1 && errno == EINTR);
  1419. if (len <= 0) {
  1420. close(fd);
  1421. LM_ERR("can't send command to a RTP proxy\n");
  1422. goto badproxy;
  1423. }
  1424. do {
  1425. len = read(fd, buf, sizeof(buf) - 1);
  1426. } while (len == -1 && errno == EINTR);
  1427. close(fd);
  1428. if (len <= 0) {
  1429. LM_ERR("can't read reply from a RTP proxy\n");
  1430. goto badproxy;
  1431. }
  1432. } else {
  1433. fds[0].fd = rtpp_socks[node->idx];
  1434. fds[0].events = POLLIN;
  1435. fds[0].revents = 0;
  1436. /* Drain input buffer */
  1437. while ((poll(fds, 1, 0) == 1) &&
  1438. ((fds[0].revents & POLLIN) != 0)) {
  1439. recv(rtpp_socks[node->idx], buf, sizeof(buf) - 1, 0);
  1440. fds[0].revents = 0;
  1441. }
  1442. v[0].iov_base = gencookie();
  1443. v[0].iov_len = strlen(v[0].iov_base);
  1444. for (i = 0; i < rtpproxy_retr; i++) {
  1445. do {
  1446. len = writev(rtpp_socks[node->idx], v, vcnt);
  1447. } while (len == -1 && (errno == EINTR || errno == ENOBUFS));
  1448. if (len <= 0) {
  1449. LM_ERR("can't send command to a RTP proxy\n");
  1450. goto badproxy;
  1451. }
  1452. while ((poll(fds, 1, rtpproxy_tout * 1000) == 1) &&
  1453. (fds[0].revents & POLLIN) != 0) {
  1454. do {
  1455. len = recv(rtpp_socks[node->idx], buf, sizeof(buf)-1, 0);
  1456. } while (len == -1 && errno == EINTR);
  1457. if (len <= 0) {
  1458. LM_ERR("can't read reply from a RTP proxy\n");
  1459. goto badproxy;
  1460. }
  1461. if (len >= (v[0].iov_len - 1) &&
  1462. memcmp(buf, v[0].iov_base, (v[0].iov_len - 1)) == 0) {
  1463. len -= (v[0].iov_len - 1);
  1464. cp += (v[0].iov_len - 1);
  1465. if (len != 0) {
  1466. len--;
  1467. cp++;
  1468. }
  1469. goto out;
  1470. }
  1471. fds[0].revents = 0;
  1472. }
  1473. }
  1474. if (i == rtpproxy_retr) {
  1475. LM_ERR("timeout waiting reply from a RTP proxy\n");
  1476. goto badproxy;
  1477. }
  1478. }
  1479. out:
  1480. cp[len] = '\0';
  1481. return cp;
  1482. badproxy:
  1483. LM_ERR("proxy <%s> does not respond, disable it\n", node->rn_url.s);
  1484. node->rn_disabled = 1;
  1485. node->rn_recheck_ticks = get_ticks() + rtpproxy_disable_tout;
  1486. return NULL;
  1487. }
  1488. /*
  1489. * select the set with the id_set id
  1490. */
  1491. static struct rtpp_set * select_rtpp_set(int id_set ){
  1492. struct rtpp_set * rtpp_list;
  1493. /*is it a valid set_id?*/
  1494. if(!rtpp_set_list || !rtpp_set_list->rset_first){
  1495. LM_ERR("no rtp_proxy configured\n");
  1496. return 0;
  1497. }
  1498. for(rtpp_list=rtpp_set_list->rset_first; rtpp_list!=0 &&
  1499. rtpp_list->id_set!=id_set; rtpp_list=rtpp_list->rset_next);
  1500. if(!rtpp_list){
  1501. LM_ERR(" script error-invalid id_set to be selected\n");
  1502. }
  1503. return rtpp_list;
  1504. }
  1505. /*
  1506. * Main balancing routine. This does not try to keep the same proxy for
  1507. * the call if some proxies were disabled or enabled; proxy death considered
  1508. * too rare. Otherwise we should implement "mature" HA clustering, which is
  1509. * too expensive here.
  1510. */
  1511. struct rtpp_node *
  1512. select_rtpp_node(str callid, int do_test)
  1513. {
  1514. unsigned sum, sumcut, weight_sum;
  1515. struct rtpp_node* node;
  1516. int was_forced;
  1517. if(!selected_rtpp_set){
  1518. LM_ERR("script error -no valid set selected\n");
  1519. return NULL;
  1520. }
  1521. /* Most popular case: 1 proxy, nothing to calculate */
  1522. if (selected_rtpp_set->rtpp_node_count == 1) {
  1523. node = selected_rtpp_set->rn_first;
  1524. if (node->rn_disabled && node->rn_recheck_ticks <= get_ticks())
  1525. node->rn_disabled = rtpp_test(node, 1, 0);
  1526. return node->rn_disabled ? NULL : node;
  1527. }
  1528. /* XXX Use quick-and-dirty hashing algo */
  1529. for(sum = 0; callid.len > 0; callid.len--)
  1530. sum += callid.s[callid.len - 1];
  1531. sum &= 0xff;
  1532. was_forced = 0;
  1533. retry:
  1534. weight_sum = 0;
  1535. for (node=selected_rtpp_set->rn_first; node!=NULL; node=node->rn_next) {
  1536. if (node->rn_disabled && node->rn_recheck_ticks <= get_ticks()){
  1537. /* Try to enable if it's time to try. */
  1538. node->rn_disabled = rtpp_test(node, 1, 0);
  1539. }
  1540. if (!node->rn_disabled)
  1541. weight_sum += node->rn_weight;
  1542. }
  1543. if (weight_sum == 0) {
  1544. /* No proxies? Force all to be redetected, if not yet */
  1545. if (was_forced)
  1546. return NULL;
  1547. was_forced = 1;
  1548. for(node=selected_rtpp_set->rn_first; node!=NULL; node=node->rn_next) {
  1549. node->rn_disabled = rtpp_test(node, 1, 1);
  1550. }
  1551. goto retry;
  1552. }
  1553. sumcut = sum % weight_sum;
  1554. /*
  1555. * sumcut here lays from 0 to weight_sum-1.
  1556. * Scan proxy list and decrease until appropriate proxy is found.
  1557. */
  1558. for (node=selected_rtpp_set->rn_first; node!=NULL; node=node->rn_next) {
  1559. if (node->rn_disabled)
  1560. continue;
  1561. if (sumcut < node->rn_weight)
  1562. goto found;
  1563. sumcut -= node->rn_weight;
  1564. }
  1565. /* No node list */
  1566. return NULL;
  1567. found:
  1568. if (do_test) {
  1569. node->rn_disabled = rtpp_test(node, node->rn_disabled, 0);
  1570. if (node->rn_disabled)
  1571. goto retry;
  1572. }
  1573. return node;
  1574. }
  1575. static int
  1576. get_extra_id(struct sip_msg* msg, str *id_str) {
  1577. if(msg==NULL || extra_id_pv==NULL || id_str==NULL) {
  1578. LM_ERR("bad parameters\n");
  1579. return 0;
  1580. }
  1581. if (pv_printf_s(msg, extra_id_pv, id_str)<0) {
  1582. LM_ERR("cannot print the additional id\n");
  1583. return 0;
  1584. }
  1585. return 1;
  1586. }
  1587. static int
  1588. unforce_rtp_proxy_f(struct sip_msg* msg, char* flags, char* str2)
  1589. {
  1590. str callid, from_tag, to_tag, viabranch;
  1591. char *cp;
  1592. int via = 0;
  1593. int to = 1;
  1594. int extra = 0;
  1595. str extra_id;
  1596. int ret;
  1597. struct rtpp_node *node;
  1598. struct iovec v[1 + 4 + 3 + 2] = {{NULL, 0}, {"D", 1}, {" ", 1}, {NULL, 0}, {NULL, 0}, {NULL, 0}, {" ", 1}, {NULL, 0}, {" ", 1}, {NULL, 0}};
  1599. /* 1 */ /* 2 */ /* 3 */ /* 4 */ /* 5 */ /* 6 */ /* 7 */ /* 8 */ /* 9 */
  1600. for (cp = flags; cp && *cp; cp++) {
  1601. switch (*cp) {
  1602. case '1':
  1603. via = 1;
  1604. break;
  1605. case '2':
  1606. via = 2;
  1607. break;
  1608. case '3':
  1609. if(msg && msg->first_line.type == SIP_REPLY)
  1610. via = 2;
  1611. else
  1612. via = 1;
  1613. break;
  1614. case 't':
  1615. case 'T':
  1616. to = 0;
  1617. break;
  1618. case 'b':
  1619. extra = 1;
  1620. break;
  1621. case 'a':
  1622. case 'A':
  1623. case 'i':
  1624. case 'I':
  1625. case 'e':
  1626. case 'E':
  1627. case 'l':
  1628. case 'L':
  1629. case 'f':
  1630. case 'F':
  1631. case 'r':
  1632. case 'R':
  1633. case 'c':
  1634. case 'C':
  1635. case 'o':
  1636. case 'O':
  1637. case 'x':
  1638. case 'X':
  1639. case 'w':
  1640. case 'W':
  1641. case 'z':
  1642. case 'Z':
  1643. /* ignore them - they can be sent by rtpproxy_manage() */
  1644. break;
  1645. default:
  1646. LM_ERR("unknown option `%c'\n", *cp);
  1647. return -1;
  1648. }
  1649. }
  1650. if (get_callid(msg, &callid) == -1 || callid.len == 0) {
  1651. LM_ERR("can't get Call-Id field\n");
  1652. return -1;
  1653. }
  1654. to_tag.s = 0;
  1655. to_tag.len = 0;
  1656. if ((to == 1) && get_to_tag(msg, &to_tag) == -1) {
  1657. LM_ERR("can't get To tag\n");
  1658. return -1;
  1659. }
  1660. if (get_from_tag(msg, &from_tag) == -1 || from_tag.len == 0) {
  1661. LM_ERR("can't get From tag\n");
  1662. return -1;
  1663. }
  1664. if (via) {
  1665. if (via == 1)
  1666. ret = get_via_branch(msg, 1, &viabranch);
  1667. else /* (via == 2) */
  1668. ret = get_via_branch(msg, 2, &viabranch);
  1669. if (ret == -1 || viabranch.len == 0) {
  1670. LM_ERR("can't get Via branch\n");
  1671. return -1;
  1672. }
  1673. v[4].iov_base = ";";
  1674. v[4].iov_len = 1;
  1675. STR2IOVEC(viabranch, v[5]);
  1676. } else
  1677. /* Append extra id to call-id */
  1678. if (extra && extra_id_pv && get_extra_id(msg, &extra_id)) {
  1679. v[4].iov_base = ";";
  1680. v[4].iov_len = 1;
  1681. STR2IOVEC(extra_id, v[5]);
  1682. }
  1683. STR2IOVEC(callid, v[3]);
  1684. STR2IOVEC(from_tag, v[7]);
  1685. STR2IOVEC(to_tag, v[9]);
  1686. if(msg->id != current_msg_id){
  1687. selected_rtpp_set = default_rtpp_set;
  1688. }
  1689. node = select_rtpp_node(callid, 1);
  1690. if (!node) {
  1691. LM_ERR("no available proxies\n");
  1692. return -1;
  1693. }
  1694. send_rtpp_command(node, v, (to_tag.len > 0) ? 10 : 8);
  1695. return 1;
  1696. }
  1697. /* This function assumes p points to a line of requested type. */
  1698. static int
  1699. set_rtp_proxy_set_f(struct sip_msg * msg, char * str1, char * str2)
  1700. {
  1701. rtpp_set_link_t *rtpl;
  1702. pv_value_t val;
  1703. rtpl = (rtpp_set_link_t*)str1;
  1704. current_msg_id = 0;
  1705. selected_rtpp_set = 0;
  1706. if(rtpl->rset != NULL) {
  1707. current_msg_id = msg->id;
  1708. selected_rtpp_set = rtpl->rset;
  1709. } else {
  1710. if(pv_get_spec_value(msg, rtpl->rpv, &val)<0) {
  1711. LM_ERR("cannot evaluate pv param\n");
  1712. return -1;
  1713. }
  1714. if(!(val.flags & PV_VAL_INT)) {
  1715. LM_ERR("pv param must hold an integer value\n");
  1716. return -1;
  1717. }
  1718. selected_rtpp_set = select_rtpp_set(val.ri);
  1719. if(selected_rtpp_set==NULL) {
  1720. LM_ERR("could not locate rtpproxy set %d\n", val.ri);
  1721. return -1;
  1722. }
  1723. current_msg_id = msg->id;
  1724. }
  1725. return 1;
  1726. }
  1727. static int
  1728. rtpproxy_manage(struct sip_msg *msg, char *flags, char *ip)
  1729. {
  1730. char *cp = NULL;
  1731. char newip[IP_ADDR_MAX_STR_SIZE];
  1732. int method;
  1733. int nosdp;
  1734. if(msg->cseq==NULL && ((parse_headers(msg, HDR_CSEQ_F, 0)==-1)
  1735. || (msg->cseq==NULL)))
  1736. {
  1737. LM_ERR("no CSEQ header\n");
  1738. return -1;
  1739. }
  1740. method = get_cseq(msg)->method_id;
  1741. if(!(method==METHOD_INVITE || method==METHOD_ACK || method==METHOD_CANCEL
  1742. || method==METHOD_BYE || method==METHOD_UPDATE || method==METHOD_PRACK))
  1743. return -1;
  1744. if(method==METHOD_CANCEL || method==METHOD_BYE)
  1745. return unforce_rtp_proxy_f(msg, flags, 0);
  1746. if(ip==NULL)
  1747. {
  1748. cp = ip_addr2a(&msg->rcv.dst_ip);
  1749. strcpy(newip, cp);
  1750. }
  1751. if(msg->msg_flags & FL_SDP_BODY)
  1752. nosdp = 0;
  1753. else
  1754. nosdp = parse_sdp(msg);
  1755. if(msg->first_line.type == SIP_REQUEST) {
  1756. if(method==METHOD_ACK && nosdp==0)
  1757. return force_rtp_proxy(msg, flags, (cp!=NULL)?newip:ip, 0,
  1758. (ip!=NULL)?1:0);
  1759. if(method==METHOD_PRACK && nosdp==0)
  1760. return force_rtp_proxy(msg, flags, (cp!=NULL)?newip:ip, 1,
  1761. (ip!=NULL)?1:0);
  1762. if(method==METHOD_UPDATE && nosdp==0)
  1763. return force_rtp_proxy(msg, flags, (cp!=NULL)?newip:ip, 1,
  1764. (ip!=NULL)?1:0);
  1765. if(method==METHOD_INVITE && nosdp==0) {
  1766. msg->msg_flags |= FL_SDP_BODY;
  1767. if(tmb.t_gett!=NULL && tmb.t_gett()!=NULL
  1768. && tmb.t_gett()!=T_UNDEFINED)
  1769. tmb.t_gett()->uas.request->msg_flags |= FL_SDP_BODY;
  1770. if(route_type==FAILURE_ROUTE)
  1771. return unforce_rtp_proxy_f(msg, flags, 0);
  1772. return force_rtp_proxy(msg, flags, (cp!=NULL)?newip:ip, 1,
  1773. (ip!=NULL)?1:0);
  1774. }
  1775. } else if(msg->first_line.type == SIP_REPLY) {
  1776. if(msg->first_line.u.reply.statuscode>=300)
  1777. return unforce_rtp_proxy_f(msg, flags, 0);
  1778. if(nosdp==0) {
  1779. if(method==METHOD_PRACK)
  1780. return force_rtp_proxy(msg, flags, (cp!=NULL)?newip:ip, 0,
  1781. (ip!=NULL)?1:0);
  1782. if(method==METHOD_UPDATE)
  1783. return force_rtp_proxy(msg, flags, (cp!=NULL)?newip:ip, 0,
  1784. (ip!=NULL)?1:0);
  1785. if(tmb.t_gett==NULL || tmb.t_gett()==NULL
  1786. || tmb.t_gett()==T_UNDEFINED)
  1787. return force_rtp_proxy(msg, flags, (cp!=NULL)?newip:ip, 0,
  1788. (ip!=NULL)?1:0);
  1789. if(tmb.t_gett()->uas.request->msg_flags & FL_SDP_BODY)
  1790. return force_rtp_proxy(msg, flags, (cp!=NULL)?newip:ip, 0,
  1791. (ip!=NULL)?1:0);
  1792. return force_rtp_proxy(msg, flags, (cp!=NULL)?newip:ip, 1,
  1793. (ip!=NULL)?1:0);
  1794. }
  1795. }
  1796. return -1;
  1797. }
  1798. static int
  1799. rtpproxy_manage0(struct sip_msg *msg, char *flags, char *ip)
  1800. {
  1801. return rtpproxy_manage(msg, 0, 0);
  1802. }
  1803. static int
  1804. rtpproxy_manage1(struct sip_msg *msg, char *flags, char *ip)
  1805. {
  1806. str flag_str;
  1807. if(fixup_get_svalue(msg, (gparam_p)flags, &flag_str)<0)
  1808. {
  1809. LM_ERR("invalid flags parameter\n");
  1810. return -1;
  1811. }
  1812. return rtpproxy_manage(msg, flag_str.s, 0);
  1813. }
  1814. static int
  1815. rtpproxy_manage2(struct sip_msg *msg, char *flags, char *ip)
  1816. {
  1817. str flag_str;
  1818. str ip_str;
  1819. if(fixup_get_svalue(msg, (gparam_p)flags, &flag_str)<0)
  1820. {
  1821. LM_ERR("invalid flags parameter\n");
  1822. return -1;
  1823. }
  1824. if(fixup_get_svalue(msg, (gparam_p)ip, &ip_str)<0)
  1825. {
  1826. LM_ERR("invalid IP parameter\n");
  1827. return -1;
  1828. }
  1829. return rtpproxy_manage(msg, flag_str.s, ip_str.s);
  1830. }
  1831. static int
  1832. rtpproxy_offer1_f(struct sip_msg *msg, char *str1, char *str2)
  1833. {
  1834. char *cp;
  1835. char newip[IP_ADDR_MAX_STR_SIZE];
  1836. cp = ip_addr2a(&msg->rcv.dst_ip);
  1837. strcpy(newip, cp);
  1838. return force_rtp_proxy(msg, str1, newip, 1, 0);
  1839. }
  1840. static int
  1841. rtpproxy_offer2_f(struct sip_msg *msg, char *param1, char *param2)
  1842. {
  1843. return force_rtp_proxy(msg, param1, param2, 1, 1);
  1844. }
  1845. static int
  1846. rtpproxy_answer1_f(struct sip_msg *msg, char *str1, char *str2)
  1847. {
  1848. char *cp;
  1849. char newip[IP_ADDR_MAX_STR_SIZE];
  1850. if (msg->first_line.type == SIP_REQUEST)
  1851. if (msg->first_line.u.request.method_value != METHOD_ACK)
  1852. return -1;
  1853. cp = ip_addr2a(&msg->rcv.dst_ip);
  1854. strcpy(newip, cp);
  1855. return force_rtp_proxy(msg, str1, newip, 0, 0);
  1856. }
  1857. static int
  1858. rtpproxy_answer2_f(struct sip_msg *msg, char *param1, char *param2)
  1859. {
  1860. if (msg->first_line.type == SIP_REQUEST)
  1861. if (msg->first_line.u.request.method_value != METHOD_ACK)
  1862. return -1;
  1863. return force_rtp_proxy(msg, param1, param2, 0, 1);
  1864. }
  1865. struct options {
  1866. str s;
  1867. int oidx;
  1868. };
  1869. static int
  1870. append_opts(struct options *op, char ch)
  1871. {
  1872. void *p;
  1873. if (op->s.len <= op->oidx) {
  1874. p = pkg_realloc(op->s.s, op->oidx + 32);
  1875. if (p == NULL) {
  1876. return (-1);
  1877. }
  1878. op->s.s = p;
  1879. op->s.len = op->oidx + 32;
  1880. }
  1881. op->s.s[op->oidx++] = ch;
  1882. return (0);
  1883. }
  1884. static void
  1885. free_opts(struct options *op1, struct options *op2, struct options *op3)
  1886. {
  1887. if (op1->s.len > 0 && op1->s.s != NULL) {
  1888. pkg_free(op1->s.s);
  1889. op1->s.len = 0;
  1890. }
  1891. if (op2->s.len > 0 && op2->s.s != NULL) {
  1892. pkg_free(op2->s.s);
  1893. op2->s.len = 0;
  1894. }
  1895. if (op3->s.len > 0 && op3->s.s != NULL) {
  1896. pkg_free(op3->s.s);
  1897. op3->s.len = 0;
  1898. }
  1899. }
  1900. #define FORCE_RTP_PROXY_RET(e) \
  1901. do { \
  1902. free_opts(&opts, &rep_opts, &pt_opts); \
  1903. return (e); \
  1904. } while (0);
  1905. static int
  1906. force_rtp_proxy(struct sip_msg* msg, char* str1, char* str2, int offer, int forcedIP)
  1907. {
  1908. str body, body1, oldport, oldip, newport, newip;
  1909. str callid, from_tag, to_tag, tmp, payload_types;
  1910. str newrtcp = {0, 0};
  1911. str viabranch;
  1912. int create, port, len, flookup, argc, proxied, real, via, ret;
  1913. int orgip, commip;
  1914. int pf, pf1, force;
  1915. struct options opts, rep_opts, pt_opts;
  1916. char *cp, *cp1;
  1917. char *cpend, *next;
  1918. char **ap, *argv[10];
  1919. struct lump* anchor;
  1920. struct rtpp_node *node;
  1921. struct iovec v[] = {
  1922. {NULL, 0}, /* reserved (cookie) */
  1923. {NULL, 0}, /* command & common options */
  1924. {NULL, 0}, /* per-media/per-node options 1 */
  1925. {NULL, 0}, /* per-media/per-node options 2 */
  1926. {" ", 1}, /* separator */
  1927. {NULL, 0}, /* callid */
  1928. {NULL, 0}, /* via-branch separator ";" */
  1929. {NULL, 0}, /* via-branch */
  1930. {" ", 1}, /* separator */
  1931. {NULL, 7}, /* newip */
  1932. {" ", 1}, /* separator */
  1933. {NULL, 1}, /* oldport */
  1934. {" ", 1}, /* separator */
  1935. {NULL, 0}, /* from_tag */
  1936. {";", 1}, /* separator */
  1937. {NULL, 0}, /* medianum */
  1938. {" ", 1}, /* separator */
  1939. {NULL, 0}, /* to_tag */
  1940. {";", 1}, /* separator */
  1941. {NULL, 0}, /* medianum */
  1942. {" ", 1}, /* separator */
  1943. {NULL, 0}, /* Timeout-Socket */
  1944. };
  1945. int iovec_param_count;
  1946. int autobridge_ipv4v6;
  1947. int extra;
  1948. str extra_id;
  1949. char *c1p, *c2p, *bodylimit, *o1p;
  1950. char itoabuf_buf[20];
  1951. int medianum, media_multi;
  1952. str itoabuf_str;
  1953. int c1p_altered;
  1954. int sdp_session_num, sdp_stream_num;
  1955. sdp_session_cell_t* sdp_session;
  1956. sdp_stream_cell_t* sdp_stream;
  1957. int_str ice_candidate_priority_val;
  1958. memset(&opts, '\0', sizeof(opts));
  1959. memset(&rep_opts, '\0', sizeof(rep_opts));
  1960. memset(&pt_opts, '\0', sizeof(pt_opts));
  1961. /* Leave space for U/L prefix TBD later */
  1962. if (append_opts(&opts, '?') == -1) {
  1963. LM_ERR("out of pkg memory\n");
  1964. FORCE_RTP_PROXY_RET (-1);
  1965. }
  1966. flookup = force = real = orgip = commip = via = autobridge_ipv4v6 = extra = 0;
  1967. for (cp = str1; cp != NULL && *cp != '\0'; cp++) {
  1968. switch (*cp) {
  1969. case '1':
  1970. via = 1;
  1971. break;
  1972. case '2':
  1973. via = 2;
  1974. break;
  1975. case '3':
  1976. if(msg && msg->first_line.type == SIP_REPLY)
  1977. via = 2;
  1978. else
  1979. via = 1;
  1980. break;
  1981. case 'a':
  1982. case 'A':
  1983. if (append_opts(&opts, 'A') == -1) {
  1984. LM_ERR("out of pkg memory\n");
  1985. FORCE_RTP_PROXY_RET (-1);
  1986. }
  1987. real = 1;
  1988. break;
  1989. case 'b':
  1990. extra = 1;
  1991. break;
  1992. case 'i':
  1993. case 'I':
  1994. if (append_opts(&opts, 'I') == -1) {
  1995. LM_ERR("out of pkg memory\n");
  1996. FORCE_RTP_PROXY_RET (-1);
  1997. }
  1998. break;
  1999. case 'e':
  2000. case 'E':
  2001. if (append_opts(&opts, 'E') == -1) {
  2002. LM_ERR("out of pkg memory\n");
  2003. FORCE_RTP_PROXY_RET (-1);
  2004. }
  2005. break;
  2006. case 'l':
  2007. case 'L':
  2008. if (offer == 0) {
  2009. FORCE_RTP_PROXY_RET (-1);
  2010. }
  2011. flookup = 1;
  2012. break;
  2013. case 'f':
  2014. case 'F':
  2015. force = 1;
  2016. break;
  2017. case 'r':
  2018. case 'R':
  2019. real = 1;
  2020. break;
  2021. case 'c':
  2022. case 'C':
  2023. commip = 1;
  2024. break;
  2025. case 'o':
  2026. case 'O':
  2027. orgip = 1;
  2028. break;
  2029. case 'x':
  2030. case 'X':
  2031. autobridge_ipv4v6 = 1;
  2032. break;
  2033. case 'w':
  2034. case 'W':
  2035. if (append_opts(&opts, 'S') == -1) {
  2036. LM_ERR("out of pkg memory\n");
  2037. FORCE_RTP_PROXY_RET (-1);
  2038. }
  2039. break;
  2040. case 'z':
  2041. case 'Z':
  2042. if (append_opts(&rep_opts, 'Z') == -1) {
  2043. LM_ERR("out of pkg memory\n");
  2044. FORCE_RTP_PROXY_RET (-1);
  2045. }
  2046. /* If there are any digits following Z copy them into the command */
  2047. for (; cp[1] != '\0' && isdigit(cp[1]); cp++) {
  2048. if (append_opts(&rep_opts, cp[1]) == -1) {
  2049. LM_ERR("out of pkg memory\n");
  2050. FORCE_RTP_PROXY_RET (-1);
  2051. }
  2052. }
  2053. break;
  2054. case 't':
  2055. case 'T':
  2056. /* Only used in rtpproxy_destroy */
  2057. break;
  2058. default:
  2059. LM_ERR("unknown option `%c'\n", *cp);
  2060. FORCE_RTP_PROXY_RET (-1);
  2061. }
  2062. }
  2063. if (offer != 0) {
  2064. create = 1;
  2065. } else {
  2066. create = 0;
  2067. }
  2068. /* extract_body will also parse all the headers in the message as
  2069. * a side effect => don't move get_callid/get_to_tag in front of it
  2070. * -- andrei */
  2071. if (extract_body(msg, &body) == -1) {
  2072. LM_ERR("can't extract body from the message\n");
  2073. FORCE_RTP_PROXY_RET (-1);
  2074. }
  2075. if (get_callid(msg, &callid) == -1 || callid.len == 0) {
  2076. LM_ERR("can't get Call-Id field\n");
  2077. FORCE_RTP_PROXY_RET (-1);
  2078. }
  2079. to_tag.s = 0;
  2080. if (get_to_tag(msg, &to_tag) == -1) {
  2081. LM_ERR("can't get To tag\n");
  2082. FORCE_RTP_PROXY_RET (-1);
  2083. }
  2084. if (get_from_tag(msg, &from_tag) == -1 || from_tag.len == 0) {
  2085. LM_ERR("can't get From tag\n");
  2086. FORCE_RTP_PROXY_RET (-1);
  2087. }
  2088. if (via) {
  2089. if (via == 1)
  2090. ret = get_via_branch(msg, 1, &viabranch);
  2091. else /* (via == 2) */
  2092. ret = get_via_branch(msg, 2, &viabranch);
  2093. if (ret == -1 || viabranch.len == 0) {
  2094. LM_ERR("can't get Via branch\n");
  2095. FORCE_RTP_PROXY_RET (-1);
  2096. }
  2097. v[6].iov_base = ";";
  2098. v[6].iov_len = 1;
  2099. STR2IOVEC(viabranch, v[7]);
  2100. } else
  2101. /* Append extra id to call-id */
  2102. if (extra && extra_id_pv && get_extra_id(msg, &extra_id)) {
  2103. v[6].iov_base = ";";
  2104. v[6].iov_len = 1;
  2105. STR2IOVEC(extra_id, v[7]);
  2106. }
  2107. if (flookup != 0) {
  2108. if (to_tag.len == 0) {
  2109. FORCE_RTP_PROXY_RET (-1);
  2110. }
  2111. create = 0;
  2112. } else if ((msg->first_line.type == SIP_REPLY && offer != 0)
  2113. || (msg->first_line.type == SIP_REQUEST && offer == 0)) {
  2114. if (to_tag.len == 0) {
  2115. FORCE_RTP_PROXY_RET (-1);
  2116. }
  2117. tmp = from_tag;
  2118. from_tag = to_tag;
  2119. to_tag = tmp;
  2120. }
  2121. proxied = 0;
  2122. if (nortpproxy_str.len) {
  2123. for ( cp=body.s ; (len=body.s+body.len-cp) >= nortpproxy_str.len ; ) {
  2124. cp1 = ser_memmem(cp, nortpproxy_str.s, len, nortpproxy_str.len);
  2125. if (cp1 == NULL)
  2126. break;
  2127. if (cp1[-1] == '\n' || cp1[-1] == '\r') {
  2128. proxied = 1;
  2129. break;
  2130. }
  2131. cp = cp1 + nortpproxy_str.len;
  2132. }
  2133. }
  2134. if (proxied != 0 && force == 0) {
  2135. FORCE_RTP_PROXY_RET (-2);
  2136. }
  2137. /*
  2138. * Parsing of SDP body.
  2139. * It can contain a few session descriptions (each starts with
  2140. * v-line), and each session may contain a few media descriptions
  2141. * (each starts with m-line).
  2142. * We have to change ports in m-lines, and also change IP addresses in
  2143. * c-lines which can be placed either in session header (fallback for
  2144. * all medias) or media description.
  2145. * Ports should be allocated for any media. IPs all should be changed
  2146. * to the same value (RTP proxy IP), so we can change all c-lines
  2147. * unconditionally.
  2148. */
  2149. if(0 != parse_sdp(msg)) {
  2150. LM_ERR("Unable to parse sdp\n");
  2151. FORCE_RTP_PROXY_RET (-1);
  2152. }
  2153. #ifdef EXTRA_DEBUG
  2154. print_sdp((sdp_info_t*)msg->body, L_DBG);
  2155. #endif
  2156. bodylimit = body.s + body.len;
  2157. if(msg->id != current_msg_id){
  2158. selected_rtpp_set = default_rtpp_set;
  2159. }
  2160. opts.s.s[0] = (create == 0) ? 'L' : 'U';
  2161. v[1].iov_base = opts.s.s;
  2162. v[1].iov_len = opts.oidx;
  2163. STR2IOVEC(callid, v[5]);
  2164. STR2IOVEC(from_tag, v[13]);
  2165. STR2IOVEC(to_tag, v[17]);
  2166. if (ice_candidate_priority_avp_param) {
  2167. if (search_first_avp(ice_candidate_priority_avp_type,
  2168. ice_candidate_priority_avp,
  2169. &ice_candidate_priority_val, 0)
  2170. == NULL) {
  2171. ice_candidate_priority_val.n = 2;
  2172. } else if ((ice_candidate_priority_val.n < 1) ||
  2173. (ice_candidate_priority_val.n > 2)) {
  2174. LM_ERR("invalid ice candidate priority value %d\n",
  2175. ice_candidate_priority_val.n);
  2176. FORCE_RTP_PROXY_RET (-1);
  2177. }
  2178. } else {
  2179. ice_candidate_priority_val.n = 0;
  2180. }
  2181. /* check if this is a single or a multi stream SDP offer/answer */
  2182. sdp_stream_num = get_sdp_stream_num(msg);
  2183. switch (sdp_stream_num) {
  2184. case 0:
  2185. LM_ERR("sdp w/o streams\n");
  2186. FORCE_RTP_PROXY_RET (-1);
  2187. break;
  2188. case 1:
  2189. media_multi = 0;
  2190. break;
  2191. default:
  2192. media_multi = 1;
  2193. }
  2194. #ifdef EXTRA_DEBUG
  2195. LM_DBG("my new media_multi=%d\n", media_multi);
  2196. #endif
  2197. medianum = 0;
  2198. sdp_session_num = 0;
  2199. for(;;) {
  2200. sdp_session = get_sdp_session(msg, sdp_session_num);
  2201. if(!sdp_session) break;
  2202. sdp_stream_num = 0;
  2203. c1p_altered = 0;
  2204. o1p = sdp_session->o_ip_addr.s;
  2205. for(;;) {
  2206. sdp_stream = get_sdp_stream(msg, sdp_session_num, sdp_stream_num);
  2207. if (!sdp_stream ||
  2208. (ice_candidate_priority_val.n && sdp_stream->remote_candidates.len)) break;
  2209. if (sdp_stream->ip_addr.s && sdp_stream->ip_addr.len>0) {
  2210. oldip = sdp_stream->ip_addr;
  2211. pf = sdp_stream->pf;
  2212. } else {
  2213. oldip = sdp_session->ip_addr;
  2214. pf = sdp_session->pf;
  2215. }
  2216. oldport = sdp_stream->port;
  2217. payload_types = sdp_stream->payloads;
  2218. medianum++;
  2219. if (real != 0) {
  2220. newip = oldip;
  2221. } else {
  2222. newip.s = ip_addr2a(&msg->rcv.src_ip);
  2223. newip.len = strlen(newip.s);
  2224. }
  2225. /* XXX must compare address families in all addresses */
  2226. if (pf == AF_INET6) {
  2227. if (autobridge_ipv4v6 != 0) {
  2228. if ((append_opts(&opts, 'E') == -1) || (append_opts(&opts, 'I') == -1)) {
  2229. LM_ERR("out of pkg memory\n");
  2230. FORCE_RTP_PROXY_RET (-1);
  2231. }
  2232. /* Only execute once */
  2233. autobridge_ipv4v6 = 0;
  2234. }
  2235. if (append_opts(&opts, '6') == -1) {
  2236. LM_ERR("out of pkg memory\n");
  2237. FORCE_RTP_PROXY_RET (-1);
  2238. }
  2239. /* We need to update the pointers and the length here, it has changed. */
  2240. v[1].iov_base = opts.s.s;
  2241. v[1].iov_len = opts.oidx;
  2242. } else {
  2243. if (autobridge_ipv4v6 != 0) {
  2244. if ((append_opts(&opts, 'I') == -1) || (append_opts(&opts, 'E') == -1)) {
  2245. LM_ERR("out of pkg memory\n");
  2246. FORCE_RTP_PROXY_RET (-1);
  2247. }
  2248. /* We need to update the pointers and the length here, it has changed. */
  2249. v[1].iov_base = opts.s.s;
  2250. v[1].iov_len = opts.oidx;
  2251. /* Only execute once */
  2252. autobridge_ipv4v6 = 0;
  2253. }
  2254. }
  2255. STR2IOVEC(newip, v[9]);
  2256. STR2IOVEC(oldport, v[11]);
  2257. #ifdef EXTRA_DEBUG
  2258. LM_DBG("STR2IOVEC(newip[%.*s], v[9])", newip.len, newip.s);
  2259. LM_DBG("STR2IOVEC(oldport[%.*s], v[11])", oldport.len, oldport.s);
  2260. #endif
  2261. if (1 || media_multi) /* XXX netch: can't choose now*/
  2262. {
  2263. snprintf(itoabuf_buf, sizeof itoabuf_buf, "%d", medianum);
  2264. itoabuf_str.s = itoabuf_buf;
  2265. itoabuf_str.len = strlen(itoabuf_buf);
  2266. STR2IOVEC(itoabuf_str, v[15]);
  2267. STR2IOVEC(itoabuf_str, v[19]);
  2268. #ifdef EXTRA_DEBUG
  2269. LM_DBG("STR2IOVEC(itoabuf_str, v[15])\n");
  2270. LM_DBG("STR2IOVEC(itoabuf_str, v[19])\n");
  2271. #endif
  2272. } else {
  2273. v[14].iov_len = v[15].iov_len = 0;
  2274. v[18].iov_len = v[19].iov_len = 0;
  2275. }
  2276. do {
  2277. node = select_rtpp_node(callid, 1);
  2278. if (!node) {
  2279. LM_ERR("no available proxies\n");
  2280. FORCE_RTP_PROXY_RET (-3);
  2281. }
  2282. if (rep_opts.oidx > 0) {
  2283. if (node->rn_rep_supported == 0) {
  2284. LM_WARN("re-packetization is requested but is not "
  2285. "supported by the selected RTP proxy node\n");
  2286. v[2].iov_len = 0;
  2287. } else {
  2288. v[2].iov_base = rep_opts.s.s;
  2289. v[2].iov_len += rep_opts.oidx;
  2290. }
  2291. }
  2292. #ifdef EXTRA_DEBUG
  2293. LM_DBG("payload_types='%.*s'\n", payload_types.len, payload_types.s);
  2294. #endif
  2295. if (sdp_stream->is_rtp && payload_types.len > 0 && node->rn_ptl_supported != 0) {
  2296. pt_opts.oidx = 0;
  2297. if (append_opts(&pt_opts, 'c') == -1) {
  2298. LM_ERR("out of pkg memory\n");
  2299. FORCE_RTP_PROXY_RET (-1);
  2300. }
  2301. /*
  2302. * Convert space-separated payload types list into
  2303. * a comma-separated list.
  2304. */
  2305. for (cp = payload_types.s;
  2306. cp < payload_types.s + payload_types.len; cp++) {
  2307. if (isdigit(*cp)) {
  2308. if (append_opts(&pt_opts, *cp) == -1) {
  2309. LM_ERR("out of pkg memory\n");
  2310. FORCE_RTP_PROXY_RET (-1);
  2311. }
  2312. continue;
  2313. }
  2314. do {
  2315. cp++;
  2316. } while (!isdigit(*cp) &&
  2317. cp < payload_types.s + payload_types.len);
  2318. /* Check EOL */
  2319. if (cp >= payload_types.s + payload_types.len)
  2320. break;
  2321. if (append_opts(&pt_opts, ',') == -1) {
  2322. LM_ERR("out of pkg memory\n");
  2323. FORCE_RTP_PROXY_RET (-1);
  2324. }
  2325. cp--;
  2326. }
  2327. v[3].iov_base = pt_opts.s.s;
  2328. v[3].iov_len = pt_opts.oidx;
  2329. } else {
  2330. v[3].iov_len = 0;
  2331. }
  2332. if (to_tag.len > 0) {
  2333. iovec_param_count = 20;
  2334. if (opts.s.s[0] == 'U' && timeout_socket_str.len > 0) {
  2335. iovec_param_count = 22;
  2336. STR2IOVEC(timeout_socket_str, v[21]);
  2337. }
  2338. } else {
  2339. iovec_param_count = 16;
  2340. }
  2341. cp = send_rtpp_command(node, v, iovec_param_count);
  2342. } while (cp == NULL);
  2343. LM_DBG("proxy reply: %s\n", cp);
  2344. /* Parse proxy reply to <argc,argv> */
  2345. argc = 0;
  2346. memset(argv, 0, sizeof(argv));
  2347. cpend=cp+strlen(cp);
  2348. next=eat_token_end(cp, cpend);
  2349. for (ap=argv; cp<cpend; cp=next+1, next=eat_token_end(cp, cpend)){
  2350. *next=0;
  2351. if (*cp != '\0') {
  2352. *ap=cp;
  2353. argc++;
  2354. if ((char*)++ap >= ((char*)argv+sizeof(argv)))
  2355. break;
  2356. }
  2357. }
  2358. if (argc < 1) {
  2359. LM_ERR("no reply from rtp proxy\n");
  2360. FORCE_RTP_PROXY_RET (-1);
  2361. }
  2362. port = atoi(argv[0]);
  2363. if (port <= 0 || port > 65535) {
  2364. if (port != 0 || flookup == 0)
  2365. LM_ERR("incorrect port %i in reply "
  2366. "from rtp proxy\n",port);
  2367. FORCE_RTP_PROXY_RET (-1);
  2368. }
  2369. pf1 = (argc >= 3 && argv[2][0] == '6') ? AF_INET6 : AF_INET;
  2370. if (isnulladdr(&oldip, pf)) {
  2371. if (pf1 == AF_INET6) {
  2372. newip.s = "::";
  2373. newip.len = 2;
  2374. } else {
  2375. newip.s = "0.0.0.0";
  2376. newip.len = 7;
  2377. }
  2378. } else {
  2379. if (forcedIP) {
  2380. newip.s = str2;
  2381. newip.len = strlen(newip.s);
  2382. #ifdef EXTRA_DEBUG
  2383. LM_DBG("forcing IP='%.*s'\n", newip.len, newip.s);
  2384. #endif
  2385. } else {
  2386. newip.s = (argc < 2) ? str2 : argv[1];
  2387. newip.len = strlen(newip.s);
  2388. }
  2389. }
  2390. /* marker to double check : newport goes: str -> int -> str ?!?! */
  2391. newport.s = int2str(port, &newport.len); /* beware static buffer */
  2392. /* Alter port. */
  2393. body1.s = sdp_stream->media.s;
  2394. body1.len = bodylimit - body1.s;
  2395. #ifdef EXTRA_DEBUG
  2396. LM_DBG("alter port body1='%.*s'\n", body1.len, body1.s);
  2397. #endif
  2398. /* do not do it if old port was 0 (means media disable)
  2399. * - check if actually should be better done in rtpptoxy,
  2400. * by returning also 0
  2401. * - or by not sending to rtpproxy the old port if 0
  2402. */
  2403. if(oldport.len!=1 || oldport.s[0]!='0')
  2404. {
  2405. if (alter_mediaport(msg, &body1, &oldport, &newport, 0) == -1) {
  2406. FORCE_RTP_PROXY_RET (-1);
  2407. }
  2408. }
  2409. /*
  2410. * Alter RTCP attribute if present. Inserting RTP port + 1 (as allocated
  2411. * by RTP proxy). No IP-address is needed in the new RTCP attribute as the
  2412. * 'c' attribute (altered below) will contain the RTP proxy IP address.
  2413. * See RFC 3605 for definition of RTCP attribute.
  2414. * ported from ser
  2415. */
  2416. if (sdp_stream->rtcp_port.s && sdp_stream->rtcp_port.len) {
  2417. newrtcp.s = int2str(port+1, &newrtcp.len); /* beware static buffer */
  2418. /* Alter port. */
  2419. body1.s = sdp_stream->rtcp_port.s;
  2420. body1.len = bodylimit - body1.s;
  2421. #ifdef EXTRA_DEBUG
  2422. LM_DBG("alter rtcp body1='%.*s'\n", body1.len, body1.s);
  2423. #endif
  2424. if (alter_rtcp(msg, &body1, &sdp_stream->rtcp_port, &newrtcp) == -1) {
  2425. FORCE_RTP_PROXY_RET (-1);
  2426. }
  2427. }
  2428. /* Add ice relay candidates */
  2429. if (ice_candidate_priority_val.n && sdp_stream->ice_attrs_num > 0) {
  2430. body1.s = sdp_stream->ice_attr->foundation.s - 12;
  2431. body1.len = bodylimit - body1.s;
  2432. if (insert_candidates(msg, sdp_stream->ice_attr->foundation.s - 12,
  2433. &newip, port, ice_candidate_priority_val.n) == -1) {
  2434. FORCE_RTP_PROXY_RET (-1);
  2435. }
  2436. }
  2437. c1p = sdp_session->ip_addr.s;
  2438. c2p = sdp_stream->ip_addr.s;
  2439. /*
  2440. * Alter IP. Don't alter IP common for the session
  2441. * more than once.
  2442. */
  2443. if (c2p != NULL || !c1p_altered) {
  2444. body1.s = c2p ? c2p : c1p;
  2445. body1.len = bodylimit - body1.s;
  2446. #ifdef EXTRA_DEBUG
  2447. LM_DBG("alter ip body1='%.*s'\n", body1.len, body1.s);
  2448. #endif
  2449. if (alter_mediaip(msg, &body1, &oldip, pf, &newip, pf1, 0)==-1) {
  2450. FORCE_RTP_PROXY_RET (-1);
  2451. }
  2452. if (!c2p)
  2453. c1p_altered = 1;
  2454. }
  2455. /*
  2456. * Alter common IP if required, but don't do it more than once.
  2457. */
  2458. if (commip && c1p && !c1p_altered) {
  2459. body1.s = c1p;
  2460. body1.len = bodylimit - body1.s;
  2461. #ifdef EXTRA_DEBUG
  2462. LM_DBG("alter common ip body1='%.*s'\n", body1.len, body1.s);
  2463. #endif
  2464. if (alter_mediaip(msg, &body1, &sdp_session->ip_addr, sdp_session->pf, &newip, pf1, 0)==-1) {
  2465. FORCE_RTP_PROXY_RET (-1);
  2466. }
  2467. c1p_altered = 1;
  2468. }
  2469. /*
  2470. * Alter the IP in "o=", but only once per session
  2471. */
  2472. if (o1p) {
  2473. body1.s = o1p;
  2474. body1.len = bodylimit - body1.s;
  2475. #ifdef EXTRA_DEBUG
  2476. LM_DBG("alter media ip body1='%.*s'\n", body1.len, body1.s);
  2477. #endif
  2478. if (alter_mediaip(msg, &body1, &sdp_session->o_ip_addr, sdp_session->o_pf, &newip, pf1, 0)==-1) {
  2479. FORCE_RTP_PROXY_RET (-1);
  2480. }
  2481. o1p = 0;
  2482. }
  2483. sdp_stream_num++;
  2484. }
  2485. sdp_session_num++;
  2486. }
  2487. free_opts(&opts, &rep_opts, &pt_opts);
  2488. if (proxied == 0 && nortpproxy_str.len) {
  2489. cp = pkg_malloc((nortpproxy_str.len + CRLF_LEN) * sizeof(char));
  2490. if (cp == NULL) {
  2491. LM_ERR("out of pkg memory\n");
  2492. return -1;
  2493. }
  2494. anchor = anchor_lump(msg, body.s + body.len - msg->buf, 0, 0);
  2495. if (anchor == NULL) {
  2496. LM_ERR("anchor_lump failed\n");
  2497. pkg_free(cp);
  2498. return -1;
  2499. }
  2500. memcpy(cp, CRLF, CRLF_LEN);
  2501. memcpy(cp + CRLF_LEN, nortpproxy_str.s, nortpproxy_str.len);
  2502. if (insert_new_lump_after(anchor, cp, nortpproxy_str.len + CRLF_LEN, 0) == NULL) {
  2503. LM_ERR("insert_new_lump_after failed\n");
  2504. pkg_free(cp);
  2505. return -1;
  2506. }
  2507. }
  2508. return 1;
  2509. }
  2510. static int start_recording_f(struct sip_msg* msg, char *foo, char *bar)
  2511. {
  2512. int nitems;
  2513. str callid = {0, 0};
  2514. str from_tag = {0, 0};
  2515. str to_tag = {0, 0};
  2516. struct rtpp_node *node;
  2517. struct iovec v[1 + 4 + 3] = {{NULL, 0}, {"R", 1}, {" ", 1}, {NULL, 0}, {" ", 1}, {NULL, 0}, {" ", 1}, {NULL, 0}};
  2518. /* 1 */ /* 2 */ /* 3 */ /* 4 */ /* 5 */ /* 6 */ /* 1 */
  2519. if (get_callid(msg, &callid) == -1 || callid.len == 0) {
  2520. LM_ERR("can't get Call-Id field\n");
  2521. return -1;
  2522. }
  2523. if (get_to_tag(msg, &to_tag) == -1) {
  2524. LM_ERR("can't get To tag\n");
  2525. return -1;
  2526. }
  2527. if (get_from_tag(msg, &from_tag) == -1 || from_tag.len == 0) {
  2528. LM_ERR("can't get From tag\n");
  2529. return -1;
  2530. }
  2531. if(msg->id != current_msg_id){
  2532. selected_rtpp_set = default_rtpp_set;
  2533. }
  2534. STR2IOVEC(callid, v[3]);
  2535. STR2IOVEC(from_tag, v[5]);
  2536. STR2IOVEC(to_tag, v[7]);
  2537. node = select_rtpp_node(callid, 1);
  2538. if (!node) {
  2539. LM_ERR("no available proxies\n");
  2540. return -1;
  2541. }
  2542. nitems = 8;
  2543. if (msg->first_line.type == SIP_REPLY) {
  2544. if (to_tag.len == 0)
  2545. return -1;
  2546. STR2IOVEC(to_tag, v[5]);
  2547. STR2IOVEC(from_tag, v[7]);
  2548. } else {
  2549. STR2IOVEC(from_tag, v[5]);
  2550. STR2IOVEC(to_tag, v[7]);
  2551. if (to_tag.len <= 0)
  2552. nitems = 6;
  2553. }
  2554. send_rtpp_command(node, v, nitems);
  2555. return 1;
  2556. }
  2557. /*
  2558. * Returns the current RTP-Statistics from the RTP-Proxy
  2559. */
  2560. static int
  2561. pv_get_rtpstat_f(struct sip_msg *msg, pv_param_t *param,
  2562. pv_value_t *res)
  2563. {
  2564. str ret_val = {0, 0};
  2565. int nitems;
  2566. str callid = {0, 0};
  2567. str from_tag = {0, 0};
  2568. str to_tag = {0, 0};
  2569. struct rtpp_node *node;
  2570. struct iovec v[1 + 4 + 3 + 1] = {{NULL, 0}, {"Q", 1}, {" ", 1}, {NULL, 0},
  2571. {" ", 1}, {NULL, 0}, {";1 ", 3}, {";1", }, {NULL, 0}};
  2572. if (get_callid(msg, &callid) == -1 || callid.len == 0) {
  2573. LM_ERR("can't get Call-Id field\n");
  2574. return pv_get_null(msg, param, res);
  2575. }
  2576. if (get_to_tag(msg, &to_tag) == -1) {
  2577. LM_ERR("can't get To tag\n");
  2578. return pv_get_null(msg, param, res);
  2579. }
  2580. if (get_from_tag(msg, &from_tag) == -1 || from_tag.len == 0) {
  2581. LM_ERR("can't get From tag\n");
  2582. return pv_get_null(msg, param, res);
  2583. }
  2584. if(msg->id != current_msg_id){
  2585. selected_rtpp_set = default_rtpp_set;
  2586. }
  2587. STR2IOVEC(callid, v[3]);
  2588. STR2IOVEC(from_tag, v[5]);
  2589. STR2IOVEC(to_tag, v[7]);
  2590. node = select_rtpp_node(callid, 1);
  2591. if (!node) {
  2592. LM_ERR("no available proxies\n");
  2593. return -1;
  2594. }
  2595. nitems = 8;
  2596. if (msg->first_line.type == SIP_REPLY) {
  2597. if (to_tag.len == 0)
  2598. return -1;
  2599. STR2IOVEC(to_tag, v[5]);
  2600. STR2IOVEC(from_tag, v[7]);
  2601. } else {
  2602. STR2IOVEC(from_tag, v[5]);
  2603. STR2IOVEC(to_tag, v[7]);
  2604. if (to_tag.len <= 0)
  2605. nitems = 6;
  2606. }
  2607. ret_val.s = send_rtpp_command(node, v, nitems);
  2608. if(ret_val.s==NULL)
  2609. return pv_get_null(msg, param, res);
  2610. ret_val.len = strlen(ret_val.s);
  2611. return pv_get_strval(msg, param, res, &ret_val);
  2612. }